REALLY SIMPLE QUESTION- Option buttons and sheet selection

nac3371

New Member
Joined
Jun 6, 2012
Messages
1
I'm really rusty on my VBA and this is really simple, but I can't seem to get it to work. I'm trying to create a database in excel. Basically I have a data entry form and one part allows you 4 options. The sheet that the data is entered in depends on which option you choose. So if you choose the first, the data enters into sheet 1 when you press submit. I just can't get this to work for some reason.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi and welcome to the forum.

Here is how I would approach coding your Submit button.

I have assumed your from is on a worksheet, so we need to set this up as the source, e.g.,
Code:
   [COLOR=green]'set up the source worksheet[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wsSource = Sheets("[COLOR=red]Form[/COLOR]")

Then we have to determine which Option Button was selected and assign a worksheet name accordingly:
Code:
   [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]True[/COLOR]
      [COLOR=darkblue]Case[/COLOR] OptionButton1
         sheetName = "Sheet1"
      [COLOR=darkblue]Case[/COLOR] OptionButton2
         sheetName = "Sheet2"
      [COLOR=darkblue]Case[/COLOR] OptionButton3
         sheetName = "Sheet3"
      [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]Else[/COLOR]
         sheetName = "Sheet4"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]

And we update this worksheet with the values on the "Form":
Code:
   [COLOR=green]'get the next available row in column A[/COLOR]
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      rw = .Range("A" & .Rows.Count).End(xlUp).Row
 
      [COLOR=green]'sample output[/COLOR]
      .Range("A" & rw).Value = wsSource.Range("A1").Value
      .Range("B" & rw).Value = wsSource.Range("B1").Value
      [COLOR=green]'etc[/COLOR]
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]


The ranges in the sample code will need to be edited to meet your needs but I hope this gives you enough ideas to ehlp solve your problem.

Here is the full code. Make a copy of your worksheet before playing about with it.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] cmd[COLOR=darkblue]Sub[/COLOR]mit_Click()
   [COLOR=darkblue]Dim[/COLOR] wsSource [COLOR=darkblue]As[/COLOR] Worksheet
   [COLOR=darkblue]Dim[/COLOR] sheetName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] rw [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   [COLOR=green]'set up the source worksheet[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wsSource = Sheets("Form")
 
   [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]True[/COLOR]
      [COLOR=darkblue]Case[/COLOR] OptionButton1
         sheetName = "Sheet1"
      [COLOR=darkblue]Case[/COLOR] OptionButton2
         sheetName = "Sheet2"
      [COLOR=darkblue]Case[/COLOR] OptionButton3
         sheetName = "Sheet3"
      [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]Else[/COLOR]
         sheetName = "Sheet4"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]
 
   [COLOR=green]'get the next available row in column A[/COLOR]
   [COLOR=darkblue]With[/COLOR] Sheets(sheetName)
      rw = .Range("A" & .Rows.Count).End(xlUp).Row
 
      [COLOR=green]'sample output[/COLOR]
      .Range("A" & rw).Value = wsSource.Range("A1").Value
      .Range("B" & rw).Value = wsSource.Range("B1").Value
      [COLOR=green]'etc[/COLOR]
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'free up memory[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wsSource = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] Sub
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

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