Consulting

Results 1 to 9 of 9

Thread: Solved: Last Used Cell

  1. #1

    Solved: Last Used Cell

    Hi All

    How do I check for the last cell that has data in, e.g. I have a sheet which has serial numbers in col A, and in Col B there are Names, some cells are empty, I need to get to the very last cell that has data in Col B, is this possible?

    Alan

  2. #2
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Hello Alan

    Perhaps:

    Dim myCell As Range
     
    Set myCell = Cells(Application.Rows.Count,"B").End(xlUp)
    which will give you a range reference to the last cell in column B containing a value on the Activesheet.

    Richard

  3. #3
    You can also use Specialcells
    Dim rlastCl As Range
    'change the sheet to suit
    Set rlastCl = Sheet1.Cells.SpecialCells(xlCellTypeLastCell)
    MsgBox rlastCl.Address
    Hope that helps.
    Get the free Excel based Toolbar or click here for free examples & Excel Consultancy

    RoyUK

  4. #4
    I have tried both options. The first option does notihng and the second option gives me a box with $A$1 in it. I changed Sheet1 to the sheet name which is Day but then get an error. I added both options as a Sub(), is that right?

    Alan

  5. #5
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Alan,
    The first piece of code gives you a reference to the cell - it doesn't do anything with it: that's up to you. If you wanted to select it, add this line:
    [VBA]myCell.Select[/VBA]
    to the end of the sub.
    The second option uses the worksheet's code name, which is not necessarily the same as what you see on the tab - try:
    [VBA]
    Dim rlastCl As Range
    'change the sheet to suit
    Set rlastCl = Worksheets("Day").Cells.SpecialCells(xlCellTypeLastCell)
    MsgBox rlastCl.Address
    [/VBA]
    Regards,
    Rory

  6. #6
    Sorry Rory, still no luck, I have a lot to learn.

    ALan

  7. #7
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    What do you mean by "still no luck"? Are you getting errors, or nothing happening or what?

  8. #8
    I've got the first one working now Rory, I was obviously doing something wrong.

    Many thanks both of you.

    Alan

  9. #9
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Glad you got it sorted!
    Rory

Posting Permissions

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