0

I am trying to automate some reporting work and am trying to create a vba code that copies certain date from file 2 to file 1. However the code stops and i am getting an error of Application-defined or object-defined error in VBA code. Below is the code

Sub CopyDataFromFiles()
    Dim filePath1 As String
    Dim filePath2 As String
    Dim file1 As Workbook
    Dim file2 As Workbook
    Dim summarySheet As Worksheet
    Dim dataSheet As Worksheet
    Dim lastRow As Long
    Dim copyRange As Range
    Dim pasteRange As Range
    
    ' Set the file paths for File 1 and File 2
    filePath1 = "C:\Users\User\Documents\Test\Test_Report_Master.xlsx"
    filePath2 = GetLastModifiedFile("C:\Users\User\Documents\Test\Daily Reports")
    
    ' Open File 1 and File 2
    Set file1 = Workbooks.Open(filePath1)
    Set file2 = Workbooks.Open(filePath2)
    
    ' Set the summary sheet in File 2
    Set summarySheet = file2.Sheets("Summary")
    
    ' Set the data sheet in File 1
    Set dataSheet = file1.Sheets("Data")
    
    ' Find the last row in the data sheet of File 1
    lastRow = dataSheet.Cells(dataSheet.Rows.Count, "C").End(xlUp).Row
    
    ' Copy values from File 2 to File 1
    
    ' Copy values from column A and B in the Summary sheet of File 2
    Set copyRange = summarySheet.Range("A5:B" & summarySheet.Cells(summarySheet.Rows.Count, "A").End(xlUp).Row)
    
    ' Insert a new row in File 1's data sheet
    dataSheet.Cells(lastRow + 1, "C").EntireRow.Insert xlShiftDown
    
    ' Paste the copied values as values in File 1
    Set pasteRange = dataSheet.Range("C" & lastRow + 1)
    pasteRange.Resize(copyRange.Rows.Count, copyRange.Columns.Count).Value = copyRange.Value
    
    ' Copy the value from cell B4 in the Summary sheet of File 2
    Dim lastRowB As Long
    lastRowB = dataSheet.Cells(dataSheet.Rows.Count, "B").End(xlUp).Row
    Dim copyCell As Range
    Set copyCell = summarySheet.Range("B4")
    
    ' Find the first blank cell and all blank cells below in column B of File 1's data sheet **(This is where the error happens)**
    Dim blankCell As Range
    Set blankCell = dataSheet.Range("B" & lastRowB + 1).End(xlDown).Offset(1)
    
    ' Paste the copied value in the blank cells of column B
    blankCell.Resize(dataSheet.Rows.Count - blankCell.Row + 1).Value = copyCell.Value
    
    ' Save and close the workbooks
    file1.Close SaveChanges:=True
    file2.Close SaveChanges:=False
End Sub

Function GetLastModifiedFile(folderPath As String) As String
    Dim lastModifiedFile As String
    Dim lastModifiedDate As Date
    Dim fileName As String
    Dim fileDate As Date
    
    lastModifiedDate = DateSerial(1900, 1, 1)
    fileName = Dir(folderPath & "\*.xlsx")
    
    Do While fileName <> ""
        fileDate = FileDateTime(folderPath & "\" & fileName)
        If fileDate > lastModifiedDate Then
            lastModifiedDate = fileDate
            lastModifiedFile = folderPath & "\" & fileName
        End If
        fileName = Dir
    Loop
    
    GetLastModifiedFile = lastModifiedFile
End Function

Can anyone help me with this?

Using the vba code I tried to open C:\Users\User\Documents\Test Reports/Test_Report_Master.xlsx (You can assume this as File 1)

enter image description here

and also open the last modified file from folder C:\Users\User\Documents\Test Reports\Daily Reports (You can assume this as file 2).

enter image description here

Then I needed to copy all value from cell A5 and B5 and below in sheet named Summary of file 2 and paste them as value by inserting new row in cell C and D of Table 1 in sheet named Data of file 1. Again. copy the cell B4 in Summary sheet of file 2 and paste it in the first blank cell and all blank cells below of column B of Table 1 in Data sheet of file 1.

Update: As suggested in the comment, I have changed

Set blankCell = dataSheet.Range("B" & lastRowB + 1).End(xlDown).Offset(1)

to

Set blankCell = dataSheet.Range("B" & lastRowB + 1)

However, since I have already copied the data in by inserting a blank row at the end of Table 1 in column C and D of Data Sheet. The Set blankCell = dataSheet.Range("B" & lastRowB + 1) seems to ignore the blank cell in column B up until the recently pasted data in column C and D. It detects the blank cell only after the pasted data and copies it up until cell B1048526. You can see the image below to understand it better.

enter image description here

Kairu
  • 381
  • 1
  • 9
  • `Set blankCell = dataSheet.Range("B" & lastRowB + 1)`. – BigBen May 30 '23 at 16:32
  • Thank you for the help but when i run the vba after rmaking the changes, the date value from cell b4 is copied right below the data pasted before. See this screenshot https://ibb.co/vxyGBGt . Also the date gets copied to infinite rows below upto cell B1048526 – jitendra singh May 30 '23 at 16:50
  • The problem with that `Set blankCell = dataSheet.Range("B" & lastRowB + 1).End(xlDown).Offset(1)` is the `.Offset(1)`. With the `.End(xlDown)`, you get to the very last row of the sheet, and you can't `.Offset(1)` from there. I'm not sure which cell you actually want though. The first blank cell is `Set blankCell = dataSheet.Range("B" & lastRowB + 1)`. – BigBen May 30 '23 at 16:54
  • I changed Set blankCell = dataSheet.Range("B" & lastRowB + 1).End(xlDown).Offset(1) to Set blankCell = dataSheet.Range("B" & lastRowB + 1) . However, since i have already copied the data in by inserting a blank row at the end of data in column C and D. The Set blankCell = dataSheet.Range("B" & lastRowB + 1) seems to ignore the blank cell in column B up untill the recently pasted data in column C and D. It detects the blank cell only after the pasted data . That is the issue I'm facing now. You can see this image for further clarification ibb.co/vxyGBGt – jitendra singh May 30 '23 at 17:54

0 Answers0