PDA

View Full Version : [SOLVED:] vba help on declaring varible



malleshg24
01-14-2018, 01:19 PM
:help
Hi Team,

I am new in vba , learning it, I have a question, my below macro works give the result.
But if I write variable in between Parenthesis , my macro didn't work. like
Sub test(dim i as integer)

Below macro works.



Sub test()
Dim i As Long
For i = 1 To 10
Range("a" & i).Value = i
Next i
End Sub

This didn't work.


Sub test(i As Long)

For i = 1 To 10
Range("a" & i).Value = i
Next i
End Sub

On google I come across coding like as below?.... So which scenerio we have to declare
variable like the below One. Thanks in advance!


Sub QuickSort(coll As Collection, first As Long, last As Long

SamT
01-14-2018, 02:48 PM
So which scenerio we have to declare variable like the below One.

Sub QuickSort(coll As Collection, first As Long, last As Long

Sub AnyName(Var 1 as Collection, var2 as Long, Var3 As Long)

I don't understand your problem.


Sub test_SamT(i As Long)
Dim x as long
For x = 1 To i
Range("a" & x).Value = x
Next x
End Sub


Sub Using_SubtestSamT()
Test_SamT 10
End Sub

Paul_Hossler
01-14-2018, 03:47 PM
1. You can't run a sub with parameters by using F5 (if that's what you were doing)

2. 'i' is declared as a parameter in the call, not as a local variable, and must be passed in the call



Sub test(i As Long)

For i = 1 To 10
Range("a" & i).Value = i
Next i
End Sub



Normally, it'd be something like this




Sub test2()

Call test(10)

End Sub


Sub test(n As Long)
Dim I as Long

For i = 1 To n
Range("a" & i).Value = i
Next I
End Sub


to put 1..10 in A1..A10

malleshg24
01-14-2018, 09:18 PM
Paul/Sam
I got it now ! Thank you guys for your precious time on my query. !!!

Regards,
Mallesh