vba 搜索标题名称后选择整列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21742365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:53:44  来源:igfitidea点击:

Selecting entire column after searching for header name

excelvbaexcel-vba

提问by NumberJuan

Trying to teach myself VBA and am very new to this. Stackoverflow has been an awesome resource so far so I figured I'd ask it here.

试图自学 VBA,对此我很陌生。到目前为止,Stackoverflow 一直是一个很棒的资源,所以我想我会在这里问它。

What I want it to do is find the cell named "Reference," then copy the column below it.

我想让它做的是找到名为“参考”的单元格,然后复制它下面的列。

For some reason I can't find this anywhere online and figured I'd bite the bullet and submit a question.

出于某种原因,我在网上的任何地方都找不到这个,我想我会咬紧牙关提交一个问题。

Thanks.

谢谢。

回答by Netloh

It depends on what you mean by "cell named". If the cell is in fact named "Reference" (i.e. if you have renamed the range of cell A1to Reference, you can then copy the cell below this by using the offset method:

这取决于您所说的“单元格命名”是什么意思。如果单元格实际上被命名为“参考”(即,如果您已将单元格范围重命名A1Reference,则可以使用偏移方法复制其下方的单元格:

ActiveSheet.Range("Reference").Offset(1, 0).Copy

If you instead are searching for a cell with the value "Reference" you can find this cell and copy the cell blow this by using a method like this (note that if there are multiple cells that meet the search criteria, it will copy the cell below the last found cell):

如果您要搜索值为“Reference”的单元格,您可以找到该单元格并使用这样的方法复制该单元格(请注意,如果有多个单元格符合搜索条件,它将复制该单元格在最后找到的单元格下方):

With ActiveSheet.UsedRange
    Set c = .Find("Reference", LookIn:=xlValues)
    If Not c Is Nothing Then
        ActiveSheet.Range(c.Address).Offset(1, 0).Copy
    End If
End With

If you want to copy an entire column below a specific cell, you can do something in line of the following code. In this case it will copy all of used cells below the cell C7.

如果要复制特定单元格下方的整列,可以在以下代码行中执行某些操作。在这种情况下,它将复制单元格下方的所有使用过的单元格C7

Sub CopyColumnBelow()
    Dim r As Range
    Set r = ActiveSheet.Range("C7")

    ActiveSheet.Range(r, ActiveSheet.Cells(Rows.Count, r.Column).End(xlUp).Address).Copy
End Sub

Hope you can use this to move forward learning Excel VBA.

希望您可以使用它来继续学习 Excel VBA。