我有一个我要创建的83个工作簿的未来名称列表。 该列表位于工作簿“casenames”的第一列。
这是我正在使用的代码,但它给了我一个错误。
Sub createworkbook()
Dim casenames As Workbook
Dim ptnames(82) As String
Const FPath As String = ""
Set casenames = Workbooks.Open("")
``For i = 1 To 83
Workbooks.Add (FPath & casenames.Sheets("Sheet1").Cells(i, 1).Text & ".xlsx")
Next i
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
End Sub``
请帮忙。
答案 0 :(得分:2)
您应该将名称读入数组,然后遍历数组。
此外,您无法添加具有名称的新工作簿,您必须添加工作簿,然后使用您想要的名称保存它,然后将其关闭。
试试这个:
Option Explicit
Sub createworkbook()
Dim casenames As Workbook
Dim ptnames As Variant
Const FPath As String = "C:\temp\"
'Disable the calculation and screenupdating
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
'Assume the casenames workbook is already open
Set casenames = Workbooks("casenames.xlsx")
ptnames = casenames.Worksheets(1).Range("A1:A83").Value
Dim wbk As Workbook
Dim i As Long
For i = LBound(ptnames, 1) To UBound(ptnames, 1)
Set wbk = Workbooks.Add
wbk.SaveAs FPath & ptnames(i, 1) & ".xlsx"
wbk.Close
Next i
'Reinstate the calculation and screenupdating
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub