PDA

View Full Version : [SOLVED:] Word ListBox - Selection Column 3 to Run the Macros



dj44
01-30-2017, 05:18 PM
Hi folks,
:)
I have a listbox in word on a userform.

I would like to run a macro when an item is selected.

This is a multicolumn listbox

And the macro name is in Column 3.

So each row has its own macro i would like to run




How can this be achieved ive looked everywhere and not sure what im doing wrong

ListBox1.List(RowIndex, ColumnIndex)



Private Sub CommandButton1()

Dim i as long
oMacro as string

If ListBox1.ListIndex = (i, 2) then
Call oMacro



'----- Basic Code
If ListBox1.ListIndex = 0 Then
Call Macro1
End If
If ListBox1.ListIndex = 1 Then
Call Macro2
End If
End Sub


Thank you for tips and advice

gmaxey
01-30-2017, 07:11 PM
Private Sub UserForm_Initialize()
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "A"
.List(.ListCount - 1, 1) = "Apples"
.List(.ListCount - 1, 2) = "Macro1"
.AddItem
.List(.ListCount - 1, 0) = "B"
.List(.ListCount - 1, 1) = "Blueberries"
.List(.ListCount - 1, 2) = "Macro2"
End With
End Sub
Private Sub ListBox1_Click()
Application.Run "Module1." & ListBox1.Column(2)
End Sub

dj44
01-30-2017, 07:37 PM
Thank you very much Greg,

That worked very nicely indeed. I've been looking for this for a couple of days

I have downloaded all your userforms and there is so much to learn :)

I started with a basic array


Private Sub UserForm_Initialize()
'Greg Array
Dim myArray() As String
Use Split function to return a zero based one dimensional array.
myArray = Split("AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|" _
& |HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|" _
& "MA|MI|MN|MS||MT|NE|NV|NH|NJ|NM|" _
& "NY|NC||OH|OK|ZOE|PA|RI|SC|SD|TN|" _
& "TX|UT|VT|VA|WA|WV|WI|WY", "|")
Use .List method to populate listbox.
ListBox1.List = myArray
lbl_Exit:
Exit Sub
End Sub


But then I got stuck running a macro, and a few hours later , I was troubled, so here I am

But nicely solved, I forgot I only put one column up so the blueberries dissapeard
but its solved now
Have a great evening!