我正在尝试使用单词VBA重命名Excel中复制的工作表。当我尝试将工作表变量设置为新工作表时,它将尝试打开Microsoft Outlook2016。此后(逐步执行)每一行都会打开一个对话框,要求我创建一个新的Microsoft Outlook帐户。
该代码确实成功创建了模板工作表的副本,但是无法重命名它们。
**编辑该代码是为了澄清实际问题并添加“ Set oWB = oXL.Workbooks.Open(FileName:= WorkbookToWorkOn)'Opens Excel'”(发生问题时该宏已存在于工作簿宏中)。
Dim oXL As Excel.Application 'Requires loading "Microsoft Excel 16.0 Object Library" from Tools -> References
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oXL = New Excel.Application
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn) 'Opens Excel
oXL.Visible = True
For i = LBound(seller_names) To UBound(seller_names)
With oXL.ActiveWorkbook
.Sheets("Template").Copy After:=.Sheets(.Sheets.Count)
Set oSheet = Sheets(.Sheets.Count) 'set worksheet to be the template copy
If Len(seller_names(i)) > 31 Then
oSheet.name = Left(seller_names(i), 31)
Else
oSheet.name = seller_names(i)
End If
End With
Next i
“ Set oSheet = Sheets(.Sheets.Count)行是打开的Outlook错误所在。这两行oSheet.name =运行都不会产生错误,但无法重命名工作表。
答案 0 :(得分:0)
我通过更改
解决了此问题.Sheets("Template").Copy After:=.Sheets(.Sheets.Count)
到
.Sheets("Template").Copy After:=.Sheets("Template (2)")