Still struggling with VBA to delete rows

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello folks:

I posted earlier last week on this code, I have done some work on it but it is still not working as intended. I want to delete all rows in byEmployee and byPosition sheets if the row name is not listed in range A1:A in Sheet1.

Right now, the loop deletes all rows on byEmployee and by Position except for row 1 (which is my header name). I can't get it to work properly.

Any ideas where I am going wrong here?

Code:
Sub delColumnNames()
 
Dim ColName As String
Dim LastRow As Long
Dim LastCol As Long
Dim RowRange As Range
Dim RowWS As Worksheet
Dim lngLr As Long
Dim WS As Worksheet
Dim i As Long
Dim temp
 
    'List of rows (to keep)
    Set RowWS = Sheets("Sheet1")
    LastRow = RowWS.Cells(Rows.Count, 2).End(xlUp).Row
    Set RowRange = RowWS.Range("A2:A" & LastRow)
   
   
    For Each WS In Sheets(Array("byEmployee", "byPosition"))
       
        With WS
           
            LastCol = .Cells(Rows.Count, 1).End(xlUp).Row
            'Step through rows and delete those not in RowRange list or Blank
            For i = LastCol To 1 Step -1
                ColName = .Cells(2, i).Value
                If ColName = "" Then
                    'Row is blank - delete it
                    .Cells(2, LastCol).Rows.EntireRow.Delete
                Else
                    On Error Resume Next
                    temp = WorksheetFunction.Match(ColName, RowRange, 0)
                    On Error GoTo 0
                    'Row not found in row range list - delete it
                    If IsNumeric(temp) Then
                        .Cells(1, i).EntireRow.Delete
                    End If
                End If
            Next i 'Next column
       
        End With
   
    Next WS
   
   
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Code:
LastCol = .Cells(Rows.Count, 1).End(xlUp).Row
is not last column but last row
 
Upvote 0
You've got quite a bit of confusion and conflicting info in your code.

ASSUMING you really do want do delete ROWS (not columns) and assuming the item you're trying to match is in Col A on all 3 sheets, try this:
Code:
Sub delColumnNames()
 
Dim ColName As String
Dim LastRow As Long
Dim RowRange As Range
Dim RowWS As Worksheet
Dim lngLr As Long
Dim WS As Worksheet
Dim i As Long
Dim temp As Long
 
    'List of rows (to keep)
    Set RowWS = Sheets("Sheet1")
       
    'Finds # of last row in Col A of RowWS
    LastRow = RowWS.Range("A" & Rows.Count).End(xlUp).Row
    
    Set RowRange = RowWS.Range("A2:A" & LastRow)
   
    For Each WS In Sheets(Array("byEmployee", "byPosition"))
       
        With WS
           WS.Activate
           'Finds # of last row in Col A of worksheet in loop
            lngLr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
                        
            'Step through rows and delete those
            'where Col A name is not found in RowRange list
            'or Col a name is blank
'Stepping to row TWO since you say row 1 is header
            For i = lngLr To 2 Step -1
                
                'Get current row's Col A Value
                ColName = Range("A" & i).Value
                
                If ColName = "" Then
                    'Row is blank - delete it
                    Range("A" & i).EntireRow.Delete
                Else
                    On Error Resume Next
                    
                    'Counts if WS current row's Col A value
                    'is found in RowRange
                    temp = WorksheetFunction.CountIf(RowRange, ColName)
                    On Error GoTo 0
                    'Name not found - delete row
                    '(if not found count will be ZERO)
                    If temp > 0 Then
'Skips "delete" if count is >0                        
GoTo Skip
                        Else
                        Range("A" & i).EntireRow.Delete
                    End If
                End If
Skip:
            Next i
        End With
    Next WS
   
MsgBox "Done!"
   
End Sub
 
Upvote 0
it doesn't work if the strangers are on a1
so:
Code:
Sub delColumnNames()
 
Dim ColName As String
Dim LastRow As Long
Dim RowRange As Range
Dim RowWS As Worksheet
Dim lngLr As Long
Dim WS As Worksheet
Dim i As Long
Dim temp As Long
 
    'List of rows (to keep)
    Set RowWS = Sheets("Sheet1")
       
    'Finds # of last row in Col A of RowWS
    LastRow = RowWS.Range("A" & Rows.Count).End(xlUp).Row
    
    Set RowRange = RowWS.Range("A2:A" & LastRow)
   
    For Each WS In Sheets(Array("byEmployee", "byPosition"))
       
        With WS
           WS.Activate
           'Finds # of last row in Col A of worksheet in loop
            lngLr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
                        
            'Step through rows and delete those
            'where Col A name is not found in RowRange list
            'or Col a name is blank
'Stepping to row TWO since you say row 1 is header
            For i = lngLr To 1 Step -1 '''''''''''''''''''here
                
                'Get current row's Col A Value
                ColName = Range("A" & i).Value
                
                If ColName = "" Then
                    'Row is blank - delete it
                    Range("A" & i).EntireRow.Delete
                Else
                    On Error Resume Next
                    
                    'Counts if WS current row's Col A value
                    'is found in RowRange
                    temp = WorksheetFunction.CountIf(RowRange, ColName)
                    On Error GoTo 0
                    'Name not found - delete row
                    '(if not found count will be ZERO)
                    If temp > 0 Then
'Skips "delete" if count is >0
GoTo Skip
                        Else
                        Range("A" & i).EntireRow.Delete
                    End If
                End If
Skip:
            Next i
        End With
    Next WS
   
MsgBox "Well done!"
   
End Sub
tanks to rallcorn :)
 
Last edited:
Upvote 0
Gigi, not sure what is meant by this:
it doesn't work if the strangers are on a1

Row 1 is header row on all sheets, so:
the RowRange is defined as A2:A & LastRow
the loop of each sheet goes from lngLR (last row) to row 2
 
Upvote 0
you're right!!
but i saw the example file....and byEmployee byPosition have no header!!!
i repeat....GREAT WORK AND THANKS TO rallcorn:cool:
 
Upvote 0
but i saw the example file....and byEmployee byPosition have no header!!!

Then, you are correct in changing the code to
Code:
For i = lngLr To 1 Step -1
and you might also want to delete the comment
Code:
'Stepping to row TWO since you say row 1 is header
so it will make sense to anyone viewing the code in your file at a later date.

Glad you got it going and happy to help!
 
Upvote 0

Forum statistics

Threads
1,202,987
Messages
6,052,932
Members
444,616
Latest member
novit19089

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top