我有一个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
答案 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