-5

Semi new to VBA and my code is throwing run time error 424 object required. I realize is not the most efficient code, but that doesn't matter to me so long as it works. Unfortunately it's not working. I wish that this error gave me the option to debug and see what line is causing it, but oh well.

My code is essentially opening 7 workbooks with dates in the file name that are the last seven days then copying data from each to a single workbook. I'm just trying to save a co worker a chunk of his time and learn a little more about vba. My code is listed below.

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

I have tried removing all of the Dim MonDayYear As String and etc to see if the fact that it's a string rather than a date was causing it, but it doesn't seem to have helped. Unless it won't function as an undeclared variable either... All that is is to get the date into the proper format for the file extension anyway. The file extensions look like "sometext-20161031.csv" and I don't know how to get the date feature to show in YYYYMMDD.

Any help on dealing with the 424 error, the date format issue, or anything else you may see is much appreciated! Sorry this is a mess, I'm still learning.

  • 4
    On which line does the error occur ?? – Gary's Student Nov 01 '16 at 21:18
  • 2
    @Gary'sStudent - Every line with `Row.Count`... ;-) These should all be `.Rows.Count`. All the `Range` and `Cells` calls inside `With` blocks should also be `.Range` and `.Cells`. – Comintern Nov 01 '16 at 21:20
  • @Gary's Student I don't know that's one of the main problems I'm having. – sdrloveshim Nov 01 '16 at 21:24
  • @Comintern I'll give that a shot, thanks. – sdrloveshim Nov 01 '16 at 21:24
  • 1
    @Comintern is on it I think. I highly suggest also removing any uses of [`.Select`](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) as that can cause issues, especially if switching between multiple sheets. – BruceWayne Nov 01 '16 at 21:26
  • 1
    A good custom is to include `Option Explicit` at the beginning of your module to force you to declare all your variables. In this vein I don't see where you declare `MonDate` or some of your other variables. That being said this error usually represents that you haven't qualified your object correctly. Tough to say without knowing exactly what line errors out. You can try placing a break point early on and step through your code line by line pressing F8. Let us know where the error ocurrs. – nbayly Nov 01 '16 at 21:27
  • @Comitern, I don't expect you to fix all my problems or anything, but I thought I'd make you aware that your solution seems to have resolve my 424 error, and now I'm getting a 438 error. Again with no debug option and I don't know what line is causing it. – sdrloveshim Nov 01 '16 at 21:33
  • 1
    is there any class module executing. – Techie Nov 01 '16 at 21:37
  • @Nbayly Thank you! I've never been able to understand the step through feature very well, but it seems to have worked here to pin point the problem. I'm currently getting error 438 when I move between With Workbooks.Open(FilePathText & MonDateFull & ".csv") to CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row Does that mean it's the CCheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row is the line causing the error? – sdrloveshim Nov 01 '16 at 21:38
  • @HA560 There is a class module I was originally going to use, but I decided to make it a macro instead. It shouldn't be running but it is there. I don't need it anymore. I guess I'll try deleting it. Edit: that does not seemed to have worked, but I needed to get rid of it anyway, so thank you. – sdrloveshim Nov 01 '16 at 21:42
  • 1
    When it errors out on the ` CheckNumRows...` line highlight individual parts of the line and add a watch to see what value populates. As a guess I would say that you have scoped to the workbook in your with but not scoped the sheet. Testing will tell you specifically what part of that line is not working. – nbayly Nov 01 '16 at 21:44
  • @nbayly I'm not sure exactly how the watch works, but I see the values for monDateFull, CheckNumRows and FilePathText populate correctly. I did put a watch on .Cells(.Rows.Count, 1).End(xlUp).Row and nothing happens to that in terms of value or type, but I'm not sure it's supposed show anything. I'm guessing that's not a valid target to watch, but I really don't know. – sdrloveshim Nov 01 '16 at 21:54
  • @Comintern Sorry, I didn't see your comment about removing the .select until just now. I went a head and removed that, though it doesn't seem to have been causing my -current- errors. – sdrloveshim Nov 01 '16 at 21:58
  • @Gary's Student Thanks to the help of Nbayly I've found I'm getting run time error 438 on line CheckNumRows = .Cells(.Rows.Count, 1).End(xlUp).Row – sdrloveshim Nov 01 '16 at 22:01

1 Answers1

0

Thanks for all your help! I was able to get this working. Code below for anyone interested.

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