+ Reply to Thread
Results 1 to 5 of 5

Go to empty cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-04-2006
    Posts
    201

    Go to empty cell

    Hi,

    does anyone know how to ensure that the first empty cell within column A is selected before the file is closed.

    thanks

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454
    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".

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Here are a few diffent ways to find a vacant cell, row, column

    '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
    To use in code

    cells(iLastRow,"A").select
    or 
    range("a" & iLastRow).select
    sweep

    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 assisted or failed to assist you I welcome your Feedback.

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454
    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...

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    When dealing with an unknown number of rows in Excel I recommend always use Long never Integer

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1