繁体   English   中英

VBA代码搜索行标题,然后选择该标题下方的整个范围

[英]VBA code to search for a row header, and select the entire range beneath that header

我对VBA还是很陌生,我正在尝试自动化一些日常任务。 我要自动化的一项任务要求我“搜索”指定的标头,然后选择整个数据列。 然后,我将该范围粘贴到另一个电子表格中。

Tamcolumn = Cells.Find(what:="plan_tamaward", after:="a1", searchdirection:=xlPrevious, searchorder:=xlBycolumn, lookat:=xlPart).Column

我发现这段代码对定义整个列很有用,问题是我无法定义列标题和下面的范围,而无需“选择”数据-我知道这是一个很大的问题不,不。

我希望这是有道理的。

提前致谢。

您可以将以下方法合并到代码中...

Dim Col As Long, LastRow As Long
Dim Rng As Range
If Application.CountIf(Rows(1), "plan_tamaward*") > 0 Then
    Col = Application.Match("plan_tamaward*", Rows(1), 0)
    LastRow = Cells(Rows.Count, Col).End(xlUp).Row
    Set Rng = Range(Cells(2, Col), Cells(LastRow, Col))
End If
If Not Rng Is Nothing Then
    MsgBox Rng.Address
    'do whatever you want to do with this range here
Else
    MsgBox "The column named like plan_tamaward* was not found in Row1.", vbExclamation, "Column Not Found!"
    Exit Sub
End If

暂无
暂无

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

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