Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Writing multicolumn array data to a worksheet range

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Writing multicolumn array data to a worksheet range

Any error message?
Try putting Unload userform1 at the very end of that Sub

RBS


"BobbyC163" wrote in message
...
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Writing multicolumn array data to a worksheet range

Thanks for the reply.

No error message appears. I did re locate the unload command as you
suggested but it made no difference.

I tried using a test array that I created as follows:
testArray = Array (1,2,3,4,5,6,7,8)

The data in the test array does get written into the worksheet range the way
I want using my code so I believe my problem has to do with the way i have
created my 2D array called CustArray.



"RB Smissaert" wrote:

Any error message?
Try putting Unload userform1 at the very end of that Sub

RBS


"BobbyC163" wrote in message
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Writing multicolumn array data to a worksheet range

Ah, yes, didn't look at that properly, but I see you have this solved now.

RBS

"BobbyC163" wrote in message
...
Thanks for the reply.

No error message appears. I did re locate the unload command as you
suggested but it made no difference.

I tried using a test array that I created as follows:
testArray = Array (1,2,3,4,5,6,7,8)

The data in the test array does get written into the worksheet range the
way
I want using my code so I believe my problem has to do with the way i have
created my 2D array called CustArray.



"RB Smissaert" wrote:

Any error message?
Try putting Unload userform1 at the very end of that Sub

RBS


"BobbyC163" wrote in message
...
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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Writing multicolumn array data to a worksheet range

I amended the code...
issues:
1) REDIM clears th earray, you need to include teh PRESERVE keyword
2) yuo can redim only the LAST column of the array with a redim, so I you
will, your table grows sideways, so the ROW in the LISTBOX transposes to
COLUMN in teh arraw...vice-versa with the columns of the listbox
3) because the array is transposed, we need to transpose it again before
dropping to the sheet.




Option Explicit
Private Sub cmdOK_Click()
Dim i As Long, j As Long
Dim lRows As Long, lCols As Long
Dim CustArray() As Variant

Dim arw As Long


lRows = ListBox1.ListCount - 1
lCols = ListBox1.ColumnCount - 1
arw = 0
'Fill array with data of selected customer
For i = 0 To lRows
If ListBox1.Selected(i) Then
arw = arw + 1
For j = 0 To lCols
ReDim Preserve CustArray(0 To 1, 1 To arw)
CustArray(j, arw) = 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 = WorksheetFunction.Transpose(CustArray)
End With


End Sub


"BobbyC163" wrote:

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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Writing multicolumn array data to a worksheet range

Awesome. It worked.

My customer list was 8 columns so I just changed your ReDim Preserve
parameters to (0 to 8, 1 to arw) and everything worked.

Thank you for the help.



"Patrick Molloy" wrote:

I amended the code...
issues:
1) REDIM clears th earray, you need to include teh PRESERVE keyword
2) yuo can redim only the LAST column of the array with a redim, so I you
will, your table grows sideways, so the ROW in the LISTBOX transposes to
COLUMN in teh arraw...vice-versa with the columns of the listbox
3) because the array is transposed, we need to transpose it again before
dropping to the sheet.




Option Explicit
Private Sub cmdOK_Click()
Dim i As Long, j As Long
Dim lRows As Long, lCols As Long
Dim CustArray() As Variant

Dim arw As Long


lRows = ListBox1.ListCount - 1
lCols = ListBox1.ColumnCount - 1
arw = 0
'Fill array with data of selected customer
For i = 0 To lRows
If ListBox1.Selected(i) Then
arw = arw + 1
For j = 0 To lCols
ReDim Preserve CustArray(0 To 1, 1 To arw)
CustArray(j, arw) = 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 = WorksheetFunction.Transpose(CustArray)
End With


End Sub


"BobbyC163" wrote:

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

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Writing multicolumn array data to a worksheet range

here's a tidier method if you don't need the arraY::

Private Sub cmdOK_Click()
Dim i As Long, j As Long
Dim target As Range
'clear the table
With Worksheets("MyWorksheet")
.Range("CustomerData").ClearContents
Set target = .Range("CustomerData").Resize(1, 1)
End With
'populate table from listbox
With ListBox1
For i = 0 To .ListCount - 1
If ListBox1.Selected(i) Then
target = .List(i, 0)
target.Offset(, 1) = .List(i, 1)
Set target = target.Offset(1)
End If
Next i
Unload UserForm1
End With
End Sub



"BobbyC163" wrote:

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill a multicolumn listbox with data from a range in another works Dale Fye Excel Programming 1 October 12th 07 06:58 PM
Writing array data to range object always writes 0's on worksheet eholz1 Excel Programming 5 September 22nd 07 03:54 AM
Writing a range to an array... Alex Excel Programming 2 December 29th 05 01:59 PM
Q: Best way to take data from VBA into graphs without writing data to worksheets? (Can a named range refer to an array in memory only?) KR Excel Programming 2 December 16th 04 11:12 PM
Writing Range to Array Marston Excel Programming 3 August 9th 04 09:11 PM


All times are GMT +1. The time now is 03:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"