从特定单元格中选择一个范围,然后使用VBA选择范围内的特殊粘贴

时间:2018-05-03 12:39:00

标签: excel vba excel-vba

我有一个excel文件,其中我有月份数据,所以我想从活动单元格中选择F,G和H列,直到该列的最后一个数据,然后特殊粘贴它。 我正在使用此代码来选择该范围但不能这样做。它正在从F1中选择数据。

Sub selecting_range()
Dim rng As Range
Dim LastRow As Long
currentcell = ActiveCell
LastRow = Cells(Rows.Count, "F" & currentcell).End(xlUp).Row
Set rng = Range("F1:H" & LastRow)
rng.Select
End Sub

here is the snippet of my excel in which i want to select range from 'F11527'

1 个答案:

答案 0 :(得分:1)

考虑到" F"和" H"是硬编码的,那么你可以建立这样的东西:

Sub SelectingRange()

    Dim rng             As Range
    Dim lastRow         As Long

    lastRow = Cells(Rows.Count, "F").End(xlUp).Row
    Set rng = Range(Cells(ActiveCell.Row, "F"), Cells(lastRow, "H"))

    rng.Select

End Sub

或者你可以用1行编写,只是为了让别人迷惑:

Sub SelectingRange()
 Range(Cells(ActiveCell.Row, "F"), Cells(Cells(Rows.Count, "F").End(xlUp).Row, "H")).Select
End Sub