Consulting

Results 1 to 4 of 4

Thread: vba help on declaring varible

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    vba help on declaring varible


    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
    Last edited by SamT; 01-14-2018 at 02:42 PM. Reason: Corrected Code Format tag placement

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location
    Paul/Sam
    I got it now ! Thank you guys for your precious time on my query. !!!

    Regards,
    Mallesh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •