Loop through textbox values in userform

NichoD

Board Regular
Joined
Jul 31, 2022
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello, I am trying to loop through textboxes from 1 through 24 in my userform, and print the text (if text exist) in column a. However, only the last textbox with a value is printed. Any advice?

Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row

Dim rng As Range
Set rng = ws.Range("A" & lr + 1)

Dim i As Integer

For i = 1 To 24
If Controls("txtColA" & i).Value <> "" Then
rng = Controls("txtColA" & i).Value

End If
Next i
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi,
see if this update to your code does what you want

VBA Code:
    Dim lr          As Long, i As Long
    Dim rng         As Range
    
    lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
    
    Set rng = ws.Range("A" & lr)
    
    For i = 1 To 24
        With Me.Controls("txtColA" & i)
            If Len(.Value) > 0 Then rng.Cells(i, 1).Value = .Value
        End With
    Next i

You will note that I have qualified the range for your last row variable (lr) with your worksheet object variable ws which I assume you have initialized elsewhere in your code

Dave
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
    Dim lr          As Long, i As Long
    Dim rng         As Range
    
    lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
    
    Set rng = ws.Range("A" & lr)
    
    For i = 1 To 24
        With Me.Controls("txtColA" & i)
            If Len(.Value) > 0 Then rng.Cells(i, 1).Value = .Value
        End With
    Next i

You will note that I have qualified the range for your last row variable (lr) with your worksheet object variable ws which I assume you have initialized elsewhere in your code

Dave
Thank you so much! this saved me so much time!!
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

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