Update multiple textboxes and cells

artzfx

New Member
Joined
Jul 7, 2004
Messages
6
Hello I am new here and very new to VBA,

I have a userForm with multiple textboxes referencing cells on a worksheet. Due to the vast number of Textboxes and cells, is there a method for the below code to be simplified, opposed to typing out 125 lines of similar code. Thankyou for any help.

Code:
Private Sub BtnUpdate_Click()
Sheets("Sheet1").Range("A1") = TextBox1.Text
Sheets("Sheet1").Range("A2") = TextBox2.Text
to... 
Sheets("Sheet1").Range("A125") = TextBox125.Text
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi!
Welcome to the board!
Try this!
Code:
Sub TextboxesToCells()
Dim sh As Shape
For Each sh In Sheet1.Shapes
If UCase(sh.Name) Like "*TEXTBOX*" Then
   Range("A" & Val(Mid(sh.Name, 8))) = ActiveSheet.OLEObjects(sh.Name).Object.Value
End If
Next sh
End Sub
 
Upvote 0
Thankyou for the quick reply, however I can't follow what you have suggested. Not sure what shapes refer to or where I should reference my text box and cell numbers.
 
Upvote 0
Try this

Private Sub BtnUpdate_Click()
Dim I As Integer

For I = 1 to 125

Sheets("Sheet1").Range("A" & I).Value = Me.Controls("Textbox" & I).Text

Next I
End Sub

This assumes BtnUpdate is on the same Userform as the all the textboxes.
 
Upvote 0
Thanks Norie it works great.

My textboxes are on various pages of my MultiPage control and the Update button is on the Userform itself. It still works, however should I be referencing the MultiPage1 as part of my code?

Thanks again.
 
Upvote 0
If it works I would just leave it.

The Controls collection of the Userform contains all the controls and I don't think they are grouped in any way.
 
Upvote 0
Cheers Norie.

Just noticed where you are from. I am heading to that part of the world next month for my brothers wedding at Wedderburn Castle (Duns). Small world...
 
Upvote 0

Forum statistics

Threads
1,215,543
Messages
6,125,423
Members
449,223
Latest member
Narrian

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