what does this code do?

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
What does this line of code do in the following procedure?
Code:
With srcWS.Range("A:A,B:B,H:H")

Code:
Private Sub CmdSend_Click()
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    Dim desWS As Worksheet
    Dim srcWS As Worksheet
    Set srcWS = ThisWorkbook.Sheets("NPSS_quote_sheet")
    Set desWS = Workbooks("Costing tool.xlsm").Sheets("Home")
    
    Dim lastRow1 As Long
    Dim lastRow2 As Long
    lastRow1 = srcWS.Range("B" & srcWS.Rows.Count).End(xlUp).Row
    lastRow2 = desWS.Range("A" & srcWS.Rows.Count).End(xlUp).Row
    
    Dim i As Long
    Dim header As Range
    Dim x As Long
      
    With srcWS.Range("A:A,B:B,H:H")
        For i = 1 To .Areas.Count
            x = .Areas(i).Column
            Set header = desWS.Rows(4).Find(.Areas(i).Cells(10), LookIn:=xlValues, lookat:=xlWhole)
            If Not header Is Nothing Then
                srcWS.Range(srcWS.Cells(11, x), srcWS.Cells(lastRow1, x)).Copy
                desWS.Cells(lastRow2 + 1, header.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            End If
        Next i
    End With
    With desWS
        .Range("D" & lastRow2 + 1 & ":D" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("G7")
        .Range("F" & lastRow2 + 1 & ":F" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("B7")
        .Range("G" & lastRow2 + 1 & ":G" & .Range("A" & .Rows.Count).End(xlUp).Row) = srcWS.Range("B6")
    End With
    With Application
        .CutCopyMode = False
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You use 'With' when you are doing multiple things with the same object, to shorten/simplify your code.
In your example, if you didn't use 'With', it would look like this

Code:
       For i = 1 To srcWS.Range("A:A,B:B,H:H").Areas.Count
            x = srcWS.Range("A:A,B:B,H:H").Areas(i).Column
            Set header = desWS.Rows(4).Find(srcWS.Range("A:A,B:B,H:H").Areas(i).Cells(10), LookIn:=xlValues, lookat:=xlWhole)
            If Not header Is Nothing Then
                srcWS.Range(srcWS.Cells(11, x), srcWS.Cells(lastRow1, x)).Copy
                desWS.Cells(lastRow2 + 1, header.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            End If
        Next i
 
Upvote 0
You use 'With' when you are doing multiple things with the same object, to shorten/simplify your code.
In your example, if you didn't use 'With', it would look like this

Code:
       For i = 1 To srcWS.Range("A:A,B:B,H:H").Areas.Count
            x = srcWS.Range("A:A,B:B,H:H").Areas(i).Column
            Set header = desWS.Rows(4).Find(srcWS.Range("A:A,B:B,H:H").Areas(i).Cells(10), LookIn:=xlValues, lookat:=xlWhole)
            If Not header Is Nothing Then
                srcWS.Range(srcWS.Cells(11, x), srcWS.Cells(lastRow1, x)).Copy
                desWS.Cells(lastRow2 + 1, header.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            End If
        Next i


Sorry, I didn't explain what bit I don't understand. I understand the with statement but why does it have a for instance, A:A? Is this because it is a column and why is it those 3 columns, A:A,B:B,H:H?

I am just trying to understand this piece of code.
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,210
Members
449,090
Latest member
bes000

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