我是excel的VBA新手,我遇到了一个错误,我不确定如何解释。我想知道是否有人可以提供帮助。我正在尝试为excel文件创建s目录,我在网上找到了一个宏。它发布在下面:
Sub Create_TOC()
Dim wbBook As Workbook
Dim wsActive As Worksheet
Dim wsSheet As Worksheet
Dim lnRow As Long
Dim lnPages As Long
Dim lnCount As Long
Set wbBook = ActiveWorkbook
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
'If the TOC sheet already exist delete it and add a new
'worksheet.
On Error Resume Next
With wbBook
.Worksheets(“TOC”).Delete
.Worksheets.Add Before:=.Worksheets(1)
End With
On Error GoTo 0
Set wsActive = wbBook.ActiveSheet
With wsActive
.Name = “TOC”
With .Range(“A1:B1”)
.Value = VBA.Array(“Table of Contents”, “Sheet # – # of Pages”)
.Font.Bold = True
End With
End With
lnRow = 2
lnCount = 1
‘Iterate through the worksheets in the workbook and create
‘sheetnames, add hyperlink and count & write the running number
‘of pages to be printed for each sheet on the TOC sheet.
For Each wsSheet In wbBook.Worksheets
If wsSheet.Name <> wsActive.Name Then
wsSheet.Activate
With wsActive
.Hyperlinks.Add .Cells(lnRow, 1), “”, _
SubAddress:=”‘” & wsSheet.Name & “‘!A1”, _
TextToDisplay:=wsSheet.Name
lnPages = wsSheet.PageSetup.Pages().Count
.Cells(lnRow, 2).Value = “‘” & lnCount & “ - ” & lnPages
End With
lnRow = lnRow + 1
lnCount = lnCount + 1
End If
Next wsSheet
wsActive.Activate
wsActive.Columns(“A:B”).EntireColumn.AutoFit
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
我收到的错误是:
Compile Error: Variable TOS not defined.
我真的很陌生,我不确定这意味着什么。有人可以提供有关问题的详细解释以及如何解决问题。
答案 0 :(得分:1)
我认为问题在于引号,使用了斜角/花括号,这样就可以停止正确读取代码。
问题的第一个例子: -
.Worksheets(“TOC”).Delete
您收到错误: -
编译错误:
未定义变量
和“TOC”
突出显示。之所以发生这种情况,是因为错误的引号是TOC
不被视为文字字符串,而是“TOC”
被假定为变量,未定义(即不存在)。
这可以通过查看代码并替换以下所有内容来修复: -
将“
(带角度的公开引语)更改为"
(直引( shift + 2 )
将”
(有角度的近引号)更改为"
(直引( shift + 2 )
将‘
(有角度的撇号)更改为'
(单引号(',共享 @ 键)