Thread: Nested With
View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Jac Tremblay[_4_] Jac Tremblay[_4_] is offline
external usenet poster
 
Posts: 99
Default Nested With

Hi again.
Thanks again.
--
Jac Tremblay


"Dave Peterson" wrote:

Just to add...

I'll often use:

dim ActWks as worksheet
set ActWks = activesheet

with actwks
.range(...

Just so that I get the intellisense help. And I won't rely on unqualified
ranges being on the activesheet (ok for code in a general module), but can cause
trouble if you copy|paste code elsewhere.



Jac Tremblay wrote:

Hi Dave,
Your answer was quick.
My code was only an example that I built on the fly.
Here is a new test I just made up.
Dim rngRange As Range
Dim rngCell As Range
Dim intI As Integer
With ActiveWorkbook.Sheets("Sheet1")
Set rngRange = .Range(.Range("A1").Offset(1, 0), _
.Range("A1").Offset(1, 0).End(xlDown))
For Each rngCell In rngRange
With ActiveWorkbook.Sheets("Sheet2").Range("C3")
.Offset(intI, 0).Value = rngCell.Value
intI = intI + 1
End With
Next rngCell
Set rngRange = Nothing
End With
It works fine.
I know that here too I could have gone without nesting the With statements
but that is not the point.
The reason I posted this question is that when you type a dot after an
object name in the inner With, nothing appears as if there was something
wrong. I guess that Excel cannot guess which object you are refering to.
Thank you Dave for your comment.
--
Jac Tremblay

"Dave Peterson" wrote:

Yep.

It worked when you tried it, right?

<vbg

But you could have gotten away without nesting, too:

With ActiveWorkbook.Sheets("Param")
' ...
Set rng1stCol = .Range(.Range(strRange).Offset(2, 0), _
.Range(strRange).Offset(2, 0).End(xlDown))
End with

For Each rngCell In rng1stCol.Cells
With Me.lstTest
.AddItem rngCell.Value
.List(.ListCount - 1, 1) = _
Format(rngCell.Offset(0, 1).Value, "yyyy-mm-dd")
End With
Next rngCell

Set rng1stCol = Nothing



Jac Tremblay wrote:

Hi,
Is it possible to nest With... End With statements?
Here is an example:
With ActiveWorkbook.Sheets("Param")
' ...
Set rng1stCol = .Range(.Range(strRange).Offset(2, 0), _
.Range(strRange).Offset(2, 0).End(xlDown))
For Each rngCell In rng1stCol
With Me.lstTest
.AddItem rngCell.Value
.List(.ListCount - 1, 1) = _
Format(rngCell.Offset(0, 1).Value, "yyyy-mm-dd")
End With
Next rngCell
Set rng1stCol = Nothing
End With
I just need some confirmation.
Thanks.
--
Jac Tremblay

--

Dave Peterson


--

Dave Peterson