简体   繁体   English

在Excel VBA中选择多个列

[英]Selecting multiple columns in excel VBA

I have searched the web for this and came up short.. I am using this VBA to select a column based on the column header. 我在网上搜索了此内容,并简短了一下。我正在使用此VBA根据列标题选择一列。 What I want to do is also select the 3 columns to the right of the one i've currently got selected. 我还想选择当前选择的那一列右边的3列。 Either that or have this VBA loop through more than 1 column header. 要么让这个VBA循环通过多个列标题。

    Sub selectingcolumnstest()

Dim xRg As Range
Dim xRgUni As Range
Dim xFirstAddress As String
Dim xStr As String
    On Error Resume Next
    xStr = "ColumnNameHere"
    Set xRg = Range("A1:CD1").Find(xStr, , xlValues, xlWhole, , , True)
    If Not xRg Is Nothing Then
        xFirstAddress = xRg.Address
        Do
            Set xRg = Range("A1:CD1").FindNext(xRg)
            If xRgUni Is Nothing Then
                Set xRgUni = xRg
            Else
                Set xRgUni = Application.Union(xRgUni, xRg)
            End If
        Loop While (Not xRg Is Nothing) And (xRg.Address <> xFirstAddress)
    End If
    xRgUni.EntireColumn.Select



End Sub

There are 3 additional columns to the right of "ColumnNameHere" that I also want to select.. (I am going to delete these columns). 我也想选择“ ColumnNameHere”右边的3个附加列。(我将删除这些列)。 Any suggestions? 有什么建议么?

Try, 尝试,

Dim xStr As String, hdr as variant

xStr = "ColumnNameHere"

hdr = application.match(xStr, Range("A1:CD1"), 0)

if not iserror(hdr) then
    cells(1, hdr).resize(1, 4).entirecolumn.select
    'cells(1, hdr).resize(1, 4).entirecolumn.delete
end if

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

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