define end column in a range

C

Chris Perry

I would like to accomplish two things in vb code:
1) create a variable that defines the last column of a
given range (i.e. lastcol = ?).
2) Then use the variable as part of a selected range (i.e.
Range("A1:lastcol")
 
J

J.E. McGimpsey

One way, among many:

If the variable rng is set to your given range:

Dim lastCol as Integer
LastCol = rng(rng.Count).Column

To use (again, one among many):

Dim newRange As Range
Set newRange = Range("A1").Resize(1, lastCol)

or
Dim newRange As Range
Set newRange = Range(Cells(1, 1), Cells(1, lastCol))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top