根据单元格值选择特定的列

时间:2019-03-25 16:49:26

标签: excel vba

enter image description here

enter image description here

我正在尝试在右侧选择“ H1”至“ x”(x = 5或x = 7)列,但是我希望根据特定单元格“ L2”中的数字来更改选择

enter image description here

2 个答案:

答案 0 :(得分:1)

从您的帖子中很难理解,根据您所附的屏幕截图,我猜测可能是以下代码:

Option Explicit

Sub DynamicSelect()

Dim LastRow As Long, NumofColumns As Long
Dim Rng As Range

LastRow = Cells(Rows.Count, "H").End(xlUp).Row
NumofColumns = Range("L2").Value

' set the range object
Set Rng = Range("H1", Range("H1").Offset(LastRow - 1, NumofColumns))

' if you must Select (not recommended)
Rng.Select

End Sub

答案 1 :(得分:0)

您可以这样做

Sub rangeselect()
n = Cells(2, 12)
Dim rng As Range
Dim Str As String
Str = "H1:H" & n
Set rng = Range(Str)
rng.Select
End Sub