与VBA中的macrosvariables错误

我在Excel中是VBA新手,遇到了一个我不确定如何解释的错误。 我想知道有人可以帮忙。 我正在尝试为excel文件创build目录,并在网上find一个macros。 它张贴如下:

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. 

我对这个真的很陌生,我不确定这是什么意思。 有人可以提供一个详细的解释是什么问题,以及如何去解决它。

我相信问题是在引号中,有一些angular度/花括号被使用,这是停止正确读取代码。

以这个问题的第一个例子:

 .Worksheets(“TOC”).Delete 

您收到错误: –

编译错误:

variables未定义

“TOC”突出显示。 发生这种情况是因为错误的引号是因为TOC不被看作字面string,而是“TOC”被认为是一个variables,没有定义(即不存在)。

这可以通过检查你的代码并replace下面的所有内容来解决:

更改 (开口报价)为" (直接报价( Shift + 2

更改 (直接引用)至" (直接引用( shift + 2

改变' (斜angular撇号)为' (直单引号( ' ,共享@键)