Ich bin neu in VBA in Excel und ich lief in einen Fehler, ich bin mir nicht sicher, wie zu interpretieren. Ich habe mich gefragt, ob jemand helfen könnte. Ich versuche, ein Inhaltsverzeichnis für eine Excel-Datei zu erstellen, und ich habe ein Makro online gefunden. Es wird unten geschrieben:Variable Fehler mit Makro in VBA
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
Der Fehler, die ich erhalten war:
Compile Error: Variable TOS not defined.
Ich bin wirklich neu in diesem und ich bin nicht sicher, was das bedeutet. Könnte jemand eine detaillierte Erklärung geben, was das Problem ist und wie man es beheben kann?
Haben Sie an der Spitze des Codes "Option Explicit" enthalten? Dies zwingt Sie, alle Ihre Variablen zu definieren. Dies könnte helfen, das Problem zu finden! – MiguelH
Das ist eigentlich in der Datei bereits enthalten ist es die TOS markieren –
Wo ist TOS in Ihrem Code? –