首页 > 解决方案 > 从特定行开始选择多列

问题描述

我的问题与这个问题非常相似,但我仍然不知道自己该怎么做。我想做和这里一样的事情:Excel VBA - 不按顺序选择多个列,除了我不需要整列,而是从特定行号开始的整列。

因此,就我而言,我希望在范围内有多个列,从第 30 行开始到每个选定列的末尾。但不是工作表上的所有列,只有少数。

标签: vbaexcel

解决方案


您绝对可以使用宏录制来模拟您想要选择的第一列的内容,然后根据您的需要进行修改

这是示例代码,它将使用预定义的常量执行您想要的操作

修改列列表和起始行的常量

Sub CustomColumnSelection()

    ' Describe what columns you want to select
    Const ColumnList    As String = "A,C,D"

    ' Row to start at
    Const StartAtRow    As Long = 5

    Dim lngLastRow      As Long
    Dim arrColumns      As Variant
    Dim strSelect       As String
    Dim i               As Integer

    ' Create an array to hold columns
    arrColumns = Split(ColumnList, ",")

    ' Calculate last row of data in column
    With ActiveSheet
       lngLastRow = .Cells(.Rows.Count, arrColumns(0)).End(xlUp).Row
    End With

    ' Define first column to select
    strSelect = arrColumns(0) & StartAtRow
    ' and add rows to last ne found above
    strSelect = strSelect  & ":" & arrColumns(0) & lngLastRow

    ' Add rest of columns to selection list
    For i = 1 To UBound(arrColumns)
        strSelect = strSelect & "," & arrColumns(i) & StartAtRow & ":" & arrColumns(i) & lngLastRow
    Next i

    Range(strSelect).Select

End Sub

这是具有用于选择特殊单元格的良好示例 excel-vba 代码的众多站点之一


推荐阅读