Regarding my last query:
VBA Excel autopopulate new sheets based on the cell value for incrementing cells
I would like to have this sheet population in the correct order, as I have more than 1 sheet name to populate.
I have 2 sheets with the basic reference of the name. They are marked red in the image below:
After applying the code below:
Sub Sheetwithnames2()
Dim wsr As Worksheet, wso As Worksheet
Dim i As Long, j As Long, xCount As Long, yCount As Long
Dim SheetNames As Variant, LSheetNames As Variant 'This needs to be variant
Dim sheetname As Variant, lsheetname As Variant
Dim newsheet As Worksheet, lnewsheet As Worksheet, onewsheet As Worksheet, olnewsheet As Worksheet
Dim lr As Long, lro As Long
Set wsr = ThisWorkbook.Sheets("Vetro Area Map 1")
Set wso = ThisWorkbook.Sheets("Area Map Op 1")
lr = ThisWorkbook.Sheets("Frontsheet").Cells(Rows.Count, 4).End(xlUp).Row 'Get last row
lro = ThisWorkbook.Sheets("Frontsheet").Cells(Rows.Count, 5).End(xlUp).Row 'Get last row
'including empty cells either, but not creating new sheets for them
SheetNames = ThisWorkbook.Sheets("Frontsheet").Range("D123:D" & lr)
SheetNames = Application.Transpose(Application.Index(SheetNames, , 1)) 'Converts the 2d array into a 1d array
LSheetNames = ThisWorkbook.Sheets("Frontsheet").Range("E123:E" & lro)
For i = 1 To ActiveWorkbook.Sheets.Count
If InStr(1, Sheets(i).name, "Vetro") > 0 Then xCount = xCount + 1
Next
For j = 1 To ActiveWorkbook.Sheets.Count
If InStr(1, Sheets(j).name, "Op") > 0 Then yCount = yCount + 1
Next
For Each sheetname In SheetNames
wsr.Copy After:=ActiveWorkbook.Sheets(wsr.Index - 1 + xCount)
Set newsheet = Sheets(wsr.Index + xCount)
newsheet.name = "Vetro Area Map " & sheetname & " 1"
xCount = xCount + 1 'Preserve order of sheets from range
wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + yCount)
Set onewsheet = Sheets(wso.Index + yCount)
onewsheet.name = "Area Map Op " & sheetname & " 1"
yCount = yCount + 1
Next
End Sub
I am getting the wrong order of these sheet names. I need them to appear interchangeably, as you could see below:
I tried also something like this:
For i = 1 To ActiveWorkbook.Sheets.Count
If InStr(1, Sheets(i).name, "Vetro") > 0 Then xCount = xCount + 1
Next
For j = 1 To ActiveWorkbook.Sheets.Count
If InStr(1, Sheets(j).name, "Op") > 0 Then yCount = xCount + 2
Next
For Each sheetname In SheetNames
wsr.Copy After:=ActiveWorkbook.Sheets(wsr.Index - 1 + xCount)
Set newsheet = Sheets(wsr.Index + xCount)
newsheet.name = "Vetro Area Map " & sheetname & " 1"
xCount = xCount + 1 'Preserve order of sheets from range
wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + yCount)
Set onewsheet = Sheets(wso.Index + yCount)
onewsheet.name = "Area Map Op " & sheetname & " 1"
yCount = yCount + 1
Next
but I am getting an error:
Subscript out of range
wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + yCount)
Te same error occurs with this approach:
For i = 1 To ActiveWorkbook.Sheets.Count
If InStr(1, Sheets(i).name, "Vetro") > 0 Then xCount = xCount + 1
Next
For Each sheetname In SheetNames
wsr.Copy After:=ActiveWorkbook.Sheets(wsr.Index - 1 + xCount * 2 + 1)
Set newsheet = Sheets(wsr.Index + xCount)
newsheet.name = "Vetro Area Map " & sheetname & " 1"
xCount = xCount + 1 'Preserve order of sheets from range
wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + xCount * 2 + 2)
Set onewsheet = Sheets(wso.Index + xCount)
onewsheet.name = "Area Map Op " & sheetname & " 1"
xCount = xCount + 1
Next
SheetNames
, copy the existing sheets 'Vetro Area Map 1' and 'Area Map Op 1' after ......" - Tim Williams