需要在运行时vba的行号

时间:2017-09-19 08:56:16

标签: vba excel-vba excel

我希望通过查找"Company Name"

来获取行号动态

我想创建一个宏,它应该使用下表中的数据更新新表。

现在我正在手动更新Sheet1.Cells(421, i - 1)。但我希望它是动态的。

此处行号421"Company1"之后为"Booking $"

请帮助使代码动态化

Sub UpdateBookings()

    x = Sheet1.Range("D1").Value

    For i = 3 To 15
        Sheet2.Cells(4, i) = (Sheet1.Cells(420, i - 1).Value * 1000) / x
    Next i

End Sub

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试下面的代码,代码注释中的解释:

Option Explicit

Sub UpdateBookings()

Dim i As Long, FindRng As Range, RowStart As Long, x

With Sheet1
    x = .Range("D1").Value

    Set FindRng = .Cells.Find(What:="Company1", LookIn:=xlValues, LookAt:=xlWhole)
                                ' found an empty cell in the specified range
    If Not FindRng Is Nothing Then ' Find was successfull
        RowStart = FindRng.Row
    Else ' Find failed
        MsgBox "Unable to find Company1"
        Exit Sub
    End If    

    For i = 3 To 15
        Sheet2.Cells(4, i) = (.Cells(RowStart, i - 1).Value * 1000) / x
    Next i

End With

End Sub