Finding Last Column in a row

alien3011

New Member
Joined
Nov 6, 2007
Messages
21
Hi,

I am not sure how to find the last column in a row using excel macro programming.

What I need to do is, to find the last column of row 1,

For example, I am using row 1 as my cursor, i start from column "G", and i don't know where the last column in row 1 is...

I have used lastrow but not sure how to use lastcolumn
the codes i used for last row is

Sheet1LastRow = ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi alien3011,

Try:
Code:
Sheets("Sheet1").Range("IV1").End(xlToLeft).Column
 
Upvote 0
To make Paul's more generic (ie so it will work in xl2007) you could re-write to:

Code:
Dim lngLastCol As Long
lngLastCol = Sheets("Sheet1").Cells(1,Columns.Count).End(xlToLeft).Column

This actually may not find the last column (ie if the last column eg IV1 is populated with data) in which case you could use:

Code:
With Sheets("Sheet1")
  lngLastCol = .Cells(1,1).EntireRow.Find(What:="*",After:=.Cells(1,1),SearchDirection:=xlPrevious).Column
End With

This will only fail if row 1 is entirely blank.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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