我想创建一个VBA子例程,在表中搜索名为“Phonetic Name”的第一个列标题。然后在右下角找到表中的绝对最后一个单元格,并将变量存储为最后一个单元格上方一行的单元格坐标。然后子程序将选择第一个单元格“Phonetic Name”和“LastCell”变量之间的所有单元格。
Dim LastCol As Integer
TL = ActiveSheet.Range("A:A").Find("Phonetic Name", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True).Row
Set LastRow = Cells.Find("*", [a1], , , xlByRows, xlPrevious)
With ActiveSheet
LastCol = .Cells(TL, .Columns.Count).End(xlToLeft).Column
End With
Set LastCell = ActiveSheet.Cells(LastRow.Row - 1, LastCol)
'I would like to do something like the following...
ActiveSheet.Range("TL:LastCell").Select
Selection.Copy
如何以VBA友好的方式重写此逻辑?
答案 0 :(得分:2)
Dim LastCol As Integer
Dim TL as Range
Set TL = ActiveSheet.Range("A:A").Find("Phonetic Name", _
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
If Not TL Is Nothing Then
Set LastRow = Cells.Find("*", [a1], , , xlByRows, xlPrevious)
With ActiveSheet
LastCol = .Cells(TL.Row, .Columns.Count).End(xlToLeft).Column
End With
Set LastCell = ActiveSheet.Cells(LastRow.Row - 1, LastCol)
ActiveSheet.Range(TL,LastCell).Copy
End If