need to show one array in a listbox bt the value needs to come from another array

velthro

New Member
Joined
Aug 29, 2010
Messages
27
Ok
Code:
I have 
array1=split("1,2,3,4,5", ",")
array2=split("one,two,three,four,five", ",")
when the user selects 3 in the listbox I need the actual value to be "five" from array2. 
note the values in array1 and array2 are not numbers.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is the listbox in a sheet or on a form?
How are you adding the items to the listbox? [nudge: code sample]
What would you need the actual value to be if the user selects 1,2,4 or 5?
 
Upvote 0
this is a userform in ms word
Code:
   ' the following is my code
   
Private Sub UserForm_Initialize()
Dim array1 As Variant
'Array1 is the value I would like to show in the listbox this works
array1 = Split("111-Huston,222-Sprongfield,333-New York,444-Denver,555-Omaha", ",")
' array2=split("111,222,333,444,555",",") is the value I want to replace null on the page
'(if the user selects 111-huston in the listbox i want the actual value to be 111)
ListBox1.List = array1
End Sub
Private Sub CommandButton1_Click()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "null"
        .Replacement.Text = ListBox1.Value()
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
 
Upvote 0
(if the user selects 111-huston in the listbox i want the actual value to be 111)
If you just want to replace "null" with the code (111,222,etc) of the listbox, then you can do
Code:
.Replacement.Text = Left(ListBox1.Value, 3)
this is contradiction to the original post
when the user selects 3 in the listbox I need the actual value to be "five" from array2
 
Upvote 0
Sorry about the mistype on the first post the numBer 3 should have been 5 :banghead:

left 3 wont work because the numbers will range from 3 digets to 15 digits and i could use if statements but there is a lot of data to input i just thought there would be some way to say ok the user selected say 5 values into the first array so we need to step the same number "5" into the second array and sellect that as the value to pass. if that makes since.
 
Upvote 0
I'm not sure now if the ListBox is a multi-select, but here's three different methods to get the selected-item-code....

Code:
Dim array1, array2 As Variant
Private Sub CommandButton1_Click()
'Option #1:: If a multi-select listbox
For x = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(x) Then
        strReplaceWith = strReplaceWith & array2(x) & ", "
    End If
Next x
strReplaceWith = Left(strReplaceWith, Len(strReplaceWith) - 2)
Debug.Print "Option 1: "; strReplaceWith
    
'Option #2:: If a single-select Listbox
strReplaceWith = Left(ListBox1.List(ListBox1.ListIndex), InStr(1, ListBox1.List(ListBox1.ListIndex), "-") - 1)
Debug.Print "Option 2: "; strReplaceWith
'Option #3:: If a single-select Listbox
strReplaceWith = array2(ListBox1.ListIndex)
Debug.Print "Option 3: "; strReplaceWith

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "null"
        .Replacement.Text = strReplaceWith
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
Upvote 0
You my friend are a genius :cool: item 3 is exactly what i was looking for. I should invest in the excel vba bible :)
Thank You for your time
 
Upvote 0
I'm sure this is a simple answer but how do I store a value in a variable if a certain listbox item is selected. Basically, I don't want what is displayed on the userform I want a value attached with it. similar to an html where name is one thing but the value is what get passed.
 
Upvote 0
As a lisbox can have multiple columns, we can load a multi-dimension array to the listbox and pull values from the array in the listbox.
[This also illustrates how to manilpulate the Listbox.Columns]
A ListBox.List is, by definition, an Array of type Variant (can hold virtually anything).

You need a UserForm with 1 listbox and 3 command buttons for this illustration. Look for MyGlobalVariable



Code:
Dim MyArray(6, 3)
'Array containing column values for ListBox.
Dim MyGlobalVariable
Private Sub CommandButton1_Click()
' Exchange contents of columns 1 and 3
    Dim i As Single
    Dim Temp As Single
    For i = 0 To 5
        Temp = ListBox1.List(i, 0)
        ListBox1.List(i, 0) = ListBox1.List(i, 2)
        ListBox1.List(i, 2) = Temp
    Next i
End Sub
Private Sub CommandButton2_Click()
    Dim i As Single
    For i = 0 To 5
        If ListBox1.Selected(i) Then
            MyGlobalVariable = ListBox1.List(i, 2)
            'The 2 in ListBox1.List(i, 2) indicates which column of the listbox to get
            CommandButton2.Caption = MyGlobalVariable
            Exit Sub
        End If
    Next i
End Sub
Private Sub CommandButton3_Click()
    With ListBox1
        Select Case .ColumnCount
        Case Is = 1
            .ColumnCount = 3
        Case Is = 2
            .ColumnCount = 1
        Case Is = 3
            .ColumnCount = 2
        End Select
    End With
End Sub
Private Sub UserForm_Initialize()
    Dim i As Single
    ListBox1.ColumnCount = 3
    'This list box contains 3 data columns
    'Load integer values MyArray
    For i = 0 To 5
        MyArray(i, 0) = i
        MyArray(i, 1) = Rnd
        MyArray(i, 2) = Rnd
    Next i
    'Load ListBox1
    ListBox1.List() = MyArray
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top