' 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
If you just want to replace "null" with the code (111,222,etc) of the listbox, then you can do(if the user selects 111-huston in the listbox i want the actual value to be 111)
.Replacement.Text = Left(ListBox1.Value, 3)
when the user selects 3 in the listbox I need the actual value to be "five" from array2
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
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