+ Reply to Thread
Results 1 to 25 of 25

Find last cell in a spreadshhet using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Find last cell in a spreadshhet using VBA

    Hello everyone

    Im currently working on a spreadsheet at work and im trying to automate it a bit using VBA.

    The problem i have is that i am trying to find the last cell with data in column A, column a is full of formulas and when i use the End(xlDown) code it just keeps ending up with the last cell with a formula in but i want it to be the last cell with actual data in

    Please help
    Last edited by steve_m_2003; 11-29-2011 at 06:15 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Find last cell in a spreadshhet using VBA

    Hi,

    Not quite sure what you mean by 'data' but
    Dim vCellLast as Variant
    
    vCellLast = Range("A" & Rows.Count).End(xlUp)
    will return the vlalue of the last cell in column A to the variable vCellLast

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Find last cell in a spreadshhet using VBA

    Are you trying to say you would like to find the last cell that has values and not formulae?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Hi agasin thanks for the replys sorry i havnt i have been off work. Thaqnks richard but thats not really what i was after

    Arlu yes i am needing excel to goto the last cell with a value in instead of going to the last cell with a formula in

    I dont think i explained myself as good as i could have sorry

    Hope someone can help me

    Thanks in advance

  5. #5
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find last cell in a spreadshhet using VBA

    How about this?

    Dim LastRow As Variant
    
    LastRow = Intersect([A:A], Cells.SpecialCells(xlCellTypeConstants).EntireRow).EntireRow

  6. #6
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Quote Originally Posted by JapanDave View Post
    How about this?

    Dim LastRow As Variant
    
    LastRow = Intersect([A:A], Cells.SpecialCells(xlCellTypeConstants).EntireRow).EntireRow

    Thanks but i cant get it to work it does nothing

  7. #7
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Find last cell in a spreadshhet using VBA

    This will ignore formulae and select the last row that has data
    Range("A" & Application.WorksheetFunction.CountA(Range("A1:A60000")).Select
    Last edited by Steffen Thomsen; 11-28-2011 at 07:55 AM.
    Please take time to read the forum rules

  8. #8
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Quote Originally Posted by Steffen Thomsen View Post
    This will ignore formulae and select the last row that has data
    Range("A" & Application.WorksheetFunction.CountA(Range("A1:A60000")).Select
    Thanks i have copied it into vba and it has a problem

    Expected: list separtor or )

    So i put a bracket around A like so

    Range("A")
    But then it gives me another error of Invalid Qualifier and highlights

    .CountA

  9. #9
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Find last cell in a spreadshhet using VBA

    hi,
    as an option
    see if it helps you
    Sub Button2_Click()
    'http://www.rondebruin.nl/last.htm
    'Find the last used row in a Column: column A in this example
        Dim LastRow As Long
        With ActiveSheet
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            
        End With
        MsgBox "Row" & LastRow
    
    End Sub
    Regards, John55
    If you have issues with Code I've provided, I appreciate your feedback.
    In the event Code provided resolves your issue, please mark your Thread as SOLVED.
    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

    ...enjoy -funny parrots-

  10. #10
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Find last cell in a spreadshhet using VBA

    Sorry, missing a character, updated a bit

    Sheets(1).Range("A" & Application.WorksheetFunction.CountA(Sheets(1).Range("A1:A60000"))).Select
    Last edited by Steffen Thomsen; 11-28-2011 at 08:23 AM.

  11. #11
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Quote Originally Posted by Steffen Thomsen View Post
    Sorry, missing a character, updated a bit

    Sheets(1).Range("A" & Application.WorksheetFunction.CountA(Sheets(1).Range("A1:A60000"))).Select
    Thanks

    For some reason it keeps selecting cell A502

    My spreadsheet is 504 rows long and about 50 columns wide and everycell has a forumla to pull the information from all different sheets however it varies day to day how much information goes into it but it never goes past row 504 sometimes there maybe 50 rows worth of data or could be 500 rows of data. This data then needs to be copied into a master spreadsheet to which i do manually but i was trying to make it automatic with vba. The first row of data that gets copied is row 5 and then highlight all the way down to the ast row with data in, like i said it could be row 50 or row 500 but as every row has a formula in i think vba looks at that instead of a value.

    Hope this explains it a bit better and i am greatly appericated for all your kind help

    Steve

    Everycell in

  12. #12
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Find last cell in a spreadshhet using VBA

    Ok, then try this:

    uRows = Sheets(1).UsedRange.Rows.Count
    Range("A" & uRows - Application.WorksheetFunction.CountBlank(Sheets(1).Range("A" & uRows))).Select
    Last edited by Steffen Thomsen; 11-28-2011 at 09:36 AM.

  13. #13
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Quote Originally Posted by Steffen Thomsen View Post
    Ok, then try this:

    uRows = Sheets(1).UsedRange.Rows.Count
    Range("A" & uRows - Application.WorksheetFunction.CountBlank(Sheets(1).Range("A" & uRows))).Select
    It goes to row 503 which i dont understand as the last row is 504 but in my test spreadsheet the last row with a value that the formula returned is 446 but it skips past that and goes to row 503

    Sorry about this

  14. #14
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Find last cell in a spreadshhet using VBA

    If you need to copy the range then use this:
    This copies the range to another sheet

    Sub steffen
    lastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    lastCol = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
    Range(cells(5,lastCol),Cells(lastRow,lastCol).Copy
    
    Sheets(2).Range("A1").PasteSpecial
    end sub
    *EDIT

    use this to see if it selects the correct range

    Sub steffen
    lastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    lastCol = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
    Range(cells(5,lastCol),Cells(lastRow,lastCol).Select
    
    end sub

  15. #15
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find last cell in a spreadshhet using VBA

    I don't know if it is what you are after, but anyway.

    Sub d()
    
    Worksheets("Sheet1").UsedRange.SpecialCells(xlCellTypeFormulas).Copy
    Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteValues
    
    
    End Sub

  16. #16
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Nope none of these are working i dont have a clue what is going on but ill stick to my good old trustworthy copy and paste manually

    Thanks for all the hhelp tho people

  17. #17
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Find last cell in a spreadshhet using VBA

    Haven't really followed any of the other suggestions, but try this.

    In column A the formula goes down to A17, but the last value is in A5.
    Attached Files Attached Files
    HTH
    Regards, Jeff

  18. #18
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find last cell in a spreadshhet using VBA

    How about you post a dummy workbook with sensitive info taken out, so we can actually see what you want.

  19. #19
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Quote Originally Posted by jeffreybrown View Post
    Haven't really followed any of the other suggestions, but try this.

    In column A the formula goes down to A17, but the last value is in A5.
    Thanks for your input this has half worked, it has given me thwe cell to which i need selecting but i have tried to change the code to select the cell instead of given me a msg box.

    I have attached a file to which may help you help me

    In this sample the formulas on sheet 1 are looking at sheet 2 but the original looks at many different sheets. But on this sheet 1 i am trying to get vba to select the last cell with a value which is A457 (although its not always A457)

    Again thanks for all your help
    Attached Files Attached Files

  20. #20
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Find last cell in a spreadshhet using VBA

    Here you go.
    it will select the last row with data and not a formula.
    Please mark the thread as SOLVED if it is

    Sub FindLastData()
    
    Dim r, i As Integer
    r = Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Row
    
    For i = r To 1 Step -1
    
        If VBA.Left(Range("A" & i).Formula, 1) <> "=" Then
            Range("A" & i).Select
            Exit For
        End If
    Next
    
    End Sub

  21. #21
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Quote Originally Posted by Kelshaer View Post
    Here you go.
    it will select the last row with data and not a formula.
    Please mark the thread as SOLVED if it is

    Sub FindLastData()
    
    Dim r, i As Integer
    r = Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Row
    
    For i = r To 1 Step -1
    
        If VBA.Left(Range("A" & i).Formula, 1) <> "=" Then
            Range("A" & i).Select
            Exit For
        End If
    Next
    
    End Sub


    This selects cell A1 which contains the column labels which have been hand written and do not include a formula, sorry forgot to put column labels on the sample here is a new one
    Attached Files Attached Files

  22. #22
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Find last cell in a spreadshhet using VBA

    On your sample, this selects cell A458

    Sub findlast()
        Dim lr As Long
            For lr = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
                If Len(Range("A" & lr)) > 0 Then Exit For
            Next
        Application.Goto Range("A" & lr)
    End Sub

  23. #23
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Find last cell in a spreadshhet using VBA

    This single line will select the last row with a value in it

    Sheets(1).Range("A" & Rows.Count - Application.WorksheetFunction.CountBlank(Sheets(1).Range("A1:A" & Rows.Count))).Select

  24. #24
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Find last cell in a spreadshhet using VBA

    Sub FindLastData()
    
    Dim r, i As Integer
    r = Range("A:A").Cells.SpecialCells(xlCellTypeLastCell).Row
    
    For i = r To 1 Step -1
        
        If Range("A" & i) <> "" Then
            Range("A" & i).Select
            Exit For
        End If
            
    Next
    
    End Sub

  25. #25
    Registered User
    Join Date
    07-13-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Find last cell in a spreadshhet using VBA

    Thats great thankyou everyone for your support

+ 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