我要做什么:
我需要什么帮助:
我的代码如下:
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
答案 0 :(得分:1)
明确使用选项-在此处阅读:https://www.excel-easy.com/vba/examples/option-explicit.html
使用Option Explicit将消除拼写错误问题(始终启用Option Explicit并使用它。)
如何在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
:转到工具->选项...->编辑器,然后填写“要求变量声明”复选框。
不幸的是,它不能追溯以前创建的模块,而只能追溯到新模块。