This VBA macro filters data from one worksheet to a new worksheet based on certain criteria. It finds the last row of data, copies the header row to the new worksheet, then loops through each row checking if the value in column A meets one of three criteria ranges. If it matches, the entire row is copied to the new worksheet. Finally, it deletes any blank rows and auto-fits the columns.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
10 views1 page
Debit Balance Report Code
This VBA macro filters data from one worksheet to a new worksheet based on certain criteria. It finds the last row of data, copies the header row to the new worksheet, then loops through each row checking if the value in column A meets one of three criteria ranges. If it matches, the entire row is copied to the new worksheet. Finally, it deletes any blank rows and auto-fits the columns.
For i = 2 To lastRow 'Check if value in column A is between 371028 and 371199 or between 381054 and 381199 or between 481021 and 481199 If ws.Cells(i, "A").Value >= 371028 And ws.Cells(i, "A").Value <= 371199 Or ws.Cells(i, "A").Value >= 381054 And ws.Cells(i, "A").Value <= 381199 Or ws.Cells(i, "A").Value >= 481021 And ws.Cells(i, "A").Value <= 481199 Then 'If the criteria is met, copy the entire row to the new sheet ws.Rows(i).Copy ThisWorkbook.Sheets("FilterData").Rows(j + 2) j = j + 1 End If Next i
'Delete all blank rows in the new sheet
With ThisWorkbook.Sheets("FilterData") lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row For i = lastRow To 2 Step -1 If Application.CountA(.Rows(i)) = 0 Then .Rows(i).Delete End If Next i End With