Not understanding with/end with
set rCTyLst=wsCtyLst.Range( _
wsCtyLst.Cells(2, sCtyLstCol), _
wsCtyLst.Cells(lRow,sCtyLstCol))
With wsCtyLst
Set rCtyLst = .Range(.Cells(2, sCtyLstCol), _
.Cells(lRow,sCtyLstCol))
End With
The two are equivalent.
As Jim Cone demonstrated, in this case using with would be for convenience -
less duplicate code to write.
or
set rCtyLst = wsCtyLst.Cells(2,sCtyLstCol).Resize(lrow-1,1)
would avoid both With and duplicate code.
--
Regards,
Tom Ogilvy
"davegb" wrote in message
oups.com...
I've tried to research this on the net, but couldn't find anything that
directly deals with my confusion. That is, what does With/End With do
different than using the object name and the appropriate code?
Walkenbach says it will save running time. Elsewhere I read that it
saves entry time. But it must do more than that. For example, some code
I just wrote, when I used the spreadsheet name, wouldn't run. When I
used With/End with, it worked.
set rCTyLst=wsCtyLst.Range(.Cells(2, sCtyLstCol), .Cells(lRow,
sCtyLstCol))
This gives an "invalid or unqualified reference error"
With wsCtyLst
Set rCtyLst = .Range(.Cells(2, sCtyLstCol), .Cells(lRow,
sCtyLstCol))
End With
This code runs. Very confusing. Why?
Thanks in advance.
|