从特定行开始选择多列

时间:2018-07-01 01:30:25

标签: excel vba excel-vba

我的问题与这一问题非常相似,但我仍然不知道该如何做。  我想做的事情与这里相同:Excel VBA - select multiple columns not in sequential order,除了我不需要整个列,而是从特定行号开始的整个列。

因此,在我的情况下,我想在范围内从行号30到每个选定列的末尾有多列。但不是工作表上的所有列,只有几列。

2 个答案:

答案 0 :(得分:0)

您绝对可以使用宏录制来模拟所选第一列的内容,然后进行修改以适合您的需求

这是示例代码,将使用预定义的常量完成您想要的

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

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

这里是one of many sites with good example excel-vba code,用于选择特殊单元格

答案 1 :(得分:0)

尝试行和列的交集。

Dim lr As Long

With Worksheets("sheet10")
    lr = .Cells.Find(what:="*", after:=.Cells(1), _
                     SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Intersect(.Range("C:C, J:L, O:O"), .Range("30:" & lr)).Select
End With