1

I have the following spreadsheet

**Columns a-i**

A    B               C       D      E      F    **G**   H       I

**Row 1**

003 His Dressing    3305    S4S     0      0    **35**  35      1           

**Row 2**

003 His Dressing    Bidding   0      0     0    **0**   0        0                          

I need a macro that will delete row 2 based on cell g being 0, but ignore row 1 as the value in column g is 35.

of course my spreadsheet has multiple rows that have column g with a value of 0, need to delete all the rows in the workbook.

so here is the sub that has an error in it, dont know why.

Sub DeleteRows()
    Dim rownum As Long
    for rownum 1 to 1000
    If Cells(rownum, 1).Value = 0 Then
        Rows(rownum).Delete
    Next rownum
    Cells(rownum, 1).Activate
End Sub
4
  • 3
    What did you try so far ??
    – Maraboc
    Commented Nov 17, 2015 at 15:26
  • 3
    Welcome to Super User. This is not a service site for someone to write macros for you. This is a Q&A for us to help you with specific questions about what you are trying to do and get stuck. Reading How to Ask may help you improve your question by showing what you have already done and what you are working with so far. We can help you from there.
    – CharlieRB
    Commented Nov 17, 2015 at 15:28
  • here is what i am trying to do in a sub
    – DanM
    Commented Nov 17, 2015 at 15:36
  • Sub DeleteRows() Dim rownum As Long for rownum 1 to 1000 If Cells(rownum, 1).Value = 0 Then Rows(rownum).Delete Next rownum Cells(rownum, 1).Activate End Sub
    – DanM
    Commented Nov 17, 2015 at 15:36

1 Answer 1

1

Here it is:

Public Sub removeRows()
    Application.ScreenUpdating = False
    Dim wkb As Workbook
    Dim wks As Worksheet
    Set wkb = ThisWorkbook
    filtercolumn = "G"
    Set wks = wkb.Sheets("Sheet1")
    totalrows = wks.Cells(Rows.Count, "A").End(xlUp).Row
    For j = totalrows To 1 Step -1
        If wks.Cells(j, filtercolumn) = 0 Then
            wks.Rows(j).Delete
        End If
    Next j
    Application.ScreenUpdating = True
    themessage = MsgBox("Finished", vbInformation)
End Sub

Open Macros with ALT + F11, inser a new module under ThisWorkbook, and paste the code on the right side.

8
  • thanks for the macro. however when i run it, i get a "run-time error '9': Subscript out of range. can you review the code and comment on why this is happening>=?
    – DanM
    Commented Nov 17, 2015 at 15:52
  • My bad!!! Change the "Hoja1" on the line Set wks = wkb.Sheets("Hoja1")to the name of your worksheet.
    – jcbermu
    Commented Nov 17, 2015 at 15:54
  • ok now the routine does not error out. however it kept the rows with a 0 and deleted the rest. need the opposite.
    – DanM
    Commented Nov 17, 2015 at 15:56
  • The variable filtercolumn = "G"indicates the column to check. Is it G?
    – jcbermu
    Commented Nov 17, 2015 at 15:58
  • so my bad. i didn't copy some data into column g for the rows that i wanted to keep. now when i run your routine, it doesn't delete anything. and i verified that column g is the correct column.
    – DanM
    Commented Nov 17, 2015 at 16:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .