Select the last cell in a column

zacheisjd

Board Regular
Joined
Oct 23, 2008
Messages
50
I am very new to VBA and have Microsoft Office Excel 2007 Power programming with VBA in which this website was listed as a good reference site. I am working on a spreadsheet where I want to select the last non-empty cell in a column and the column always has some cells that are empty, though it is NOT the last non-empty cell in the column. This spreadsheet is automatically generated and I want to change this particular column to all one format and all one number, 0042. This spreadsheet is then loaded into another system via tab-delimited txt file, so this number is meant to serve as an indicator. Sounds like I gave too much information, just wanted to be clear. Any help is appreciated. Thanks.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the board. And that book is right, this is a GREAT reference site for excel...

Here's a common code to find the last used cell in a column. This will use column A for example...

LR = Cells(Rows.Count, "A").End(xlup).Row

LR is now a variable represinting the Row # of the last used cell in column A
Now you can use that variable in a range statement like this

Range("A1:A" & LR)

Hope that helps...
 
Upvote 0
Range("E" & Cells.Rows.Count).End(xlUp).Select

will select the last non-empty cell in Col E
 
Upvote 0
I get a run-time error 91 Object Variable or With block not set. So I am basically trying to create a macro that selects a whole column from the beginning to the last non-blank cell, but there may blank cells within the column. The end function won't work as a generic macro as the data and the total amount of rows will change from report to report. Once I get this I can continue with my formatting. Thanks again!
 
Upvote 0
Try this for column E

Range("E1").Resize(Cells(Rows.Count,"E").End(xlUp).Row,1).Select
 
Upvote 0
Note, it is not actually necessary to select, you can work with ranges directly:

Code:
Sub test()
With Range("E1:E" & Range("E" & Rows.Count).End(xlUp).Row)
    .NumberFormat = "@"
    .Value = "0042"
End With
End Sub
 
Upvote 0
The last empty cell would be the last cell in the column, provided it is empty. Do you mean the first empty cell after the last non-empty cell?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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