Compile error: Next without for while For is there

Saab95

New Member
Joined
Mar 26, 2021
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi there,

Need your help again, I have the below that works as long as my lastRow count is greater than 2.

So I wanted to add the condition in red. " If lastRow > 2 Then".

But now I have an error "Compile error: Next without for" . Why that ? I have a For. Thanks
Rich (BB code):
Sub Sheet_Date2()
'take3'
Dim lstRow As Long
Dim ws As Worksheet
   For Each ws In ThisWorkbook.Worksheets
   If ws.Name <> "Master" And ws.Name <> "Summary" And ws.Name <> "Shaping" Then
    lastRow = ws.Range("AR" & Rows.Count).End(xlUp).Row
    If lastRow > 2 Then
    ws.Range("AT2").AutoFill Destination:=ws.Range("AT2:AT" & lastRow)
    End If
    Next ws
End Sub
 
Last edited by a moderator:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You have two If...Then clauses but only one End If.
 
Upvote 0
So I wanted to add the condition in red. " If lastRow > 2 Then".
Just a hint on forum use. If you want to highlight part of code like that, you need to use the 'RICH' tags, not 'VBA' tags.
I have fixed this for in your post above.

1621934867750.png
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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