Help with VBA user form data

rgreyb

New Member
Joined
Aug 30, 2011
Messages
6
How do I link data input to a UserForm TextBox to a specific cell in my worksheet?

I am trying to create a data input form.
Thanks
 

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 and welcome to the forum.

Say you had a form with two text boxes and one command button.

To place the text box contents into a specific cell you would use something like:

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
   [COLOR=darkblue]With[/COLOR] Sheets("Sheet1")
      .Range("A1").Value = TextBox1.Value
      .Range("B1").Value = TextBox2.Value
      [COLOR=green]'etc[/COLOR]
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
If you want to find the next available row to input the data try this:

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton2_Click()
   [COLOR=green]'===========================[/COLOR]
   [COLOR=green]'find the next available row[/COLOR]
   [COLOR=green]'===========================[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] [COLOR=Red]rw [/COLOR][COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   
   [COLOR=darkblue]With[/COLOR] Sheets("sheet1")
      [COLOR=green]'find the next available row for input[/COLOR]
      [COLOR=Red]rw [/COLOR]= .Range("A" & .Rows.Count).End(xlUp).Row + 1
      .Range("A" &[COLOR=Red] rw[/COLOR]).Value = TextBox1.Value
      .Range("B" &[COLOR=Red] rw[/COLOR]).Value = TextBox2.Value
      [COLOR=green]'etc[/COLOR]
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
   
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Bertie, you're brilliant. Thanks so much! I am just starting with VBA, and it was amazing how such a simple procedure could be so vexing.
 
Upvote 0

Forum statistics

Threads
1,225,155
Messages
6,183,212
Members
453,151
Latest member
Lizamaison

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