【发布时间】:2021-03-18 15:44:51
【问题描述】:
关于我上次的查询:
VBA Excel autopopulate new sheets based on the cell value for incrementing cells
我希望以正确的顺序填充此工作表,因为我有超过 1 个工作表名称要填充。
我有 2 张纸,上面有名字的基本参考。它们在下图中被标记为红色:
应用以下代码后:
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
我得到这些工作表名称的错误顺序。我需要它们交替出现,如下所示:
我也试过这样的:
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
但我收到一个错误:
下标超出范围
wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + yCount)
这种方法也会出现同样的错误:
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范围内的每个工作表名称,在......之后复制现有工作表'Vetro Area Map 1'和'Area Map Op 1'” -
这些名称可能是机密的。这就是为什么我决定改为更改标签颜色的原因。我希望 Vetro Map 1 和 Op Map 1 下一个 Vetro Map 0085a 和 Op Map 0085a、Vetro Map 0085b、Op Map 0085b 等交替出现。