views:

280

answers:

1

In a former life I wrote some Visual Basic, and today I need to resurrect those skills to write an Excel Macro. While trawling the Internet looking for VBA examples to help we with this Excel macro, I came across this unusual syntax:

Set rFound = .Columns(1).Find(What:="Cat", After:=.Cells(1, 1), LookIn:=xlValues, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False)

Notice the .Columns and .Cells--the dot operators have no object names to the left.

I remember using this syntax long ago, but I can't remember the details. I'm having a hard time finding anything helpful on Google or in MSDN.

Questions:

  • When is this legal?
  • What variable is the dot operator infering?
  • Where can I get more information?

Thanks.

+9  A: 

It most likely is a with statement. Like this:

With testObject
    .Height = 100
    .Text = "Hello, World"
    .Rows = 20
    .Cols = 20
End With

For more information on the VB With statement look at the MSDN reference

Lucas McCoy
Of course! I had forgotten about that. I didn't realize that you could use the with statement to do more than set properties--you can put function calls in there. Thanks!
I actually liked that feature of VB.
Lucas McCoy