Consulting

Results 1 to 5 of 5

Thread: Excel VBA - Delete last row containing data

  1. #1
    VBAX Newbie
    Joined
    Feb 2014
    Posts
    1
    Location

    Excel VBA - Delete last row containing data

    Hello,

    I would like to delete the last non-empty row on a worksheet - seems quite simple but I haven't managed to make it work !

    Using the Record Macro function, I get this :

    Sub Macro6()
        Rows("63:63").Select
        Selection.Delete Shift:=xlUp
    End Sub
    In my exemple, the row I want to delete is 63, so I'd need to replace it by the actual last row number...how can I do that ?

    Thanks !

  2. #2
    VBAX Regular
    Joined
    Oct 2011
    Posts
    13
    Location
    Hi,

    if you can tell which column will contain data at the last row, you can do something like this this:
    Sub Macro()
    Dim lastRow as integer
    lastRow = Worksheets(1).Range("A65536").End(xlUp).Row
    Worksheets(1).Rows(lastRow & ":" & lastRow).Delete shift:=xlUp
    end sub

  3. #3
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Is better as it doesn't limit you to 65536 rows.

    In older versions of Excel it was enough, but now sheets can be longer.

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    sub M_snb()
       sheets("feuille1").cells(rows.count,1).end(xlup).entirerow.delete
    end sub

Posting Permissions

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