B
BobbyC163
have an Excel worksheet (called MyWorksheet) where I am trying to select a
customer name and address from a list of all customers located on another
worksheet (CustomerWorksheet). I am using a multicolumn listbox on a userform
to select the customer data. Once selected, I want to capture the customer
data in a multicolumn array and then write the customer data to a worksheet
range in MyWorksheet.
The code I have written seems to be able to capture the selected customer
data (all columns) in an array but it will not write the data to the range in
the MyWorksheet. Any help would be greatly appreciated.
Thanks
Private Sub cmdOkay_Click()
Dim i As Long, j As Long
Dim lRows As Long, lCols As Long
Dim CustArray() As Variant
lRows = ListBox1.ListCount - 1
lCols = ListBox1.ColumnCount - 1
'Fill array with data of selected customer
For i = 1 To lRows
If ListBox1.Selected(i) Then
For j = 0 To lCols
ReDim CustArray(lRows, lCols)
CustArray(1, j) = ListBox1.List(i, j)
Next j
End If
Next i
Unload userform1
'Write array data to range on worksheet
With Worksheets("MyWorksheet").Range("CustomerData")
..ClearContents
..Value = CustArray
End With
End Sub
customer name and address from a list of all customers located on another
worksheet (CustomerWorksheet). I am using a multicolumn listbox on a userform
to select the customer data. Once selected, I want to capture the customer
data in a multicolumn array and then write the customer data to a worksheet
range in MyWorksheet.
The code I have written seems to be able to capture the selected customer
data (all columns) in an array but it will not write the data to the range in
the MyWorksheet. Any help would be greatly appreciated.
Thanks
Private Sub cmdOkay_Click()
Dim i As Long, j As Long
Dim lRows As Long, lCols As Long
Dim CustArray() As Variant
lRows = ListBox1.ListCount - 1
lCols = ListBox1.ColumnCount - 1
'Fill array with data of selected customer
For i = 1 To lRows
If ListBox1.Selected(i) Then
For j = 0 To lCols
ReDim CustArray(lRows, lCols)
CustArray(1, j) = ListBox1.List(i, j)
Next j
End If
Next i
Unload userform1
'Write array data to range on worksheet
With Worksheets("MyWorksheet").Range("CustomerData")
..ClearContents
..Value = CustArray
End With
End Sub