我希望在单行中选择后续单元格,直到某个单元格,其值为" Total"在里面。我如何在VBA中执行此操作?我做的VBA程序依赖于行的长度,该行必须是动态的(长度可以改变)。
答案 0 :(得分:3)
Sub test()
Dim myRow As Long
Dim rngEnd As Range
Dim rngToFormat As Range
myRow = 4
Set rngEnd = Rows(myRow).Find("total")
If Not rngEnd Is Nothing Then
Set rngToFormat = Range(Cells(myRow, 1), rngEnd)
Debug.Print rngToFormat.Address
Else
Debug.Print "No total on row " & myRow
End If
End Sub
答案 1 :(得分:0)
在子内:
For i = 1 To 9999
If ActiveCell.Offset(0, i).Value = "Total" Then Exit For
If ActiveCell.Offset(0, 1).Value = "" Then Exit For
Next
If ActiveCell.Offset(0, i).Value = "Total" Then Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(ActiveCell.Row, ActiveCell.Column + i - 1)).Select
宏从Activecell中选择值" Total"。
如果你想从第5列(样本):
For i = 1 To 9999
If ActiveCell.Offset(0, i).Value = "Total" Then Exit For
If ActiveCell.Offset(0, 1).Value = "" Then Exit For
Next
If ActiveCell.Offset(0, i).Value = "Total" Then Range(Cells(ActiveCell.Row, 5), Cells(ActiveCell.Row, ActiveCell.Column + i - 1)).Select
答案 2 :(得分:0)
Sub FindTotal()
Dim rng As Variant
rng = Rows(20) ' Then number of the row where "Total" is. Keep in mind that this will add all columns to rng and which will use a lot of memory. If you can limit the number of columns to be added e.g. rng = Range("A20:Z20") as long as Total will always be within the range
i = 1
While rng(1, i) <> "Total"
i = i + 1
Wend
End Sub