【问题标题】:VBA - Get row number of a value in a sheet, and then insert a different value into a different column of that same rowVBA - 获取工作表中值的行号,然后将不同的值插入同一行的不同列
【发布时间】:2020-07-24 03:33:26
【问题描述】:

有没有办法在 A 列等列表中搜索一个值,确定该值的行号(例如,它可能是 A 列的第 40 行),转到不同的列(例如第 40 行, 列 B),然后通过宏将数据插入该列(因此它是自动完成的)。

我尝试过使用下面的代码,但似乎无法到达任何地方;

Dim Cell As Range
Dim RowNumber As Long
Columns("B:B").Select
Set cell = Selection.Find(What:="celda", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
RowNumber = Cell.Row

If cell = "celda" Then
    'find row, go to Column B of that row, and insert "abc"

Else
    'do it another thing
End If

我在下面的链接中找到了上面的代码;

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您几乎不需要对代码进行修改。试试下面...

    Sub FindAndAdd()
    Dim fCell As Range
    Dim strSearch As String
    
    strSearch = "Harun"
    
    With ActiveSheet
       Set fCell = .Columns("A:A").Find(What:=strSearch, _
            After:=.Range("A1"), _
            Lookat:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False)
            'Lookat:=xlWhole to match whole word.
    End With
    
        If Not fCell Is Nothing Then
            fCell.Offset(0, 1) = "New Value"
              Else
            MsgBox "No match found.", vbInformation, "Search Result"
        End If
    End Sub
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多