Userform populate Textbox value or Label caption from an Array

netuser

Active Member
Joined
Jun 19, 2015
Messages
420
Hi,

I have looped through certain range of cells to put value in an array. Now I want to put these values inside a Textbox with multiple lines or in a Label Caption.

I searched like crazy on Google but I find nothing.

Can anyone help please I need to finish a project and I am stuck because of this.

Code:
Dim arr() As Variant, i As Integer, LastRow, Rng As Range
 
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Worksheets("test").Range("A2:A" & LastRow)
 
i = 0
ReDim arr(0)
For Each cell In Rng
   
    If Range("C" & cell.Row) = "xyz" Then
    arr(i) = cell & "-" & Range("B" & cell.Row) & vbCrLf
    i = i + 1
    End If
    ReDim Preserve arr(i)
Next
 
‘Getting error here:
TextBox1.Text = arr


Thanks for your help J
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
MayBe!!!
Code:
[COLOR="Navy"]Sub[/COLOR] MG19Jul56
[COLOR="Navy"]Dim[/COLOR] LastRow, Rng [COLOR="Navy"]As[/COLOR] Range, cell [COLOR="Navy"]As[/COLOR] Range, nStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
 
LastRow = Range("A" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]Set[/COLOR] Rng = Worksheets("test").Range("A2:A" & LastRow)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] cell [COLOR="Navy"]In[/COLOR] Rng
   [COLOR="Navy"]If[/COLOR] Range("C" & cell.Row) = "xyz" [COLOR="Navy"]Then[/COLOR]
      nStr = nStr & cell & "-" & Range("B" & cell.Row) & vbCrLf
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
 
'[COLOR="Green"][B]‘Getting error here:[/B][/COLOR]
[COLOR="Navy"]With[/COLOR] TextBox1
    .Text = ""
    .Text = nStr
    .MultiLine = True
    .WordWrap = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
I was about done on this when Mick gave a similar answer:
Here is my script:
Code:
Private Sub CommandButton2_Click()
'Modified  7/19/2018  1:12:52 PM  EDT
Application.ScreenUpdating = False
Dim c As Range
Dim ans As String
Dim Lastrow As Long
TextBox1.MultiLine = True
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each c In Cells(1, 1).Resize(Lastrow)
        If c.Value = "xyz" Then ans = ans & c.Value & "-" & c.Offset(, 1).Value & vbNewLine
    Next
TextBox1.Value = ans
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I forgot about sheet name being test:
Try this:
Code:
Private Sub CommandButton2_Click()
'Modified  7/19/2018  1:35:28 PM  EDT
Application.ScreenUpdating = False
Dim c As Range
Dim ans As String
Dim Lastrow As Long
TextBox1.MultiLine = True
With Sheets("test")
    Lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    For Each c In .Cells(1, 1).Resize(Lastrow)
        If c.Value = "xyz" Then ans = ans & c.Value & "-" & c.Offset(, 1).Value & vbNewLine
    Next
End With
TextBox1.Value = ans
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top