I have two forms.
form1 has four text boxes and a button for each textbox.
The button would setfocus on the textbox and bring up form2.
form2 is basicly a number keypad that also has a text box so that the user can select a number. That number will go in form2.textbox, which when changed will put the data in form1.textbox1.
The problem I'm having is how to tell form2.textbox to put data in form1.textbox2.
This is what my code looks like:
Public Sub textbox1_Click()
Me.textbox1.SetFocus
numbfrm.Show
End Sub
Private Sub textbox2_Click()
Me.textbox2.SetFocus
numbfrm.Show
End Sub
Private Sub textbox3_Click()
Me.txtactual.SetFocus
numbfrm.Show
End Sub
This is what is in the number form. It contains all of the numbers 1 to 10, but I just put the first three numbers here.
Private Sub Cmd1_Click()
TxtNumber.Value = TxtNumber.Value & "1"
End Sub
Private Sub Cmd2_Click()
TxtNumber.Value = TxtNumber.Value & "2"
End Sub
Public Sub TxtNumber_Change()
passnumber
End Sub
This is in a module:
Sub passnumber()
form1.textbox1.Value = numbfrm.TxtNumber
End Sub
I've been looking through the web to find an easy way to do that.
I tried puting in the module
Sub passnumber()
If form1.texbox1.foucs =true then
form1.textbox1.Value = numbfrm.TxtNumber
Else If form1.textbox2.foucs = true then
form1.texbox2.value =numbfrm.txtnumber
End sub
I have made a workaround for it, I put toggle buttons next to each box and when button is pressed it would mark it as true, and then I told it if that toggle is true it would use certain text box
Sub passnumber()
If form1.option1.value =true then
form1.textbox1.Value = numbfrm.TxtNumber
else
If form1.option2.value =true then
form1.textbox2.Value = numbfrm.TxtNumber
end if
end if
End sub
Related
I am trying to auto select a checkbox if any another checkbox is selected. All this checkboxes are on the same sheet and basically i want check box 7 to tick if checkbox 3,4 or 5 is selected.
Private Sub Worksheet_Change(ByVal Target As Range)
If Sheets("Start Page").CheckBox3 = True Or Sheets("Start Page").CheckBox4 = True Or
Sheets("Start Page").CheckBox5 = True Then
Sheets("Start Page").CheckBox7 = True
Else
End If
End Sub
Any help would be greatly appreciated.
Even if your checkboxes are linked to a cell (LinkedCell-property), the Worksheet-Change-event is not triggered when you click on a Checkbox.
You need to catch the Click-Event of the checkboxes. For every checkbox, put a Click-event routine into the sheet module. To prevent that the logic if or if not to set the "calculated" checkbox is repeated, let those event handler call a common routine that does the calculation.
Private Sub CheckBox3_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox4_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox5_Click()
Call SetMyCheckBox
End Sub
Sub SetMyCheckBox()
Me.CheckBox7.Value = Me.CheckBox3.Value Or Me.CheckBox4.Value Or Me.CheckBox5.Value
End Sub
You should, by the way, consider to give your checkboxes more meaningful names.
I have two textboxes in a userform.
I need that if the user has entered some number in one textbox and then places the cursor in the other textbox, the first text box data is deleted and vice versa.
I was able to lock the other textbox if one has data in it, as a workaround.
I tried to replicate the lock method for deleting the value, but it does not work.
Sub checkTB()
If ConversionForm.Controls("UnitFromEntry").Text <> "" Then
ConversionForm.Controls("UnitToEntry").Locked = True
Else
ConversionForm.Controls("UnitFromEntry").Locked = True
End If
End Sub
I call this sub in the before update event of the text box.
Private Sub UnitFromEntry_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Call checkTB
End Sub
Try this
Private Sub UnitToEntry_Enter()
UnitToEntry.Locked = False
UnitFromEntry.Locked = True
UnitFromEntry.Text = ""
End Sub
Private Sub UnitFromEntry_Enter()
UnitFromEntry.Locked = False
UnitToEntry.Locked = True
UnitToEntry.Text = ""
End Sub
I am creating a form out of excel-ActiveX but I have no idea how I can make the command button disabled if all mandatory fields (textbox and combobox) are empty. I've attached the sample form for your reference.
sample LA Form
Use the *_Change event for each control (and optionally the UserForm_Activate event) to set the .Enabled property for the Control Button.
For example, on a simple UserForm with 1 ComboBox (ComboBox1), 2 TextBoxes (TextBox1 and TextBox2) and 1 CommandButton (CommandButton1), the following code will ensure that CommandButton1 is disabled unless there is data in each of the other 3 controls:
Option Explicit
Private Sub CheckMandatoryFields()
'Is a separate sub to make it easy to edit for all Controls at the same time
If Len(TextBox1.Value) > 0 And Len(TextBox2.Value) > 0 And Len(ComboBox1.Value) > 0 Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub
Private Sub CommandButton1_Click()
MsgBox "Button is enabled!"
End Sub
Private Sub ComboBox1_Change()
CheckMandatoryFields
End Sub
Private Sub TextBox1_Change()
CheckMandatoryFields
End Sub
Private Sub TextBox2_Change()
CheckMandatoryFields
End Sub
Private Sub UserForm_Activate()
CheckMandatoryFields
End Sub
If your Buttons and Controls are in a Worksheet, then you need to change UserForm_Activate to Worksheet_Activate. If they are in different worksheets, or some in a Worksheet and others in a UserForm, then you will need to Fully Qualify your references (e.g. Sheet1.Textbox1.Value)
If you are using Form Controls in a Worksheet instead of ActiveX controls, then you can't use the _Change events, and the way you Reference the objects is different:
Sheet1.Shapes("TextBox1").TextFrame2.TextRange.Text 'The value of TextBox1
Sheet1.Shapes("ComboBox1").ControlFormat.Value 'Which Number item in ComboBox1 is selected
Sheet1.Shapes("ComboBox1").ControlFormat.List(n) 'The value of the nth item in ComboBox1
A Form Control button does not have an Enabled property - however, if you have a "Do Nothing" macro, you can bodge it like so:
Sub DoNothing()
'As it says
End Sub
Sub MakeASound()
Beep
End Sub
Sub ToggleButton()
If Sheet1.Shapes("Button 1").OnAction = "Sheet1!DoNothing" Then 'Disable Button 1
Sheet1.Shapes("Button 1").OLEFormat.Object.Font.ColorIndex = 16 'Font colour = Grey
Sheet1.Shapes("Button 1").OnAction = "Sheet1!DoNothing"
Else 'Enable Button 1
Sheet1.Shapes("Button 1").OLEFormat.Object.Font.ColorIndex = 1 'Font colour = Black
Sheet1.Shapes("Button 1").OnAction = "Sheet1!MakeASound"
End If
End Sub
I have some option buttons set up such as stp1, stp2, stp3, etc.
I have something that which is active into a cell, I want to be able to re-activate the option button via its name which is placed in the cell B6.
I tried many thing here is an example:
Worksheets("Data").Select
[B6].Value = oSel 'Option button select
oSel.Value = True 'as to re-activate the option button
I recieved the name by using:
For Each Control In Me.Controls
If InStr(Control.Name, "stp") Then
If Control.GroupName = "Beginning" Then
If Control.Value = True Then
oSel = Control.Name
Exit For
End If
End If
End If
Next
If you could help I would really appreciate it, and if I forgot some important information or you just need more... just ask. I am fairly new to VBA and I am experimenting.
Although not entirely sure what you require, this simple 'bare-bones' example should take you forward. It allows you to select a radio button on the userform which updates cell 'B6' and also put the radio button name in cell 'B6' which updates the userform. Cell 'B6' is on a sheet called 'Data'
Assumes a userform called UserForm1 and three radio buttons named stp1, stp2 and stp3 as shown. Note that for this example you should set the ShowModal property of UserForm1 to False and set the 'GroupName' property of each radio button to 'Beginning'. Run the example by selecting/running the UserForm1 module.
Put this code in a general module.
Sub chk()
Set sel = Sheets("Data").Range("B6")
With UserForm1
For Each Control In .Controls
If Control.GroupName = "Beginning" Then
If Control.Name = sel.Value Then
Control.Value = True
Exit For
End If
End If
Next
End With
End Sub
Put this code in the UserForm1 module
Private Sub UserForm_Initialize()
Call chk
End Sub
Private Sub stp1_Click()
With Me
Sheets("Data").Range("B6").Value = "stp1"
End With
End Sub
Private Sub stp2_Click()
With Me
Sheets("Data").Range("B6").Value = "stp2"
End With
End Sub
Private Sub stp3_Click()
With Me
Sheets("Data").Range("B6").Value = "stp3"
End With
End Sub
Put this code in 'Data' Sheet module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sel As Range
Set sel = Sheets("Data").Range("B6")
If Target = sel Then
Call chk
End If
End Sub
These last pieces of code may introduce you to the concept of 'events' and how to begin using them.
I have a question:
I need to create a user form that contain that usual OK and Cancel Buttons. It also should contain two sets of Options buttons, each set placed inside a frame. The captions on the first set should be basketball, baseball, football, the captions on the second set should be watch on TV and Go to games. I need to write the event handlers and code in a module so that when the program runs, the user sees the form. If the user makes a couple of choices and clicks OK, he should see a message like "Your favorite sport is basketball, and you usually watch on TV." If the user clicks Cancel, the message "Sorry you don't want to play" should appear.
I think I almost have it working, but I don't know why I cannot successfully execute the Macro.
My Code is :
Option Explicit
Private Sub CommandButton2_Click()
MsgBox ("sorry if you don't want to play")
End Sub
Private Sub commandbuttons_Click()
Dim optbasket As String, optbaseball As String, optfootball As String
Dim optwog As String, optgtg As String
Select Case True
Case optbasket
optbasket = True
Case optbaseball
optbaseball = True
Case optfootball
optfootball = True
End Select
If optwog Then
optwog = True
Else
optgtg = True
End If
btnok = MsgBox("you favorite sport is " & Frame1.Value & "you usually " & Frame2.Value & ",")
End Sub
Private Sub OptionButton1_Click()
End Sub
Private Sub btmcancel_Click()
End Sub
Private Sub btnok_Click()
End Sub
Private Sub Frame1_Click()
End Sub
Private Sub Frame2_Click()
End Sub
Private Sub optbaseball_Click()
End Sub
Private Sub optbasketball_Click()
End Sub
Private Sub optfootball_Click()
End Sub
Thank you very much!!!
There are a few things here:
You should name your buttons to be "OkButton" and "CancelButton" or something like that. They'll be easier to track later. Same thing for your radio buttons (baseball, basketball, etc.)
You don't need your select statement or your if statement
I don't think Frame1 and Frame2 have a .Value property you can call
Here is some sample code. You would add an object to your worksheet that could be clicked on. In this example I just inserted a rectangle object. form the Insert tab. Then in the UserForm code, I renamed the Ok Button to be OkButton and added the function OkButton_click. When it is clicked, I capture the values of the radio buttons. I named them baseball, basketball, and football accordingly as well as watch and go. If one of them is true, then I assign "game" which is a string I declared to be the appropriate title of the game. I did the same thing for whether the person likes to go to the game or watch it. Then I added the CancelButton_Click function to close the userForm.
Private Sub Rectangle1_Click()
UserForm1.Show
End Sub
Private Sub OkButton_Click()
Dim game as String, watchOrGo as String
If baseball Then game = "baseball"
If basketball Then game = "basketball"
If football Then game = "football"
If watch Then watchOrGo = "watch"
If go then watchOrGo = "go"
okbtn = Msg("Your favorite sport is " & game & ". You usually " & watchOrGo)
End Sub
Private Sub CancelButton_Click()
cnclbtn = Msg("Sorry you don't want to play")
Unload Me
End Sub
If you want the code in commandbuttons_Click() to run when you click OK, you need to put it in the click handler for the OK button: btnok_Click(); likewise for CommandButton2_CLick() and btncancel_Click().