VBA Excel 文件未编译 : Sub or Function not defined

标签 vba excel compiler-errors

以下是相关代码:
它说错误在函数 operation() 的定义中
我觉得我需要包括一些东西,但我不确定是什么。
有人可以建议我需要做什么吗?
我知道去工具>引用>..但之后我不确定。

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

最佳答案

你的变量名有错别字。你有:

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

但应该是:
lastRow = Range("B" & Rows.Count).End(xlUp).Row

如果你把
Option Explicit

在模块的顶部,您会收到一条更有用的错误消息。你真的应该养成包含它的习惯,因为它会检查以确保你的所有变量都被声明了。事实上,它非常有用,您可以在默认情况下在模块创建时设置它。在 VBA IDE 中,转到工具 => 选项 => 勾选标有“需要变量声明”的框。

关于VBA Excel 文件未编译 : Sub or Function not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31058685/

相关文章:

excel - 复制和粘贴的宏扩展不起作用

.net - 有哪些基于 Open XML SDK 构建的框架?

Excel VBA : can't paste my function into a cell

c++ - 运算符的简单重载 = 不起作用

c++ - 无法将字符串流传递给构造函数

android-ndk - 编译tesseract时出错

excel - 从重复范围中获取某些值并将它们粘贴为一行

vba - excel vba 打开工作簿

Excel 2010 - 条件格式、同一行中相邻单元格的颜色

vba - 如何将图片添加到Powerpoint演示文稿图片占位符?