首页 > 解决方案 > “您为工作表或图表输入了无效名称”,需要更新代码

问题描述

当我的“复制数据透视表”代码创建数据透视表时,我有一个带有“/”的品牌。一旦发现它调试并停止创建数据透视表,excel 中的选项卡就不能用“/”创建/命名。是否有我可以包含的代码告诉它当它看到“RE/DONE”时将名称更改为“RE DONE”,这样它就可以在不调试的情况下创建数据透视表。我的代码如下。


On Error GoTo errHandler
Application.DisplayAlerts = False

Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim strPF As String
Dim strPI As String

Set ws = Sheets("Invoice PVTTBL Template")
Set pt = ws.PivotTables(1)
Set pf = pt.PageFields(1)
strPF = pf.Name

For Each pi In pf.PivotItems
  strPI = Left(pi.Name, 31)
  On Error Resume Next
    Sheets(strPI).Delete
  On Error GoTo 0
  ws.Copy After:=Sheets(Sheets.Count)
  With ActiveSheet
    .Name = strPI
    With .PivotTables(1).PivotFields(strPF)
      .PivotItems(pi.Name).Visible = True
      .CurrentPage = pi.Name
    End With
  End With

Next pi

ws.Activate

MsgBox "Invoices Created"

exitHandler:
  Application.DisplayAlerts = True
  Exit Sub
errHandler:
  MsgBox "Could not create sheets"
  Resume exitHandler

End Sub

标签: excelvbapivot-table

解决方案


推荐阅读