Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在excel VBA中选择多列_Vba_Excel_Excel Formula - Fatal编程技术网

在excel VBA中选择多列

在excel VBA中选择多列,vba,excel,excel-formula,Vba,Excel,Excel Formula,我在网上搜索了一下,结果发现很短。。我正在使用此VBA根据列标题选择列。我要做的就是选择我当前选择的那一列右边的3列。或使此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" Se

我在网上搜索了一下,结果发现很短。。我正在使用此VBA根据列标题选择列。我要做的就是选择我当前选择的那一列右边的3列。或使此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
子选择ColumnStest()
Dim xRg As范围
Dim xRgUni As范围
Dim xFirstAddress作为字符串
Dim xStr作为字符串
出错时继续下一步
xStr=“ColumnNameHere”
设置xRg=Range(“A1:CD1”)。查找(xStr,xlValues,xlWhole,True)
如果不是,那么xRg什么都不是
xFirstAddress=xRg.Address
做
设置xRg=范围(“A1:CD1”)。FindNext(xRg)
如果xRgUni什么都不是,那么
设置xRgUni=xRg
其他的
设置xRgUni=Application.Union(xRgUni,xRg)
如果结束
循环While(非xRg即无)和(xRg.Address xFirstAddress)
如果结束
xRgUni.entireclumn.Select
端接头
“ColumnNameHere”右侧还有3列我也要选择。。(我将删除这些列)。有什么建议吗?

试试看

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

这非常有效,而且它要短得多,更容易理解。我感谢你的快速反应!