我想创建一个宏,它将根据cel的值生成N个模板表。例如,用户在此特定单元格中输入4,然后在此模板的工作簿中生成4个新工作表。
我已经在Stack溢出搜索了一个与我匹配的问题,但没有一个问题。我发现的最接近的是this,尽管初始标题问题通常都是同一个问题,但在详细说明请求此用户的用户将其问题更改为“根据单元格值插入单元格数”时。我仍然以此为出发点。
Sub CreateSheets()
Dim facilitiesNum As Integer
facilitiesNum = Range("B2").Value
sheetsNeeded = facilitiesNum
With ThisWorkbook.Sheets
For i = sheetsNeeded To Master.Range("B2").Value2
.Item("TemplateSheet").Copy After:=.Item(.Count)
.Item(.Count).Name = sheetsNeeded
Next
End With
End Sub
我是VBA的新手,所以我可能非常违反语法,但在伪代码中,我的目标是
numberOfTemplates = cell value
numSheetsNeeded = numberOfTemplates
For i = numSheetsNeeded To NumOfTemp:
create sheets using numSheetsNeeded as reference for how many need to be
generated
我该怎么做呢?
答案 0 :(得分:2)
如果你只是想添加新表,那就足够了
Sub CreateSheets()
Dim facilitiesNum As Long
facilitiesNum = Range("B2").Value
With ThisWorkbook.Sheets
For i = 1 To facilitiesNum
.Item("TemplateSheet").Copy After:=.Item(.Count)
.Item(.Count).Name = i
Next i
End With
end sub