Hi,
does anyone know how to ensure that the first empty cell within column A is selected before the file is closed.
thanks
Hi,
does anyone know how to ensure that the first empty cell within column A is selected before the file is closed.
thanks
Hi,
This in the workbook section of your VBA project
![]()
Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim x As Variant For x = 1 To 65535 Step 1 If Cells(x, 1).Value = "" Then Cells(x, 1).Activate: Exit Sub Next End Sub
Rule 1: Never merge cells
Rule 2: See rule 1
"Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".
Here are a few diffent ways to find a vacant cell, row, column
To use in code![]()
'find row before next blank cell in column A iLastRow = Range("A1").End(xlDown).Row 'find last used row in column A iLastRow = Cells(Rows.Count, "a").End(xlUp).Row 'find last used row on sheet iLastRow = Cells.Find(what:="*", SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row 'find column before next blank cell in row 1 iLastColumn = Range("A1").End(xlToRight).Column 'find last used column in row 1 iLastColumn = Cells(1, Columns.Count).End(xlToLeft).Column 'find last used column on sheet iLastColumn = Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column
sweep![]()
cells(iLastRow,"A").select or range("a" & iLastRow).select
You are better off declaring your variable as Long which is more efficient than declarring it as varient
for more info read this page
http://www.cpearson.com/excel/variables.htm
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
Thanks Mudraker,
That's a bit of a mental block for me. I always go for integer for a loop variable, and when that returns an error, I change it to Variant.
Must remember long....must remember long...
When dealing with an unknown number of rows in Excel I recommend always use Long never Integer
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks