Consulting

Results 1 to 9 of 9

Thread: List Box - Application.run Macros

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

    List Box - Application.run Macros

    Hi folks,


    I made a list box.

    I am trying to run a macro when the item is selected

    Column 1 list box list index

    Column 2 macro name to run

    eg

    0 Macro1

    1 Macro2

    2 Macro 3

    it is populated from the worksheet list
    Private Sub ListBox100_Change()
    
    
    
    Dim ws                 As Worksheet
    Dim i                   As Integer
    
    
        Set ws = Worksheets("Macros")
    
        For i = 3 To 7
    
        If ListBox100.ListIndex = ws.Cells(i, 1).Value Then    'Listindex
    
        Application.Run ws.Cells(i, 2).Value    ' Macro name to run
    
        'Call ws.Cells(i, 5).Value
    
    End If
    Next i
    End Sub
    it was at some point working but now the error says

    on the applicaiton.run line


    i have set the row source to column 1


    any ideas what it could be
    Cheers for your help

    dj

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


  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It's easier if you attach your workbook, then we can see returned values etc.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

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

    Problems Noted


    if no Macro in workbook gives error

    if numbers used in macro name or 2 short macro name gives error

    Is there any way to have error resume next ie only run macro if i select the item from the userform list box?
    I thought that was the idea run macro only if item is selected from the list box


    this has vexxed me no end since yesterday my list box malfunctioning
    Attached Files Attached Files
    Cheers for your help

    dj

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


  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Remove the RowSource and populate the listbox on activation. This helps ensure compatibility; better thought to use Find & Offset in case data is changed and the Row is not correct.
    Private Sub UserForm_Activate()Dim r As Range
    With Sheets("Macros")
    Set r = Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp))
    End With
    Me.ListBox100.List = r.Value
    End Sub


    You can avoid the loop in the code
    Private Sub ListBox100_Change()    ' or click the item
        Dim ws As Worksheet
        Dim i As Integer
        Set ws = Worksheets("Macros")
        i = ListBox100
        Application.Run ws.Cells(i + 2, 2).Value
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you M,
    let me do some investigating - Im sure too find some new self inflicted bug i create
    Cheers for your help

    dj

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


  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Why not just load the macro names into the listbox and run them directly from there?
    Be as you wish to seem

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

    pardon me , but im not sure how i would do that .
    Cheers for your help

    dj

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


  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    pardon me , but im not sure how i would do that
    Fill the listbox with Macro names

    Private Sub ListBox100_Change() ' or click the item
        Application.Run Listbox100 
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

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

    oh yes that would work too, Greg helped me a while back on the column target for a macro

    I could do this too

    Private Sub ListBox100_Click()
    Application.Run ListBox100.Column(2)
    End Sub

    I will test this later
    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
  •