Last Column

Dathan

New Member
Joined
Oct 25, 2006
Messages
49
I found this code and I am using it to copy a data set of unknown length:

LastRow = Range(col & Rows.Count).End(xlUp).Row

Can someone help me write something similar for a data set of unknown width? I need something that will find the last column. I've tried this:

LastCol = Range(Columns.Count & row).End(xlLeft).Column

or/and

LastCol = Range(Columns.Count & row).End(xlRight).Column

but for some reason it isn't working. Is my problem with this code or is it somewhere else in my macro? Should either of these work? In advance, thanks!
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,911
Office Version
  1. 365
Platform
  1. Windows
Neither of those will work, Range expects a string representing a range.

I assume here you've defined col as A or whatever?
Code:
LastRow = Range(col & Rows.Count).End(xlUp).Row
To find the last column try this.
Code:
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Note this will return the column number.
 

Dathan

New Member
Joined
Oct 25, 2006
Messages
49
Thanks Sergio. Can I define LastCol as the value returned from your code?
 

Dathan

New Member
Joined
Oct 25, 2006
Messages
49
Nori, your assumption is correct. I will try what you proposed. Thanks!
 

Dathan

New Member
Joined
Oct 25, 2006
Messages
49
Norie, your assumption is correct. I will try what you proposed. Thanks!
 

Watch MrExcel Video

Forum statistics

Threads
1,112,802
Messages
5,542,585
Members
410,561
Latest member
Sasha Lawrence
Top