Looping Two Variables..Getting an Error..Invalid Next Control Variable Reference

peee2

New Member
Joined
Jun 4, 2019
Messages
14
Hi All.. Im new to Macros..so getting an error..the macro has to basically check whether there exists a worksheet with a certain name (the name is in BF column) if it exists den do nothing. if does not exist den create a copy of an existing worksheet and rename it with name in BF column.Repeat this for all the rows in BF column.
what changes should be done in the below macro to work properly. pls help

Sub CopySheetAndRename()

For i = 1 To Worksheets.count
For r = 1 To Rows.count
If Worksheets(i).Name = Range("BF" & r + 1) Then
exists = True
End If
Next i


If Not exists Then

Dim ws As Worksheet
Set ws = ActiveSheet
Worksheets("CopySheet").Copy After:=Worksheets("Misc")
If ws.Range("BF" & r + 1).Value <> "" Then
On Error Resume Next
ActiveSheet.Name = ws.Range("BF" & r + 1).Value

End If
Next r
ws.Activate
End If

End Sub

Thanks in Advance
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
you need to loop through the rows before the sheets...
so, the first Next statement needs to be r !!
AND
It would help you if you got into the habit of declaring ALL of your variables
Code:
Sub CopySheetAndRename()
[color=red]dim r as long, i as long,ws as worksheet[/color]
For i = 1 To Worksheets.count
For r = 1 To Rows.count
If Worksheets(i).Name = Range("BF" & r + 1) Then
exists = True
End If
Next [color=red]r[/color]


If Not exists Then
Set ws = ActiveSheet
Worksheets("CopySheet").Copy After:=Worksheets("Misc")
If ws.Range("BF" & r + 1).Value <> "" Then
On Error Resume Next
ActiveSheet.Name = ws.Range("BF" & r + 1).Value

End If
Next [color=red]i[/color] 
ws.Activate
End If
End Sub
 
Last edited:
Upvote 0
Theres allsorts wrong there im afraid. What is the name of the sheet where the list of sheet names appears?
 
Upvote 0
Thanks Micheal for the quick response. Tried ur macro. now getting a different error mssg..Compile error: Next without For.
 
Upvote 0
Id recommend using the name of the sheet as it alters the action of the macro if you happen to run it when the 'wrong' sheet is active. That said this will work if you have the sheet with the list of names active:

Code:
Function SheetExists(sh As String) As Boolean

Dim mySheet As Worksheet

On Error Resume Next
Set mySheet = Worksheets(sh)

If Not mySheet Is Nothing Then
    SheetExists = True
Else
    SheetExists = False
End If

End Function

Sub CopySheetAndRename()

Dim lr As Long, i As Long

With ActiveSheet
    lr = .Range("BF" & .Rows.Count).End(xlUp).Row
    For i = 2 To lr
        If Not SheetExists(.Range("BF" & i).Value) Then
            Worksheets("CopySheet").Copy After:=Worksheets("Misc")
            ActiveSheet.Name = .Range("BF" & i).Value
        End If
    Next
End With

End Sub
 
Upvote 0
Great. Did what you said and it worked perfectly. Many a thanks Steve and Best wishes. Grateful to you & Mr Excel.
 
Upvote 0

Forum statistics

Threads
1,214,518
Messages
6,119,985
Members
448,935
Latest member
ijat

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