Explain With statement notation?

Desu Nota from Columbus

Well-known Member
Joined
Mar 17, 2011
Messages
556
Can someone explain the notation for With statements?

Why is there a period? Do you use a "dot" before every line in the with statement? If not, why/when do you use/dont use the period?


I understand With statements function, but I don't understand the syntax.


Thanks.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Code:
[COLOR="Blue"]Sub[/COLOR] WithWith()

    [COLOR="Blue"]With[/COLOR] Range("A1")
        .Value = 1
        [COLOR="Blue"]With[/COLOR] .Font
            .Name = "Arial"
            .ColorIndex = 6
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
        .CurrentRegion.Copy
        .PasteSpecial xlPasteValues
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]


[COLOR="Blue"]Sub[/COLOR] WithoutWith()

    Range("A1").Value = 1
    Range("A1").Font.Name = "Arial"
    Range("A1").Font.ColorIndex = 6
    Range("A1").CurrentRegion.Copy
    Range("A1").PasteSpecial xlPasteValues

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
The dot means you are referring to whatever was referenced in the With.
You don't have to use the dot on every line, just the parts you want to refer to the reference in the With..

With Sheets("Sheet1")

With the With statement above, in subsequent code, instead of writing
Sheets("Sheet1").Range("A1").Value = "Hello"
You just write
.Range("A1").Value = "Hello"

Hope that helps clear it up.
 
Upvote 0
Or to put it yet another way, this:-
Code:
With [COLOR=red][B]wks[/B][/COLOR]
  [B][SIZE=3][COLOR=red].[/COLOR][/SIZE][/B]Range("A1").Copy Destination:=[SIZE=3][COLOR=red][B].[/B][/COLOR][/SIZE]Range("X99")
End With
is equivalent to:-
Code:
[B][COLOR=red]wks[/COLOR][COLOR=red].[/COLOR][/B]Range("A1").Copy Destination:=[COLOR=red][B]wks.[/B][/COLOR]Range("X99")
I'm not sure if this helps: http://msdn.microsoft.com/en-us/library/wc500chb(v=vs.80).aspx.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

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