Home > other >  VBA code not running in Excel but no debug appears
VBA code not running in Excel but no debug appears

Time:03-18

When I am trying to run the code below, it just does nothing (no break appears with debug, code looks correct).

Can you please help me ?

Sub test()

Dim jeans As Double

Sheets("UI").Activate
last_row = Cells(Rows.Count, "A").End(xlUp).Row

For i = 2 To last_row 

jeans = Cells(i, 8).Value

If Cells(i, 3).Value = "US" Then
    Select Case jeans
    Case jeans < 20
    Rows(i).Delete
    Case Else
    End Select

ElseIf Cells(i, 3).Value = "EU" Then
    Select Case jeans
    Case jeans < 10
    Rows(i).Delete
    Case Else
    End Select
    
ElseIf Cells(i, 3).Value = "ASIA" Then
    Select Case jeans
    Case jeans < 10
    Rows(i).Delete
    Case Else
    End Select

Else
    Rows(i).Delete
End If

Next i

End Sub

Thank you in advance !

Matt

CodePudding user response:

Loop Backwards With Conditions

  • You need to loop backward, from the bottom to the top to cover all cells.
  • When a code is doing nothing, check if there is data in the column where you're calculating the last row, that is column A in this case.
Option Explicit

Sub LoopBackward()

    With ThisWorkbook.Worksheets("UI")
        
        Dim LastRow As Long: LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        Dim Jeans As Double
        Dim r As Long
        
        For r = LastRow To 2 Step -1
            
            Jeans = .Cells(r, "H").Value
            
            Select Case .Cells(r, "C").Value
            Case "US"
                If Jeans < 20 Then .Rows(r).Delete
            Case "EU", "ASIA"
                If Jeans < 10 Then .Rows(r).Delete
            Case Else
                .Rows(r).Delete
            End Select
        
        Next r
    
    End With

End Sub
  • Related