半新的VBA和我的代码抛出运行时错误424对象需要。我意识到这不是最有效的代码,但只要有效,这对我来说并不重要。不幸的是它没有用。我希望这个错误给了我调试的选项,看看它导致了什么行,但是哦。
我的代码基本上是打开7个工作簿,文件名中的日期是过去七天,然后将数据从每个工作簿复制到单个工作簿。我只是试图节省一个同事的一大部分时间,并学习更多关于vba的知识。我的代码列在下面。
Sub DailyReportCopyPaste()
Dim MonDateYear As String
Dim MonDateMonth As String
Dim MonDateDay As String
Dim SunDateYear As String
Dim SunDateMonth As String
Dim SunDateDay As String
Dim SatDateYear As String
Dim SatDateMonth As String
Dim SatDateDay As String
Dim FriDateYear As String
Dim FriDateMonth As String
Dim FriDateDay As String
Dim ThuDateYear As String
Dim ThuDateMonth As String
Dim ThuDateDay As String
Dim WedDateYear As String
Dim WedDateMonth As String
Dim WedDateDay As String
Dim TueDateYear As String
Dim TueDateMonth As String
Dim TueDateDay As String
Dim MonDateFull As String
Dim SunDateFull As String
Dim SatDateFull As String
Dim FriDateFull As String
Dim ThuDateFull As String
Dim WedDateFull As String
Dim TueDateFull As String
Dim FilePathText As String
Dim CheckNumRows As Integer
Dim MainWorkbook As Workbook
Dim rowcount As String
MonDate = Date
SunDate = Date - 1
SatDate = Date - 2
FriDate = Date - 3
ThuDate = Date - 4
WedDate = Date - 5
TueDate = Date - 6
MonDateYear = DatePart("yyyy", MonDate)
MonDateMonth = Format(DatePart("m", MonDate), "00")
MonDateDay = Format(DatePart("d", MonDate), "00")
MonDateFull = MonDateYear & MonDateMonth & MonDateDay
SunDateYear = DatePart("yyyy", SunDate)
SunDateMonth = Format(DatePart("m", SunDate), "00")
SunDateDay = Format(DatePart("d", SunDate), "00")
SunDateFull = SunDateYear & SunDateMonth & SunDateDay
SatDateYear = DatePart("yyyy", SatDate)
SatDateMonth = Format(DatePart("m", SatDate), "00")
SatDateDay = Format(DatePart("d", SatDate), "00")
SatDateFull = SatDateYear & SatDateMonth & SatDateDay
FriDateYear = DatePart("yyyy", FriDate)
FriDateMonth = Format(DatePart("m", FriDate), "00")
FriDateDay = Format(DatePart("d", FriDate), "00")
FriDateFull = FriDateYear & FriDateMonth & FriDateDay
ThuDateYear = DatePart("yyyy", ThuDate)
ThuDateMonth = Format(DatePart("m", ThuDate), "00")
ThuDateDay = Format(DatePart("d", ThuDate), "00")
ThuDateFull = ThuDateYear & ThuDateMonth & ThuDateDay
WedDateYear = DatePart("yyyy", WedDate)
WedDateMonth = Format(DatePart("m", WedDate), "00")
WedDateDay = Format(DatePart("d", WedDate), "00")
WedDateFull = WedDateYear & WedDateMonth & WedDateDay
TueDateYear = DatePart("yyyy", TueDate)
TueDateMonth = Format(DatePart("m", TueDate), "00")
TueDateDay = Format(DatePart("d", TueDate), "00")
TueDateFull = TueDateYear & TueDateMonth & TueDateDay
FilePathText = "The beggining of the file path"
CheckNumRows = 0
With Workbooks.Open(FilePathText & MonDateFull & ".csv")
CheckNumRows = Cells(Row.Count, 1).End(xlUp).Row
Rows("2:" & CheckNumRows).Select
Selection.Copy
End With
With Workbooks.Open("File path for the sheet I'm pasting into")
Set MainWorkbook = ActiveWorkbook
Range("A1").Select
Selection.Paste
.Close
End With
CheckNumRows = 0
With Workbooks.Open(FilePathText & SunDateFull & ".csv")
CheckNumRows = Cells(Row.Count, 1).End(xlUp).Row
Rows("2:" & CheckNumRows).Select
Selection.Copy
End With
With MainWorkbook
Range("a1").End(xlDown).Offset(1, 0).Select
Selection.Paste
End With
CheckNumRows = 0
With Workbooks.Open(FilePathText & SatDateFull & ".csv")
CheckNumRows = Cells(Row.Count, 1).End(xlUp).Row
Rows("2:" & CheckNumRows).Select
Selection.Copy
.Close
End With
With MainWorkbook
Range("a1").End(xlDown).Offset(1, 0).Select
Selection.Paste
End With
CheckNumRows = 0
With Workbooks.Open(FilePathText & FriDateFull & ".csv")
CheckNumRows = Cells(Row.Count, 1).End(xlUp).Row
Rows("2:" & CheckNumRows).Select
Selection.Copy
.Close
End With
With MainWorkbook
Range("a1").End(xlDown).Offset(1, 0).Select
Selection.Paste
End With
CheckNumRows = 0
With Workbooks.Open(FilePathText & ThuDateFull & ".csv")
CheckNumRows = Cells(Row.Count, 1).End(xlUp).Row
Rows("2:" & CheckNumRows).Select
Selection.Copy
.Close
End With
With MainWorkbook
Range("a1").End(xlDown).Offset(1, 0).Select
Selection.Paste
End With
CheckNumRows = 0
With Workbooks.Open(FilePathText & WedDateFull & ".csv")
CheckNumRows = Cells(Row.Count, 1).End(xlUp).Row
Rows("2:" & CheckNumRows).Select
Selection.Copy
.Close
End With
With MainWorkbook
Range("a1").End(xlDown).Offset(1, 0).Select
Selection.Paste
End With
CheckNumRows = 0
With Workbooks.Open(FilePathText & TueDateFull & ".csv")
CheckNumRows = Cells(Row.Count, 1).End(xlUp).Row
Rows("2:" & CheckNumRows).Select
Selection.Copy
.Close
End With
With MainWorkbook
Range("a1").End(xlDown).Offset(1, 0).Select
Selection.Paste
End With
End Sub
我已经尝试删除所有Dim MonDayYear As String等,以确定它是否是一个字符串而不是一个日期的事实导致它,但它似乎没有帮助。除非它不能作为未声明的变量发挥作用......所有这一切都是为了将日期转换为文件扩展名的正确格式。文件扩展名看起来像" sometext-20161031.csv"而且我不知道如何在YYYYMMDD中显示日期功能。
任何有关处理424错误,日期格式问题或您可能看到的任何其他内容的帮助都非常感谢!对不起,这是一团糟,我还在学习。
答案 0 :(得分:0)
感谢您的帮助!我能够让这个工作。以下代码适用于任何感兴趣的人。
Sub DailyReportCopyPaste()
Dim MonDateYear As String
Dim MonDateMonth As String
Dim MonDateDay As String
Dim SunDateYear As String
Dim SunDateMonth As String
Dim SunDateDay As String
Dim SatDateYear As String
Dim SatDateMonth As String
Dim SatDateDay As String
Dim FriDateYear As String
Dim FriDateMonth As String
Dim FriDateDay As String
Dim ThuDateYear As String
Dim ThuDateMonth As String
Dim ThuDateDay As String
Dim WedDateYear As String
Dim WedDateMonth As String
Dim WedDateDay As String
Dim TueDateYear As String
Dim TueDateMonth As String
Dim TueDateDay As String
Dim MonDateFull As String
Dim SunDateFull As String
Dim SatDateFull As String
Dim FriDateFull As String
Dim ThuDateFull As String
Dim WedDateFull As String
Dim TueDateFull As String
Dim FilePathText As String
Dim CheckNumRows As Integer
Dim MainWorkbook As Workbook
Dim rowcount As String
Dim rLastRow As Range
Dim r2LastRow As Range
Dim MondayWorkbook As Workbook
Dim SundayWorkBook As Workbook
Dim SaturdayWorkbook As Workbook
Dim FridayWorkbook As Workbook
Dim ThursdayWorkbook As Workbook
Dim WednesdayWorkbook As Workbook
Dim TuesdayWorkbook As Workbook
MonDate = Date
SunDate = Date - 1
SatDate = Date - 2
FriDate = Date - 3
ThuDate = Date - 4
WedDate = Date - 5
TueDate = Date - 6
MonDateYear = DatePart("yyyy", MonDate)
MonDateMonth = Format(DatePart("m", MonDate), "00")
MonDateDay = Format(DatePart("d", MonDate), "00")
MonDateFull = MonDateYear & MonDateMonth & MonDateDay
SunDateYear = DatePart("yyyy", SunDate)
SunDateMonth = Format(DatePart("m", SunDate), "00")
SunDateDay = Format(DatePart("d", SunDate), "00")
SunDateFull = SunDateYear & SunDateMonth & SunDateDay
SatDateYear = DatePart("yyyy", SatDate)
SatDateMonth = Format(DatePart("m", SatDate), "00")
SatDateDay = Format(DatePart("d", SatDate), "00")
SatDateFull = SatDateYear & SatDateMonth & SatDateDay
FriDateYear = DatePart("yyyy", FriDate)
FriDateMonth = Format(DatePart("m", FriDate), "00")
FriDateDay = Format(DatePart("d", FriDate), "00")
FriDateFull = FriDateYear & FriDateMonth & FriDateDay
ThuDateYear = DatePart("yyyy", ThuDate)
ThuDateMonth = Format(DatePart("m", ThuDate), "00")
ThuDateDay = Format(DatePart("d", ThuDate), "00")
ThuDateFull = ThuDateYear & ThuDateMonth & ThuDateDay
WedDateYear = DatePart("yyyy", WedDate)
WedDateMonth = Format(DatePart("m", WedDate), "00")
WedDateDay = Format(DatePart("d", WedDate), "00")
WedDateFull = WedDateYear & WedDateMonth & WedDateDay
TueDateYear = DatePart("yyyy", TueDate)
TueDateMonth = Format(DatePart("m", TueDate), "00")
TueDateDay = Format(DatePart("d", TueDate), "00")
TueDateFull = TueDateYear & TueDateMonth & TueDateDay
FilePathText = "beggining of my file path"
Set MainWorkbook = ActiveWorkbook
Sheets(1).Cells.Clear
Set MondayWorkbook = Workbooks.Open(FilePathText & MonDateFull & ".csv")
CheckNumRows = 0
With MondayWorkbook.Sheets(1)
CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row
.Rows("2:" & CheckNumRows).Copy
End With
MainWorkbook.Activate
MainWorkbook.Sheets(1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
MondayWorkbook.Close
Set SundayWorkBook = Workbooks.Open(FilePathText & SunDateFull & ".csv")
CheckNumRows = 0
With SundayWorkBook.Sheets(1)
CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row
.Rows("2:" & CheckNumRows).Copy
End With
MainWorkbook.Activate
MainWorkbook.Sheets(1).Range("a1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
SundayWorkBook.Close
Set SaturdayWorkbook = Workbooks.Open(FilePathText & SatDateFull & ".csv")
CheckNumRows = 0
With SaturdayWorkbook.Sheets(1)
CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row
.Rows("2:" & CheckNumRows).Copy
End With
MainWorkbook.Activate
MainWorkbook.Sheets(1).Range("a1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
SaturdayWorkbook.Close
Set FridayWorkbook = Workbooks.Open(FilePathText & FriDateFull & ".csv")
CheckNumRows = 0
With FridayWorkbook.Sheets(1)
CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row
.Rows("2:" & CheckNumRows).Copy
End With
MainWorkbook.Activate
MainWorkbook.Sheets(1).Range("a1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
FridayWorkbook.Close
Set ThursdayWorkbook = Workbooks.Open(FilePathText & ThuDateFull & ".csv")
CheckNumRows = 0
With ThursdayWorkbook.Sheets(1)
CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row
.Rows("2:" & CheckNumRows).Copy
End With
MainWorkbook.Activate
MainWorkbook.Sheets(1).Range("a1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ThursdayWorkbook.Close
Set WednesdayWorkbook = Workbooks.Open(FilePathText & WedDateFull & ".csv")
CheckNumRows = 0
With WednesdayWorkbook.Sheets(1)
CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row
.Rows("2:" & CheckNumRows).Copy
End With
MainWorkbook.Activate
MainWorkbook.Sheets(1).Range("a1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
WednesdayWorkbook.Close
Set TuesdayWorkbook = Workbooks.Open(FilePathText & TueDateFull & ".csv")
CheckNumRows = 0
With TuesdayWorkbook.Sheets(1)
CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row
.Rows("2:" & CheckNumRows).Copy
End With
MainWorkbook.Activate
MainWorkbook.Sheets(1).Range("a1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Set rLastRow = Cells(Rows.Count, "A").End(xlUp)
rLastRow.Offset(-1).Resize(2).EntireRow.Delete
Range("A1:o" & Cells(Rows.Count, "A").End(xlUp).Row).Select
Selection.Copy
TuesdayWorkbook.Close
End Sub