Edgar Flores
Edgar Flores

Reputation: 17

Select specific column base on a cell value

enter image description here

enter image description here

I'm trying to select column "H1" to "x"(x = 5 or x = 7) columns to the right, but I want for the selection to change based on a number in a specific cell "L2".

enter image description here

Upvotes: 0

Views: 1214

Answers (2)

Shai Rado
Shai Rado

Reputation: 33672

It's hard to understand from your post, I am guessing according to the screen-shot you've attached maybe something like the code below:

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

Upvotes: 1

Kemal AL GAZZAH
Kemal AL GAZZAH

Reputation: 1037

You can do like this

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

Upvotes: 0

Related Questions