Command Button with options

billyshears

Board Regular
Joined
Aug 29, 2013
Messages
61
I have a userform setup that allows me to type a value into one of 5 different text boxes.

When I hit the command button, the value in Cell A1 should be changed by the value I type into one of the text boxes.

I know how to code the value being changed if I only have one box, but now that I several boxes, do I need an if statement here? If Box1=0, then grab value from Box2, if Box1 and Box2 = 0, then box 3 type of thing?

Thank you in advance
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Will only one textbox be filled?

Is it always A1 that should be changed?
 
Upvote 0
You could loop through the textboxes to get the value.
VBA Code:
Private Sub CommandButton1_Click()
Dim ctl As MSForms.Textbox
Dim idx As Long

    For idx = 1 To 5
        If Me.Controls("Box" & idx).Value <> "" Then
            Set ctl = Me.Controls("Box" & idx)
        End If
    Next I
   
    If Not ctl Is Nothing Then
        Range("A1").Value = Range("A1").Value + Val(ctl)
    End If
End Sub
[/code[
 
Upvote 0
Hmm..that seems out of my league. Would this be an easier way...

I have a userform setup with two text boxes that can be filled in.

TextBox A will increase Cell A1 by whatever figure I enter into the textbox

TextBox B will increase Cell A2 by whatever figure I enter into the textbox

I want to be able to bring up the userform, type the figures into Text Boxes A and B, and hit one button to execute the commands and then exit out of the userform.

I can get one textbox to work by using the following:

Private Sub CommandButton_Click()
With Sheets("Sheet1").Range("A1")
.Value = .Value - Me.TextBoxA.Value
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,661
Members
449,114
Latest member
aides

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