REVERSED MUSIC
REVERSED MUSIC

Reputation: 3

Print array in textbox

Im doing code where button 1 will get data from worksheet and store in array, and button 2 will print the array in worksheet and textbox.

Public arr As Variant
Private Sub UserForm_Click()
End Sub

Private Sub CommandButton1_Click()
arr = Range("B8:C17")
Range("B8:C17") = Clear
End Sub

Private Sub CommandButton2_Click()
Range("B8:C17") = arr
TextBox1.Text = arr

End Sub    
Private Sub CommandButton3_Click()
Unload Me

End Sub

Everthing is fine excepet it does not print array in textbox. what is wrong here ?

Upvotes: 0

Views: 593

Answers (1)

Simon
Simon

Reputation: 1375

So what you need to do is loop through your rows of array data and populate it one by one.

Firstly, you need to make sure to set Multiline to True in your textbox properties.

Then add this:

Private Sub CommandButton2_Click()

Range("B8:C17") = arr

Dim i As Long
For i = 1 To UBound(arr, 1)
    Me.TextBox1.Text = Me.TextBox1.Text & arr(i, 1) & Chr(9) & arr(i, 2) & vbCr
Next i

End Sub

Note:

This will only work for 2 columns in the array. If you have more, you will need to add another & arr(i, x) to the line with x being the column number.

If you need an extra tab between the 2 columns, add another & Chr(9) to the line next to the already existing one. See if that works for you.

Upvotes: 2

Related Questions