Copy Textbox values from UserForm to the worksheet

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Welcome. I am trying to copy the textbox values from the user form to the worksheet. I have 25 textboxes. What I am trying to do is copy the values while emptying all the textbox elements after execution. I found this code for @DanteAmor by chance. No, but I am having a problem modifying it to match my application.


VBA Code:
Private Sub CommandButton1_Click()
Dim arr As Variant, j As Long
  Dim k As Long, m As Long, n As Long
  ReDim arr(1 To 10, 1 To 10)
 
  Set src = Sheets("Data")
  lastrow = src.Cells(src.Rows.Count, 1).End(xlUp).Row + 1
  n = 1
      k = k + 1
      m = 0
      For j = n To n + 10
        m = m + 1
        arr(k, m) = Controls("TextBox" & j)
      Next
    n = n + 10
   src.Range("B" & lastrow).Resize(k, 10).Value = arr
 With src.Range("A2:A" & src.Cells(src.Rows.Count, "B").End(xlUp).Row)
    .Value = Evaluate("ROW(" & .Address & ")")
  End With
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
What are the names of your textboxes, and what cells do you want to put their values in? Be specific please.
 
Upvote 0
First, thank you for your interest. Text boxes from Textbox 1 to Textbox 25. I want to copy the values starting from row 3. When I try again, the copy is done after the last row with data.
 
Upvote 0
This writes to cells below the last used cell in column A

VBA Code:
Private Sub CommandButton1_Click()
    
    Dim i As Long
    Dim LastCell As Range
    
    Set LastCell = Range("A" & Rows.Count).End(xlUp)
    
    For i = 1 To 25
        'Write textbox values to sheet
        LastCell.Offset(i).Value = Me.Controls("TextBox" & i).Value
        'Clear textbox value
        Me.Controls("TextBox" & i).Value = Empty
    Next i
            
End Sub
 
Upvote 0
Solution
This writes to cells below the last used cell in column A

VBA Code:
Private Sub CommandButton1_Click()
   
    Dim i As Long
    Dim LastCell As Range
   
    Set LastCell = Range("A" & Rows.Count).End(xlUp)
   
    For i = 1 To 25
        'Write textbox values to sheet
        LastCell.Offset(i).Value = Me.Controls("TextBox" & i).Value
        'Clear textbox value
        Me.Controls("TextBox" & i).Value = Empty
    Next i
           
End Sub
Thank you, this is exactly what was needed.
 
Upvote 0
What are the names of your textboxes, and what cells do you want to put their values in? Be specific please.
Sorry, there is a problem that I did not notice before. Data must be copied horizontally. The suggested code is copied vertically
That is, from column A to W
 
Upvote 0
Change this...
VBA Code:
LastCell.Offset(i).Value = Me.Controls("TextBox" & i).Value

To this...
VBA Code:
LastCell.Offset(1, i - 1).Value = Me.Controls("TextBox" & i).Value
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,983
Members
452,540
Latest member
haasro02

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