Consulting

Results 1 to 3 of 3

Thread: Word ListBox - Selection Column 3 to Run the Macros

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Word ListBox - Selection Column 3 to Run the Macros

    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
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    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!
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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