0

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 ?

4
  • Did you just create a new account to ask this? Your other question is literally right before this one in VBA tag and you haven't even responded, (yes you are asking different things) but I guess it worked since your using it now... Commented Apr 25, 2021 at 10:55
  • Yes it works and thanks. I didnt notice that I was log in in different gmail. Commented Apr 25, 2021 at 11:01
  • 1
    You're surely getting an error when trying to populate the textbox? So you're trying to populate it in the same arrangement as the array correct? So multiple lines in the textbox and from what you have there 2 columns? Does it need to be textbox because a listbox has the built in columns control whereas textboxes do not have multi columns, which you could only replicate by using tab or something. Commented Apr 25, 2021 at 11:10
  • It needs to be a textbox. Maybe I'll use tabs to show it neatly. but i cant print it in textbox Commented Apr 25, 2021 at 11:16

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Now I'm not sure your intentions with this but if you're wanting to edit it, then add the stuff back into another array, that will be a lot more complex.
This line :For i = 1 To UBound(arr) shows error that "Type mismatch" I m sured i follow everything fine
Try For i = 1 To UBound(arr,1)
Works fine now. Thank you very much
No worries, you got the error because it's a 2d array and ubound needed to know whether to do it to the row count or the column count. Different systems have different needs. Mine works fine without adding it in (does rows by default) so that's why I didn't see the problem at first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.