Log in

View Full Version : [SOLVED:] List Box - Active Document - Run Column Macro



dj44
05-27-2017, 06:13 PM
Folks good Saturday,

Well my list box was working as far as I can tell
And I added the column to run the macro




Option Explicit
Private Sub UserForm_Initialize()

'Stolen from Greg :)


Dim arrData() As String
Dim sourcedoc As Document
Dim i As Long
Dim j As Long
Dim myitem As Range
Dim m As Long
Dim n As Long


Application.ScreenUpdating = False

i = ActiveDocument.Tables(1).Rows.Count - 1

j = ActiveDocument.Tables(1).Columns.Count

ListBox1.ColumnCount = j

ReDim arrData(i - 1, j - 1)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = ActiveDocument.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
arrData(m, n) = myitem.Text
Next m
Next n

ListBox1.List = arrData

lbl_Exit:
Exit Sub
End Sub


Private Sub ListBox1_Change()

Application.Run "Macros. & ListBox1.Column(3) '<< Did work but then :("

'Application.Run ListBox1.Column(3)

End Sub



19305


But now the macro doesn't fire

I don't know what's wrong with it

If someone can take a look at this I would be very grateful

gmayor
05-27-2017, 08:40 PM
You have a quote missing. Assuming Column 3 contains the names of macros in the Macros module, It should be

Application.Run "Macros." & ListBox1.Column(3)

or better still


Private Sub ListBox1_Change()
If ListBox1.ListIndex >= 0 Then
Application.Run "Macros." & ListBox1.Column(3)
End If
End Sub

dj44
05-28-2017, 07:09 AM
Hello Graham,

good to see you.

I added the double quote marks , but it still says cant run this macro

I put the macros in the correct module

I made the basic test as shown in the table

it worked one day but then it just stopped:think:

gmaxey
05-28-2017, 07:19 AM
It has been a long time (if ever) that Greg used a variable like myItem for a range.

If you remove the leading space from your macro name in the table then your code worked fine.


Private Sub UserForm_Initialize()
Dim arrData() As String
Dim lngRows As Long, lngCols As Long
Dim oRng As Range
Dim lngRow As Long, lngCol As Long
lngRows = ActiveDocument.Tables(1).Rows.Count - 1
lngCols = ActiveDocument.Tables(1).Columns.Count
ListBox1.ColumnCount = lngCols
ReDim arrData(lngRows - 1, lngCols - 1)
For lngCol = 0 To lngCols - 1
For lngRow = 0 To lngRows - 1
Set oRng = ActiveDocument.Tables(1).Cell(lngRow + 2, lngCol + 1).Range
oRng.End = oRng.End - 1
arrData(lngRow, lngCol) = oRng.Text
Next lngRow
Next lngCol
ListBox1.List = arrData
lbl_Exit:
Exit Sub
End Sub
Private Sub ListBox1_Change()
Application.Run "Macros." & ListBox1.Column(3)
End Sub

dj44
05-28-2017, 08:08 AM
Hello Greg,

good sunday!

I hate it when something works, and then it falls flat.

I am on the proverbial goose chase for the missing code, and usually being a non conoseur of code i end up barking up the worng tree :grinhalo:

Well i need to make a note of the double quotes and the space in the table for the macro names

but apart from that alls well now

and happy sundays all