VBA basic question

K

kirkm

I should know why this doesn't work...but
get 'Wrong number of arguments or invalid property assignment'
and can't resolve it.


With Worksheets("Stats").Range(p$)

For Each c In .Range
Debug.Print c.Value
Next

End With

The value of p$ is "C3:C20"


Many thanks - Kirk
 
N

Nigel

You cannot name a range p$ try this.....

Dim p As Range, c As Range
Set p = Range("C3:C20")

With Worksheets("Stats")

For Each c In p
Debug.Print c.Value
Next

End With
 
G

Guest

kirkm, You are doing the range thing twice; this will work:

Sub MacroTaDa()
Dim p$
p$ = "C3:C20"

With Worksheets("Stats")
For Each c In .Range(p$)
Debug.Print c.Value
Next
End With

End Sub
 
G

Guest

Or this will work:
Sub MacroTaDa2()
Dim p$
p$ = "C3:C20"

For Each c In Worksheets("Stats").Range(p$)
Debug.Print c.Value
Next

End Sub
 
D

Dana DeLouis

With Worksheets("Stats").Range(p$)
For Each c In .Range

Hi. You have answers from others.
Just to point out, your method would work if you drilled one level down ie.
"Cells" instead of "Range."

Sub Demo()
Dim P$
Dim C As Range
P$ = "C3:C20"

With Worksheets("Stats").Range(P$)
For Each C In .Cells
Debug.Print C.Value
Next
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA question 2
Conflict 8
Delete rows based on contents of Col A 0
Excel VBA 1
XNPV in VBA 2
Inaccurate count with code 6
No error message but code doesn't work 1
Much frustration... 3

Top