For Next loop - why do I get an error?

Damian86

New Member
Joined
Jun 30, 2014
Messages
47
Hi guys,

I want to create a macro that will loop through a range and clear a row if the value in the coulm A matches a value in another sheet. Then I want the macro to pick another value from another sheet and do the loop again. And again and again through the same rows but changing the checked value. My code is:

Code:
Sub UsunWiersze()

Dim Var1 As Range
Dim NumerWierszaDekret As Integer
Dim NumerWierszaBaza As Integer
Dim WIersz As Range
Dim VAB As Range

NumerWierszaDekret = 1

Set Var1 = ActiveWorkbook.Worksheets("Dekrety").Range("A" & NumerWierszaDekret)

ActiveWorkbook.Worksheets("Baza").Range("A1").Activate

For NumerWierszaDekret = 1 To 373
    Set Var1 = ActiveWorkbook.Worksheets("Dekrety").Range("A" & NumerWierszaDekret)
        For Each VAB In Range("A1:A27459")
        If VAB.Value = Var1 Then
        VAB.EntireRow.Clear
        Next VAB
Next NumerWierszaDekret

End Sub

I get and "Next withour For" Error error and I don't know why. I have 2x For and 2x Next. Why isn't it working?

Greetings!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You are missing an End If in your For Next Loop

Rich (BB code):
    If VAB.Value = Var1 Then
        VAB.EntireRow.Clear
    End If

or you could write it this way:

Rich (BB code):
If VAB.Value = Var1 Then VAB.EntireRow.Clear

Dave
 
Upvote 0
Thanks for you reply. I managed to geit it working with:

Code:
Sub UsunWiersze()

Dim Var1 As Range
Dim NumerWierszaDekret As Integer
Dim NumerWierszaBaza As Integer
Dim WIersz As Range
Dim VAB As Range

NumerWierszaDekret = 1

Set Var1 = ActiveWorkbook.Worksheets("Wartosci").Range("A" & NumerWierszaDekret)

ActiveWorkbook.Worksheets("Baza").Range("C1").Activate

For NumerWierszaDekret = 1 To 5
    Set Var1 = ActiveWorkbook.Worksheets("Dekrety").Range("A" & NumerWierszaDekret)
        For Each VAB In Range("A1:A2001")
        If VAB.Value = Var1 Then
        VAB.EntireRow.Clear
        End If
        Next VAB
Next NumerWierszaDekret

End Sub

And again I forgot about End If...

Code:
If VAB.Value = Var1 Then VAB.EntireRow.Clear</pre>
Thanks for that code, I'll have to check it.
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,291
Members
449,498
Latest member
Lee_ray

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