简体   繁体   English

VBA Excel文件未编译:子函数未定义

[英]VBA Excel File not compiling: Sub or Function not defined

Here is the associated code: It says the error is in the definition of the function operation() I feel like I need to include something, but I am not sure what. 这是相关的代码:它说错误是在函数operation()的定义中,我感觉我需要包括一些东西,但是我不确定是什么。 Can anybody please advise on what I need to do? 有人可以建议我需要做什么吗? I know to go to Tools > References > .. but after that I am not sure. 我知道要转到“工具”>“引用”>“ ..”,但是此后我不确定。

Sub operation()
'
' Macro5 Macro
'

'
Dim Erw, firstRow, lastRow
firstRow = 1
Last Row = Range("B" & Rows.Count).End(xlUp).Row
For Erw = firstRow To lastRow
    Dim newRow
    newRow = firstRow + 4
    Range("B" & newRow).Select
    ActiveCell.FormulaR1C1 = Range("B" & newRow)
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;ActiveCell.FormulaR1C1", _
        Destination:=Range("$D$5"))
        .Name = "collections1504_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    nextRow = nextRow + 1
    Next Erw

    Range("D3").Select
    Selection.Copy
    Range("C5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("D5:P143").Select
    Application.CutCopyMode = False
    Selection.QueryTable.Delete
    Selection.ClearContents
End Sub

You've got a typo in your variable names. 您的变量名中有一个错字。 You have: 你有:

Last Row = Range("B" & Rows.Count).End(xlUp).Row

but it should be: 但应该是:

lastRow = Range("B" & Rows.Count).End(xlUp).Row

If you put 如果你把

Option Explicit

at the top of your module, you get a slightly more helpful error message. 在模块顶部,您会得到一条稍微有用的错误消息。 You really should get in to the habit of including that as it will check to make sure all of your variables are declared. 您确实应该养成包括它的习惯,因为它将检查以确保所有变量都已声明。 It's so helpful in fact, that you can have it set at module creation by default. 实际上,它非常有用,您可以在默认情况下在模块创建时对其进行设置。 In the VBA IDE go to Tools => Options => Tick the box labeled "Require Variable Declaration". 在VBA IDE中,转到“工具=>选项=>”,然后选中标记为“需要变量声明”的框。

As the compiler tells you (and halts on the line), the function Last is not defined: there is a space too many: 正如编译器告诉您的(并暂停运行),未定义函数Last :空间太多:

Last Row 

should be 应该

lastRow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM