简体   繁体   English

在Excel VBA中选择范围

[英]Selecting Ranges in Excel VBA

I would like to create a VBA subroutine that searches a table for the first column header named "Phonetic Name". 我想创建一个VBA子例程,在表中搜索名为“Phonetic Name”的第一个列标题。 Then finds the absolute last cell in the table, on the bottom right corner, and store a variable as the cell coordinate one row above the last cell. 然后在右下角找到表中的绝对最后一个单元格,并将变量存储为最后一个单元格上方一行的单元格坐标。 The subroutine would then select all cells between the first cell "Phonetic Name" and the "LastCell" variable. 然后子程序将选择第一个单元“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

How can I re-write this logic in a way that is VBA friendly? 如何以VBA友好的方式重写此逻辑?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM