selecting a range with an unknown col end

KNKN9

Board Regular
Joined
Mar 27, 2017
Messages
92
Hi and thanks in advanced.

I have the data set

ColA ColB ColC ColD ColE ColF ColG ColH ColI
Row 1 x blank cell x blank cell X blank cell x blank cell

I want to select the range ("A1:I1") however the data may not always end in column I so for another sheet the data may end in col Q.

Is there a code that can select this range based on the data in row 1?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
See if this is what you mean? The range is assigned to the variable rng.

Code:
With Sheets("Sheet1")
    Set rng = .Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With
 
Upvote 0
If the last column in row1 does not always have a value you could use
Code:
Sub KNKN9()
   Dim UsdCols As Long
   UsdCols = Cells.Find("*", , , , xlByColumns, xlPrevious, , , False).Column
End Sub
 
Upvote 0
This works exactly what I am after !
Thank you !
See if this is what you mean? The range is assigned to the variable rng.

Code:
With Sheets("Sheet1")
    Set rng = .Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With
 
Upvote 0
Thank you this also works !
If the last column in row1 does not always have a value you could use
Code:
Sub KNKN9()
   Dim UsdCols As Long
   UsdCols = Cells.Find("*", , , , xlByColumns, xlPrevious, , , False).Column
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top