I need each line commented in this code so I can update it

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I don't know what each line of this code does. Can someone add a comment for each line so I can update it 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" Then
                        With ws.[A3].CurrentRegion
                                .AutoFilter 1, Dt
                                .AutoFilter 3, Req
                                .Offset(1).EntireRow.Copy sht.Range("A" & Rows.Count).End(3)(2)
                                .Offset(1).EntireRow.Delete
                                .AutoFilter
                        End With
                End If
        Next ws
        
sh.Range("B25,B27").ClearContents
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
So, rather than comment EVERY line, why not tell us which ones you don't understand ?
This part, should be obvious
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
 
Upvote 0
Ok, with these lines,
VBA Code:
       Dim Req As String: Req = sh.[B25].Value
        Dim Dt As String: Dt = sh.[B27].Value
....using [ and ], if you surround the cell reference, is this a short way of range("B25")?

I also don't understand the most of the for each loop.
With this line, I understand that it is asking if the worksheet name is different to Cancellations or Totals but that is all.
VBA Code:
If ws.Name <> "Cancellations" And ws.Name <> "Totals" Then
 
Upvote 0
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 'shorter version of Range("B25")
        Dim Dt As String: Dt = sh.[B27].Value    'As above
        
Application.ScreenUpdating = False
        
        For Each ws In Worksheets
                If ws.Name <> "Cancellations" And ws.Name <> "Totals" Then 'any worksheet except the 2 mentioned
                        With ws.[A3].CurrentRegion 'the region or range of cells [A3] resides in
                                .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(3)(2) 'copy row to first available row on sht sheet
                                .Offset(1).EntireRow.Delete 'delete the row once copied
                                .AutoFilter ' turn off the autofilter
                        End With
                End If
        Next ws
        
sh.Range("B25,B27").ClearContents 'clear the contents of B25, B27 on the "Totals" Sheet
Application.ScreenUpdating = True

End Sub
 
Upvote 0
I understand the autofilter feature looks for and finds rows that have the values in B25 and B27 in columns 1 and 3 but why does it have an offset of 1, as you are finding the cell? Does it need to include the header row?

Also, in the offset line of code, why is there a (3)(2) after the .end?
 
Upvote 0
Offset 1 is to avoid the header row and select the next row down
AND
Instead of
VBA Code:
.Offset(1).EntireRow.Copy sht.Range("A" & Rows.Count).End(3)(2)
USE
VBA Code:
.Offset(1).EntireRow.Copy sht.Range("A" & Rows.Count).End(xlUp).Offset(1)
which may make more sense to you
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top