+ Reply to Thread
Results 1 to 6 of 6

Trouble using variables across different subs

  1. #1
    Registered User
    Join Date
    01-26-2013
    Location
    Arlington, Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Question Trouble using variables across different subs

    I'm trying to populate several ComboBox's each time a filter is applied. I'm pretty new to VBA and am having difficulty with variables and calling macro's which use variables.

    Any help is much appreciated (I've pasted my code below)

    Thanks!



    Public colii As String, cboxii As String
    '***************************************************************
    '***** This Sub removes duplicate items and loads a single ComboBox *****
    '***** Obtained on the net & works good *****
    '***************************************************************
    Public Sub Load1Cbox(ByRef cboxii As String, ByRef colii As String)

    'Load a single combobox making sure no items are duplicated

    Dim AllCells As Range, Cell As Range
    Dim NoDupes As New Collection
    Dim i As Integer, j As Integer
    Dim Swap1, Swap2, Item

    FormNavigator.cboxii.Clear ' variable (Cboxii)
    Set AllCells = Sheets("IN-WORK").Range(colii) ' variable (Colii)

    On Error Resume Next
    For Each Cell In AllCells
    NoDupes.Add Cell.value, CStr(Cell.value)
    Next Cell
    On Error GoTo 0

    For i = 1 To NoDupes.Count - 1
    For j = i + 1 To NoDupes.Count
    If NoDupes(i) > NoDupes(j) Then
    Swap1 = NoDupes(i)
    Swap2 = NoDupes(j)
    NoDupes.Add Swap1, before:=j
    NoDupes.Add Swap2, before:=i
    NoDupes.Remove i + 1
    NoDupes.Remove j + 1
    End If
    Next j
    Next i

    ' Add the sorted, non-duplicated items to a ListBox
    For Each Item In NoDupes
    FormNavigator.cboxii.AddItem Item ' variable (Cboxii)
    Next Item
    FormNavigator.cboxii.ListIndex = 0 ' variable (Cboxii)
    End Sub

    '*******************************************************************
    '***** This Sub is intended to load several ComboBox's each time it is run *****
    '***** Intent is to re-populate from active x menu combobox *****
    '***** Does not run - (I'm not familiar with passing variables?? *****
    '**************************************************************

    Public Sub Load3Cboxs()
    Load1Cbox , colii = "A5:A500", cboxii = "ComboBox1" 'Tool Order Search Box
    Load1Cbox , colii = "B5:B500", cboxii = "ComboBox6" 'Tool Number Search Box
    Load1Cbox , colii = "C5:C500", cboxii = "ComboBox8" 'Customer Filter (COE)
    Load1Cbox , colii = "D5:D500", cboxii = "ComboBox9" 'Department Filter
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Trouble using variables across different subs

    Not 100% sure what the problem is - the code is hard to read without code tags, could you add them.

    Anyway, here's an example of how you would call Load1Cbox.
    Please Login or Register  to view this content.
    You don't need to use colii and cboxii, but they don't do any harm.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Trouble using variables across different subs

    Hi,

    maybe something like this?
    Please Login or Register  to view this content.
    I didn't check the rest of the code but this should be the syntax for calling other procedures that have arguments.

  4. #4
    Registered User
    Join Date
    01-26-2013
    Location
    Arlington, Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Trouble using variables across different subs

    Thanks RHCPergo!

    That helped me get to the last problem (I think). The variable "Cboxii" is not recognized in Load1Cbox.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Trouble using variables across different subs

    It should be this.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-26-2013
    Location
    Arlington, Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Trouble using variables across different subs

    Solved it Norie!... Thanks very much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1