May 23 2024 01:50 PM
Need help with vba macro in the below scenario
I have data in one workbook in sheet 1(raw data)
I have to apply filter to the sheet and filter by pending from status column.
Need to copy entire data except the header and paste in another existing workbook_2 in sheet 1,(report). Remove the filter.
Now again filter the data by completed status along with T-2 Working days from end date column.(Raw data).
Copy entire data, except the header and paste in (report) below the existing data.
Remove both the filters.
Again filter the data by exception in status column & copy entire data except the header and paste in report below the existing data.
Now from report sheet of workbook_2, have to copy column F,G,H,J and have to paste it in sheet 1 of Workbook_3 in column B only starting from row 4.
May 23 2024 08:46 PM
Here is a VBA macro that will accomplish the task of filtering and copying data between workbooks as specified. The macro assumes the following:
VBA Macro Code
The VBA code is untested and serves as an example only, please backup your file in advance as a precaution.
Sub CopyFilteredData()
Dim wbSource As Workbook
Dim wbDest As Workbook
Dim wbFinal As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim wsFinal As Worksheet
Dim lastRow As Long
Dim destLastRow As Long
Dim sourceFilePath As String
Dim destFilePath As String
Dim finalFilePath As String
' Set the file paths (adjust these paths as necessary)
sourceFilePath = "C:\Path\To\Workbook_1.xlsx"
destFilePath = "C:\Path\To\Workbook_2.xlsx"
finalFilePath = "C:\Path\To\Workbook_3.xlsx"
' Open the source workbook if not already open
On Error Resume Next
Set wbSource = Workbooks("Workbook_1.xlsx")
On Error GoTo 0
If wbSource Is Nothing Then
Set wbSource = Workbooks.Open(sourceFilePath)
End If
' Open the destination workbook if not already open
On Error Resume Next
Set wbDest = Workbooks("Workbook_2.xlsx")
On Error GoTo 0
If wbDest Is Nothing Then
Set wbDest = Workbooks.Open(destFilePath)
End If
' Open the final workbook if not already open
On Error Resume Next
Set wbFinal = Workbooks("Workbook_3.xlsx")
On Error GoTo 0
If wbFinal Is Nothing Then
Set wbFinal = Workbooks.Open(finalFilePath)
End If
' Set worksheets
Set wsSource = wbSource.Sheets("Sheet1")
Set wsDest = wbDest.Sheets("Report")
Set wsFinal = wbFinal.Sheets("Sheet1")
' Clear previous data in the destination report sheet
wsDest.Cells.ClearContents
' Apply filter for 'Pending' status and copy data
With wsSource
.AutoFilterMode = False
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A1:H" & lastRow).AutoFilter Field:=3, Criteria1:="Pending"
.Range("A2:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A1")
.AutoFilterMode = False
End With
' Apply filter for 'Completed' status and T-2 Working days from end date
With wsSource
.AutoFilterMode = False
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A1:H" & lastRow).AutoFilter Field:=3, Criteria1:="Completed"
.Range("A1:H" & lastRow).AutoFilter Field:=5, Criteria1:="T-2 Working Days" ' Adjust this filter based on your requirement
destLastRow = wsDest.Cells(wsDest.Rows.Count, 1).End(xlUp).Row + 1
.Range("A2:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A" & destLastRow)
.AutoFilterMode = False
End With
' Apply filter for 'Exception' status and copy data
With wsSource
.AutoFilterMode = False
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A1:H" & lastRow).AutoFilter Field:=3, Criteria1:="Exception"
destLastRow = wsDest.Cells(wsDest.Rows.Count, 1).End(xlUp).Row + 1
.Range("A2:H" & lastRow).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A" & destLastRow)
.AutoFilterMode = False
End With
' Copy columns F, G, H, J from report to Workbook_3 Sheet1 column B
With wsDest
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("F2:F" & lastRow).Copy wsFinal.Range("B4")
.Range("G2:G" & lastRow).Copy wsFinal.Range("C4")
.Range("H2:H" & lastRow).Copy wsFinal.Range("D4")
.Range("J2:J" & lastRow).Copy wsFinal.Range("E4")
End With
' Clean up
Application.CutCopyMode = False
MsgBox "Data transfer complete!"
End Sub
Explanation
Important Notes
This VBA macro should accomplish the task you described efficiently and accurately.
The text, steps and the code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.