mikenelena
Board Regular
- Joined
- Mar 5, 2018
- Messages
- 139
- Office Version
- 365
- Platform
- Windows
So I am looking to copy 4 columns of data (C,D,G,H) from a worksheet called "Main" to target worksheets. columns A,B,E, and F. The code is copying C and D to A and B. Column D is pasting to D, and G and H are not pasting at all.I believe the issues lies in this block of code, but I'm not sure what the exact problem is. (If it matters, column C is a file number, B is date, G is currency, and H is a comment.) As always, I appreciate any help folks can ofer.
Here is the full code:
VBA Code:
If Not IsError(destinationSheetName) Then
Set destinationSheet = ThisWorkbook.Worksheets(destinationSheetName)
Dim copyRange As Range
Set copyRange = Union(appraiserNamesRange.Cells(i, 3).Resize(1, 2), appraiserNamesRange.Cells(i, 7).Resize(1, 2))
destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 2).Value = appraiserNamesRange.Cells(i, 2).Resize(1, 2).Value
destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Offset(0, 3).Resize(1, 2).Value = copyRange.Value
destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Offset(0, 1).NumberFormat = "mm-dd-yyyy"
End If
Here is the full code:
VBA Code:
Sub CopyDataToWorksheets()
Dim mainSheet As Worksheet
Dim generalsSheet As Worksheet
Dim payrollDatesRange As Range
Dim lastPayrollDate As Date
Dim appraiserNamesRange As Range
Dim appraiserName As String
Dim destinationSheetName As String
Dim destinationSheet As Worksheet
Dim i As Long
' Set references to the relevant worksheets
Set mainSheet = ThisWorkbook.Worksheets("Main")
Set generalsSheet = ThisWorkbook.Worksheets("Generals")
Application.ScreenUpdating = False
' Determine the most recent payroll date
Set payrollDatesRange = mainSheet.Range("B:B")
lastPayrollDate = WorksheetFunction.Max(payrollDatesRange)
' Set the appraiser names range to only include the rows with the most recent payroll date
Set appraiserNamesRange = mainSheet.Range("A:C").Find(What:=lastPayrollDate, LookIn:=xlValues, LookAt:=xlWhole)
If Not appraiserNamesRange Is Nothing Then
Set appraiserNamesRange = appraiserNamesRange.Resize(mainSheet.Range("A:C").End(xlDown).Row - appraiserNamesRange.Row + 1, 3)
' Loop through the appraiser names range and copy/paste the data to the appropriate worksheets
For i = 1 To appraiserNamesRange.Rows.Count
appraiserName = appraiserNamesRange.Cells(i, 0).Value
destinationSheetName = Application.VLookup(appraiserName, generalsSheet.Range("A:H"), 8, False)
If Not IsError(destinationSheetName) Then
Set destinationSheet = ThisWorkbook.Worksheets(destinationSheetName)
Dim copyRange As Range
Set copyRange = Union(appraiserNamesRange.Cells(i, 3).Resize(1, 2), appraiserNamesRange.Cells(i, 7).Resize(1, 2))
destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 2).Value = appraiserNamesRange.Cells(i, 2).Resize(1, 2).Value
destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Offset(0, 3).Resize(1, 2).Value = copyRange.Value
destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Offset(0, 1).NumberFormat = "mm-dd-yyyy"
End If
Next i
Else
MsgBox "No data found for the most recent payroll date."
End If
Application.ScreenUpdating = True
' Clear clipboard and display a message
Application.CutCopyMode = False
MsgBox "Data has been copied to the appropriate worksheets."
End Sub