dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,352
- Office Version
- 365
- 2016
- Platform
- Windows
I have some code I have written
The code works fine but I want to fine tune it.
There are sheets for every month as well as the 3 sheets mentioned in the code. The monthly sheets and the cancellations sheet have the same format. There will be only 1 row that have Dt and Req in the document and I want to move that row to the cancellations sheet.
At the moment, it loops through all monthly sheets, runs the autofilter, copies the last row (blank or not) to sht then deletes the row just below the header then moves to the next sheet.
What I want to do with it is search through each worksheet, looking for the row that contains Dt and Req. When it is found, move the row, delete the row where it was, then exit the loop.
Can someone help me with this please?
VBA Code:
Sub Transfer()
Dim ws As Worksheet, sh As Worksheet, sht As Worksheet
Set sh = Sheets("Totals")
Set sht = Sheets("Cancellations")
Dim Req As String: Req = sh.[B25].Value
Dim Dt As String: Dt = sh.[B27].Value
Application.ScreenUpdating = False
For Each ws In Worksheets
If ws.Name <> "Cancellations" And ws.Name <> "Totals" And ws.Name <> "Sheet2" Then
With ws.[A3].CurrentRegion
.AutoFilter 1, Dt ' autofilter for the value in cell [B27]
.AutoFilter 3, Req ' autofilter for the value in cell [B25]
.Offset(1).EntireRow.Copy sht.Range("A" & Rows.Count).End(xlUp).Offset(1)
.Offset(1).EntireRow.Delete
.AutoFilter ' turn off the autofilter
End With
End If
Next ws
sh.Range("B25,B27").ClearContents
Application.ScreenUpdating = True
End Sub
The code works fine but I want to fine tune it.
There are sheets for every month as well as the 3 sheets mentioned in the code. The monthly sheets and the cancellations sheet have the same format. There will be only 1 row that have Dt and Req in the document and I want to move that row to the cancellations sheet.
At the moment, it loops through all monthly sheets, runs the autofilter, copies the last row (blank or not) to sht then deletes the row just below the header then moves to the next sheet.
What I want to do with it is search through each worksheet, looking for the row that contains Dt and Req. When it is found, move the row, delete the row where it was, then exit the loop.
Can someone help me with this please?