VBA Excel 2016在运行时引发对象必需错误

时间:2018-11-24 09:14:59

标签: excel vba excel-vba

我要做什么:

  1. 我有3个优点-Master.xlsx(我的源数据),DiscrepencyReport(我的宏将创建一个并填充来自Master.xlsx的计算值)和ReportRunner(我的活动工作表,我将在其中提供一些输入值并运行)宏)
  2. 我想以编程方式打开Master.xlsx并使用文件名中的日期范围创建一个名为DiscepencyReport的新文件。
  3. 从源中复制某些值,对其进行操作,然后将其粘贴到目标工作表中。
  4. 关闭所有工作簿。

我需要什么帮助:

  1. 首先,它给我一个运行时错误,提示“需要对象”。错误在此行“ InputFileName = This.Workbook.Path&“ \”&“ Master.xlsx” “。我用谷歌搜索并阅读了先前的错误,并尝试使用/删除set关键字,但它不起作用。我在这里缺少基本知识。编译时没有错误。
  2. 这是解决此问题的正确方法吗?还是有更好的方法?
  3. 有一部分是我在“构建报告表”。我想要有关如何将A列从称为Master.xlsx的“基础”的工作表复制到目标工作簿的sheet1的A1列的语法的指导。以下代码仅复制而不会粘贴。

我的代码如下:

Sub ReportGeneration()

'Declarations

Dim InputSheet As Workbook, AttendanceDiscrepencyReporter As Workbook
Dim Start As String
Dim Last As String
Dim InputFileName As String
Dim ADRFileName As String
Dim TWorkingDays As Integer
Dim EmpEmail As Range
Dim EmpID As Range
Dim TLeave As Range
Dim TFlexi As Range
Dim TAttendance As Range
Dim IsAtOnsite As Range
Dim CMEmail As Range

'Open Input Workbook

InputFileName = This.Workbook.Path & "\" & "Master.xlsx"
Set InputSheet = Workbooks.Open(InputFileName, True, True)

'Create the Report Workbook

Start = Replace((Range("F7").Value), "/", "_")
Last = Replace((Range("F8").Value), "/", "_")
TWorkingDays = Range("F10").Value

Set AttendanceDiscrepencyReporter = Workbooks.Add
ADRFileName = ThisWorkbook.Path & "\" & "DiscrepencyReport_from_" & Start & "_to_" & Last & ".xlsx"

With AttendanceDiscrepencyReporter
    .Title = "Discrepency Report"
    .Subject = "Discrepency Report"
    .SaveAs Filename:=ADRFileName
End With

' Construct the Report Worksheet

Set EmpEmail = InputSheet.Worksheets("Base").Columns("A")
Set EmpID = InputSheet.Worksheets("Base").Columns("B")

' Close Workbooks

InpuSheet.Close
AttendanceDiscrepencyReporter.Close

End Sub

2 个答案:

答案 0 :(得分:1)

  1. 明确使用选项-在此处阅读:https://www.excel-easy.com/vba/examples/option-explicit.html

  2. 使用Option Explicit将消除拼写错误问题(始终启用Option Explicit并使用它。)

  3. 如何在VBA中复制列:

    '范围。复制到其他工作簿     Workbooks(“ Book1.xlsx”)。Worksheets(“ Sheet1”)。Range(“ A1”)。Copy _         Workbooks(“ Book2.xlsx”)。Worksheets(“ Sheet1”)。Range(“ A1”)

在此处:How can I copy columns from one sheet to another with VBA in Excel?和此处:https://www.excelcampus.com/vba/copy-paste-cells-vba-macros/

了解更多信息

此处为纠正的鳕鱼:

Sub ReportGeneration()

'Declarations

Dim InputSheet As Workbook, AttendanceDiscrepencyReporter As Workbook
Dim Start As String
Dim Last As String
Dim InputFileName As String
Dim ADRFileName As String
Dim TWorkingDays As Integer
Dim EmpEmail As Range
Dim EmpID As Range
Dim TLeave As Range
Dim TFlexi As Range
Dim TAttendance As Range
Dim IsAtOnsite As Range
Dim CMEmail As Range

'Open Input Workbook

InputFileName = ThisWorkbook.Path & "\" & "Master.xlsx"
Set InputSheet = Workbooks.Open(InputFileName, True, True)

'Create the Report Workbook

Start = Replace((Range("F7").Value), "/", "_")
Last = Replace((Range("F8").Value), "/", "_")
TWorkingDays = Range("F10").Value

Set AttendanceDiscrepencyReporter = Workbooks.Add
ADRFileName = ThisWorkbook.Path & "\" & "DiscrepencyReport_from_" & Start & "_to_" & Last & ".xlsx"

With AttendanceDiscrepencyReporter
    .Title = "Discrepency Report"
    .Subject = "Discrepency Report"
    .SaveAs Filename:=ADRFileName
End With

' Construct the Report Worksheet

Set EmpEmail = InputSheet.Worksheets("Base").Columns("A")
Set EmpID = InputSheet.Worksheets("Base").Columns("B")

' Close Workbooks

InputSheet.Close
AttendanceDiscrepencyReporter.Close

End Sub

我已更正This.Workbook部分和InpuSheet.Close中的拼写错误。

答案 1 :(得分:1)

在每个模块的顶部不使用Option Explicit进行强制显式变量声明的情况下,VBA将在编译时为所有未声明的变量名称分配一个变体。 VBA不能将其解析为对象,方法或属性的任何代码都将成为变体类型变量,无论无法识别的代码字符串是什么。

  • UnrecognisedCodeString.VarType = vbEmpty一直适用,直到为其分配了对象或属性值

  • UnrecognisedCodeString.Value = vbNull

    如果您尝试对未分配的变量进行操作,则将无法正常工作:

  • InputFileName = This.Workbook.Path & "\" & "Master.xlsx"的计算结果

  • InputFileName = vbNull.Workbook.Path & "\" & "Master.xlsx",然后

  • InputFileName = vbNull 由于vbNull不是字符串,因此会导致错误。

好消息是,您可以通过以下方法将VBA IDE设置为始终要求Option Explicit:转到工具->选项...->编辑器,然后填写“要求变量声明”复选框。

enter image description here

不幸的是,它不能追溯以前创建的模块,而只能追溯到新模块。