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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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