yerromnitsuj
New Member
- Joined
- Sep 12, 2011
- Messages
- 32
I'm still fairly new to VBA and I am trying to figure out some code that someone else wrote. Here it is:
Sub Make_State_sheets()
Dim New_sheet As Worksheet
Set New_sheet = ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count))
New_sheet.Name = "X2"
For i = 1 To Worksheets(SheetA).Range("B3").Value
Sheets("X").Select
Sheets("X").Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Worksheets(SheetB).Cells(i + 1, 1)
Next i
Application.DisplayAlerts = False
Sheets("X").Delete
Application.DisplayAlerts = True
End Sub
I'm not positive about what this code does, but my best guess is that it's supposed to create new worksheets called X, X2, X3, X4, etc. up to i, which is the value in cell B3 on SheetA. Then again, if that's the case, I'm not sure how the DisplayAlerts part at the bottom is supposed to come into play.
I have a bunch of worksheets, and the last one is named "X". When I run the macro, it creates a new worksheet entitled "X2", then tries to create a new worksheet and I get the error "Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic."
What is your best guess as to what this macro is supposed to be doing? Why might I be getting the error I'm getting? Thanks for the help!
Sub Make_State_sheets()
Dim New_sheet As Worksheet
Set New_sheet = ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count))
New_sheet.Name = "X2"
For i = 1 To Worksheets(SheetA).Range("B3").Value
Sheets("X").Select
Sheets("X").Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Worksheets(SheetB).Cells(i + 1, 1)
Next i
Application.DisplayAlerts = False
Sheets("X").Delete
Application.DisplayAlerts = True
End Sub
I'm not positive about what this code does, but my best guess is that it's supposed to create new worksheets called X, X2, X3, X4, etc. up to i, which is the value in cell B3 on SheetA. Then again, if that's the case, I'm not sure how the DisplayAlerts part at the bottom is supposed to come into play.
I have a bunch of worksheets, and the last one is named "X". When I run the macro, it creates a new worksheet entitled "X2", then tries to create a new worksheet and I get the error "Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic."
What is your best guess as to what this macro is supposed to be doing? Why might I be getting the error I'm getting? Thanks for the help!