Results 1 to 4 of 4

Thread: [Excel] Macro to delete

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Question [Excel] Macro to delete

    Hey
    Im currently in the process of editing a huge spreadsheet, step by step, and its proving to be a huge task.

    I was wondering if there is a macro out there, or if anyone can help with one, which will delete an entire row (not just remove its contents and leave it blank, delete the row and shift it up), if the cell on that row in column "G" is empty.

    The idea basically would be the code would work through every line of the spreadsheet, deleting any rows with a blank cell in the "G" column.

    so i guess it would include "Selection.Delete Shift:=xlUp" somewhere in the code...?

    any help would be apreciated?

    thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Excel] Macro to delete

    Welcome to the Forums.

    Yes, a macro recording will give you the starting point of how Excel would do part of it.

    You will want to inclide a few routines like...

    Get the row count so you are not going through the entire sheel.
    Test for G? cell if its blank.
    Delete the row if it is blank.


    Code:
    Dim i As Long
    Dim lRowCount As Long
    lRowCount = Workbooks(1).Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row
    For i = 1 To lRowCount
        If Workbooks(1).Sheets("Sheet1").Cells(i, 7).Value = vbNullString Then
            Workbooks(1).Sheets("Sheet1").Rows(i & ":" & i).Delete Shift:=xlUp
        End If
    Next
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Re: [Excel] Macro to delete

    thanks for the quick reply, especially with how early in the morning it is over there

    Ive put this into a macro, however it seems to give me
    "run time error 9, subscript out of range" error, when i debug it highlights "lRowCount = Workbooks(1).Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row" line

    There are 7590 rows in the spreadsheet im editing

    Im do know a bit of vb code, however this is in excel seems to be more confusing

    any further advice?

    thanks again

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    3

    Re: [Excel] Macro to delete

    no matter, i figured it out, the sheet had been renamed, i just had to change the name

    all sorted then, thanks =]

Posting Permissions

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



Click Here to Expand Forum to Full Width