ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA basic question (https://www.excelbanter.com/excel-programming/380294-vba-basic-question.html)

kirkm[_6_]

VBA basic question
 
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

Nigel

VBA basic question
 
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

--
Cheers
Nigel



"kirkm" wrote in message
...
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




SongBear

VBA basic question
 
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


"kirkm" wrote:

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


SongBear

VBA basic question
 
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

"kirkm" wrote:

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


Dana DeLouis

VBA basic question
 
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

--
HTH :)
Dana DeLouis
Windows XP & Office 2003


"kirkm" wrote in message
...
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




JMB

VBA basic question
 
Please don't multipost.

http://www.cpearson.com/excel/newposte.htm


"kirkm" wrote:

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



All times are GMT +1. The time now is 04:54 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com