Jason_Kha
asked on
Create Table in Word using Listbox Items (VBA)
Hey Experts,
I need help creating a table in word using items from a multicolumn listbox.
My listbox has three columns the first is item second is description and third is quantity. If i have 10 items in my listbox I want to be able to create a table with 10 rows with 3 columns each. if the number of items in my listbox would change to 20 items then i'll create a table with 20 rows with 3 columns each. Basicly I want the table to be a copy of the listbox.
sorry if its confusing
I need help creating a table in word using items from a multicolumn listbox.
My listbox has three columns the first is item second is description and third is quantity. If i have 10 items in my listbox I want to be able to create a table with 10 rows with 3 columns each. if the number of items in my listbox would change to 20 items then i'll create a table with 20 rows with 3 columns each. Basicly I want the table to be a copy of the listbox.
sorry if its confusing
ASKER
I get an error that DisOne 'variable not defined'
ASKER
I and J also not defined what should i define them as
Public Sub MakeTable()
Dim MyArray As Variant
Dim AddFor0Base As Integer
Dim DisOne As Integer '< added this line
MyArray = UserForm1.ListBox1.List
AddFor0Base = 0
If LBound(MyArray, 2) = 0 Then
AddFor0Base = 1
End If
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=UBound(MyArray, 1) + AddFor0Base, NumColumns:= _
UBound(MyArray, 2) + AddFor0Base, DefaultTableBehavior:=wdWo rd9TableBe havior, AutoFitBehavior:= _
wdAutoFitFixed
DisOne = ActiveDocument.Tables.Coun t
With ActiveDocument.Tables(DisO ne)
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
For j = LBound(MyArray, 2) To UBound(MyArray, 2)
.Cell(i + AddFor0Base, j + AddFor0Base).Range.Text = MyArray(i, j)
Next
Next
End With
End Sub
HTH
dragontooth
Dim MyArray As Variant
Dim AddFor0Base As Integer
Dim DisOne As Integer '< added this line
MyArray = UserForm1.ListBox1.List
AddFor0Base = 0
If LBound(MyArray, 2) = 0 Then
AddFor0Base = 1
End If
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=UBound(MyArray, 1) + AddFor0Base, NumColumns:= _
UBound(MyArray, 2) + AddFor0Base, DefaultTableBehavior:=wdWo
wdAutoFitFixed
DisOne = ActiveDocument.Tables.Coun
With ActiveDocument.Tables(DisO
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
For j = LBound(MyArray, 2) To UBound(MyArray, 2)
.Cell(i + AddFor0Base, j + AddFor0Base).Range.Text = MyArray(i, j)
Next
Next
End With
End Sub
HTH
dragontooth
Public Sub MakeTable()
Dim MyArray As Variant
Dim AddFor0Base As Integer
Dim DisOne As Integer '< added this line
Dim i As Integer '< added this line
Dim j As Integer '< added this line
MyArray = UserForm1.ListBox1.List
AddFor0Base = 0
If LBound(MyArray, 2) = 0 Then
AddFor0Base = 1
End If
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=UBound(MyArray, 1) + AddFor0Base, NumColumns:= _
UBound(MyArray, 2) + AddFor0Base, DefaultTableBehavior:=wdWo rd9TableBe havior, AutoFitBehavior:= _
wdAutoFitFixed
DisOne = ActiveDocument.Tables.Coun t
With ActiveDocument.Tables(DisO ne)
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
For j = LBound(MyArray, 2) To UBound(MyArray, 2)
.Cell(i + AddFor0Base, j + AddFor0Base).Range.Text = MyArray(i, j)
Next
Next
End With
End Sub
HTH
dragontooth
Dim MyArray As Variant
Dim AddFor0Base As Integer
Dim DisOne As Integer '< added this line
Dim i As Integer '< added this line
Dim j As Integer '< added this line
MyArray = UserForm1.ListBox1.List
AddFor0Base = 0
If LBound(MyArray, 2) = 0 Then
AddFor0Base = 1
End If
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=UBound(MyArray, 1) + AddFor0Base, NumColumns:= _
UBound(MyArray, 2) + AddFor0Base, DefaultTableBehavior:=wdWo
wdAutoFitFixed
DisOne = ActiveDocument.Tables.Coun
With ActiveDocument.Tables(DisO
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
For j = LBound(MyArray, 2) To UBound(MyArray, 2)
.Cell(i + AddFor0Base, j + AddFor0Base).Range.Text = MyArray(i, j)
Next
Next
End With
End Sub
HTH
dragontooth
ASKER
.Cell(i + AddFor0Base, j + AddFor0Base).Range.Text = MyArray(i, j)
this line give me the error invalid use of null
this line give me the error invalid use of null
MyArray has the exact same information in it as the listbox does. So that means the listbox has null information. So here is the check for empty and null.
Public Sub MakeTable()
Dim MyArray As Variant
Dim AddFor0Base As Integer
Dim DisOne As Integer
Dim I As Integer
Dim J As Integer
MyArray = UserForm1.ListBox1.List
AddFor0Base = 0
If LBound(MyArray, 2) = 0 Then
AddFor0Base = 1
End If
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=UBound(MyArray, 1) + AddFor0Base, NumColumns:= _
UBound(MyArray, 2) + AddFor0Base, DefaultTableBehavior:=wdWo rd9TableBe havior, AutoFitBehavior:= _
wdAutoFitFixed
DisOne = ActiveDocument.Tables.Coun t
With ActiveDocument.Tables(DisO ne)
For I = LBound(MyArray, 1) To UBound(MyArray, 1)
For J = LBound(MyArray, 2) To UBound(MyArray, 2)
If Not IsEmpty(MyArray(I, J)) And Not IsNull(MyArray(I, J)) Then
.Cell(I + AddFor0Base, J + AddFor0Base).Range.Text = MyArray(I, J)
End If
Next
Next
End With
End Sub
hth
dragontooth
Public Sub MakeTable()
Dim MyArray As Variant
Dim AddFor0Base As Integer
Dim DisOne As Integer
Dim I As Integer
Dim J As Integer
MyArray = UserForm1.ListBox1.List
AddFor0Base = 0
If LBound(MyArray, 2) = 0 Then
AddFor0Base = 1
End If
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=UBound(MyArray, 1) + AddFor0Base, NumColumns:= _
UBound(MyArray, 2) + AddFor0Base, DefaultTableBehavior:=wdWo
wdAutoFitFixed
DisOne = ActiveDocument.Tables.Coun
With ActiveDocument.Tables(DisO
For I = LBound(MyArray, 1) To UBound(MyArray, 1)
For J = LBound(MyArray, 2) To UBound(MyArray, 2)
If Not IsEmpty(MyArray(I, J)) And Not IsNull(MyArray(I, J)) Then
.Cell(I + AddFor0Base, J + AddFor0Base).Range.Text = MyArray(I, J)
End If
Next
Next
End With
End Sub
hth
dragontooth
ASKER
still one more thing
i have three columns in my listbox but when it creates the table the table has 10 columns
i have three columns in my listbox but when it creates the table the table has 10 columns
ASKER
what do i need to add to change the formatting of the table. for instance i want the table to be times new roman, 10pt and how do i make it so that the the rows are a certain width.
I will need to see the document now. see profile for sending location. I am thinking the listbox has 10 columns but only 3 columns are filled, but that is just a guess.
dragontooth
dragontooth
ASKER
yep thats right
for the formating
With ActiveDocument.Tables(DisO ne)
.Range.Font.Name = "Times New Roman" '< here is the font
.Range.Font.Size = 10 '< here is the font size
.Rows.Height = 15 '< this is in points for the row height
For I = LBound(MyArray, 1) To UBound(MyArray, 1)
For J = LBound(MyArray, 2) To UBound(MyArray, 2)
If Not IsEmpty(MyArray(I, J)) And Not IsNull(MyArray(I, J)) Then
.Cell(I + AddFor0Base, J + AddFor0Base).Range.Text = MyArray(I, J)
End If
Next
Next
End With
With ActiveDocument.Tables(DisO
.Range.Font.Name = "Times New Roman" '< here is the font
.Range.Font.Size = 10 '< here is the font size
.Rows.Height = 15 '< this is in points for the row height
For I = LBound(MyArray, 1) To UBound(MyArray, 1)
For J = LBound(MyArray, 2) To UBound(MyArray, 2)
If Not IsEmpty(MyArray(I, J)) And Not IsNull(MyArray(I, J)) Then
.Cell(I + AddFor0Base, J + AddFor0Base).Range.Text = MyArray(I, J)
End If
Next
Next
End With
ASKER
do you still want to see the doc.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How is it going?
dragontooth
dragontooth
ASKER
it works greats Thanks Alot!
So the 3 column vs 10 column problem doesn't apply anymore?
ASKER
no
ASKER
how would you set the table position so that the left side of the table is say for instance .25 inch from the left margin
Th only way I know is setting the actual margin at 0.25 which would effect the whole doc.
ASKER
yeah i figured that just wondering if you knew
The sub below will take a multicolumn listbox from a userform, make a table at the current cursor location and fill it with the data from the listbox. This is for Word VBA.
Public Sub MakeTable()
Dim MyArray As Variant
Dim AddFor0Base As Integer
MyArray = UserForm1.ListBox1.List
AddFor0Base = 0
If LBound(MyArray, 2) = 0 Then
AddFor0Base = 1
End If
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=UBound(MyArray, 1) + AddFor0Base, NumColumns:= _
UBound(MyArray, 2) + AddFor0Base, DefaultTableBehavior:=wdWo
wdAutoFitFixed
DisOne = ActiveDocument.Tables.Coun
With ActiveDocument.Tables(DisO
For i = LBound(MyArray, 1) To UBound(MyArray, 1)
For j = LBound(MyArray, 2) To UBound(MyArray, 2)
.Cell(i + AddFor0Base, j + AddFor0Base).Range.Text = MyArray(i, j)
Next
Next
End With
End Sub
HTH
dragontooth