Private Sub cmdplay_click()
LblGameName.Caption = "Hello! And welcome to Unicorn Adventure Extream! Are you excited?"
CmdExit.Caption = "no"
CmdPlay.Caption = "yes"
If CmdPlay = True Then
LblGameName.Caption = "As you should be!! Welcome to Unicopitopia! Will you please enjoy your stay?"
ElseIf CmdExit = True Then
LblGameName.Caption = "You have angered the unicorn!! She ripped out your heart and devoured it! Bad Luck! Much Blood!"
End If
End Sub
I dont understand why my label isn't changing when i press yes or no?
There are a few issues with your example.
You will want to have your buttons named and have the text in them stay the same. One could be cmdYes, and the other cmdExit. Then have click events associated with those buttons. To get that to happen, just double click on the button in the Form Object Designer mode, and it will bring up the code and generate a click event.
You definitely have things misnamed, according to your earlier naming.
I would have the Lbl you have named something like txtMessage, and have it as a locked textbox. You can set the background color of the textbox to match the UserForm color. Then just set the text of it to update with whatever printout you are trying to achieve.
Private Sub UserForm_Activate()
txtMessage.Text = "Hello! And welcome to Unicorn Adventure Extreme! Are you excited?"
End Sub
Then have separate events for your buttons.
Private Sub cmdExit_click()
Dim exitMessage As String
exitMessage = "You have angered the unicorn!! She ripped out your heart and devoured it! Bad Luck! Much Blood!"
txtMessage.Text = exitMessage
MsgBox("Game Over, Goodbye") ' or whatever else you want to have happen.
Unload Me 'You could unload the form here.
End Sub
A separate event for each button.
Private Sub cmdYes_click()
Dim YesMessage As String
YesMessage = "As you should be!! Welcome to Unicopitopia! Will you please enjoy your stay?"
txtMessage.Text = YesMessage
'Call some other subroutine that launches your game.
End Sub
Related
I am using a calendar created by Siddharth Rout found here:How can I create a calendar input in VBA Excel?
I have a userform that sits on top of a basic database that has a number of date fields.
I would like to use the one calendar form multiple times, i.e. have a cmdbutton1 on the userform that calls the calendar and puts the date in a label1, and then a cmdbutton2 that calls the same calendar form to populate label2. Obviously these will have different dates.
I have tried to do various forms of temporary binding to variables and nothing has worked. Any help would be appreciated!
You can create instances of the UserForm, set these up when you want and display when you need. From these instances you can modify the child userform
Private Sub CommandButton1_Click()
Dim ufrm2 As UserForm2
Set ufrm2 = New UserForm2
ufrm2.Label1.Caption = Me.CommandButton1.Caption
ufrm2.Show
End Sub
Private Sub CommandButton2_Click()
Dim ufrm2 As UserForm2
Set ufrm2 = New UserForm2
ufrm2.Label1.Caption = Me.CommandButton2.Caption
ufrm2.Show
End Sub
Giving:
I was looking at this question, and I think I have found the answer #Pete_B_C was looking for. I have added some code to the Userform "frmCalendar" to get the desired result.
'~~> Insert Selected date
Private Sub DTINSERT_Click()
If Len(Trim(Label6.Caption)) = 0 Then
MsgBox "Please select a date first", vbCritical, "No date selected"
Exit Sub
End If
*'~~> Change the code here to insert date where ever you want
If UserForm1.Visible = True Then
'do something
UserForm1.TextBox1 = Label6.Caption
End If
If UserForm3.Visible = True Then
'do something
UserForm3.TextBox12 = Label6.Caption
End If*
Unload Me
End Sub
I have used the option to hide and show the Userforms depending on which Userform is being used. If anyone knows how to do this in a better way than the way I have figured it, please let me know
My macro generates a series of reports that are 60 columns wide. I want users to be able to review the reports on screen before printing them or going on to another segment of the macro.
it there a way to set a scrollarea, have user review it, and then have the respond to a message box to continue the routine?
I tried this:
Sub reviewdata()
' Application.ScreenUpdating = False
Worksheets("Fin. Stmts").ScrollArea = ""
Application.Goto Reference:="monthlydata"
ActiveCell.Offset(2, 1).Select
ActiveWindow.FreezePanes = True
Worksheets("data. Stmts").ScrollArea = "monthlydata"
If MsgBox("End Review", vbOKOnly) = vbOK Then
End If
ActiveWindow.FreezePanes = False
Worksheets("data. Stmts").ScrollArea = ""
End Sub
the problem is that once the if, then statement is executed the user can not move around the worksheet since the routine needs a response to continue.
any insights are most appreciated.
thanks.
You can Use a Dummy Variable:
Dim dummy As Range
Set dummy = Application.InputBox("Scroll and Check. After That Select Ok!", "This is Specially created so that you can", Default:="A1", Type:=8)
Input Box that Takes in Range Allows you to Scroll in Background. Keep hitting Ok in and nothing will change, code will run as it is running at the moment.
This is a little clumsy but it sort of gets what you want. Instead of using a MsgBox use and InputBox as a range, which will allow the user to click around and scroll, as you describe. Whenever they hit okay/cancel, the macro will continue.
So probably replace your MsgBox line of code with....
Dim boom As Variant
boom = Application.InputBox("When you're done hit ""Cancel""... (""OK"" might lead to problems...)", _
"Scroll around and look at stuff", _
, , , , , 8)
I would recommend doing two macros instead, but this probably does what you need.
You can show that message in a small userform and call that userform in modeless state as shown below.
UserForm1.Show vbModeless
This way you will be able to navigate in the sheet with that message still showing.
You can also put the rest of the code in the button click event as shown below.
Option Explicit
Private Sub CommandButton1_Click()
ActiveWindow.FreezePanes = False
Worksheets("data. Stmts").ScrollArea = ""
Unload Me
End Sub
Private Sub Workbook_open()
With Sheet1.ComboBox1
.AddItem "Soccer"
.AddItem "Tennis"
End With
End Sub
I would like to make an if statement such that if the ComboBox1 value changes either from nothing to Soccer/tennis or from one item to another
then
Range("A1").Value = "This learner Plays Sport"
the problem is I don't know how to do the part where the combobox value changes either from nothing to Soccer/tennis or from one item to another
I have tried workbook.change and it gives me an error the closes the whole program.
Instead of workbook change event try the ComboBox change event like this:
Private Sub ComboBox1_Change()
If Sheet1.ComboBox1.Value = "Soccer" Or Sheet1.ComboBox1.Value = "Tennis" Then
Worksheets("Sheet1").Range("A1").Value = "This learner Plays Sport"
End If
End Sub
To get to this change event just double click the combobox from the Worksheet and it will open the VBA editor for you.
This only tells you what the newly selected value is. If you need to know what it was before the change it will be a bit more complicated. You'll need to create a variable to store/track the value to compare against once changed.
If you end up with a long list of sports, I'd suggest using Select Case instead of if statements. For example:
Private Sub ComboBox1_Change()
Select Case Sheet1.ComboBox1.Value
Case "Soccer", "Tennis"
Worksheets("Sheet1").Range("A1").Value = "This learner Plays Sports"
Case "Lacross"
Worksheets("Sheet1").Range("A1").Value = "Something Different"
Case Else
Debug.Print "value not in list"
End Select
End Sub
I have a worksheet that runs a weightlifting meet. Last year I had created a tab that would give information on the current lifter and on who was lifting next.
left side - Display, right side - input tab
When I input an "x" in columns R, S, T, or W on the data tab, it changes the information in the BenchGenerator tab, like so:
Updated Display tab
What I want to do is make a userform display to run on a different screen so people can see this information. I had accomplished this last year by widening excel and using two view windows - display on the second screen and running the meet on the computer. It was ok but very clunky looking. With a floating userform tab, it would look fantastic. I am new to this, but got the form floating:
Private Sub Worksheet_Change(ByVal Target As Range)
UserForm1.Show (vbModeless)
End Sub
And got the labels to initially populate:
Userform Display
Using this code:
Private Sub UserForm_Activate()
UserForm1.Label1.Caption = Sheets("BenchGenerator").Range("c4").Value
UserForm1.Label2.Caption = Sheets("BenchGenerator").Range("c5").Value
UserForm1.Label3.Caption = Sheets("BenchGenerator").Range("c6").Value
UserForm1.Label4.Caption = Sheets("BenchGenerator").Range("d3").Value
UserForm1.Label5.Caption = Sheets("BenchGenerator").Range("d4").Value
UserForm1.Label6.Caption = Sheets("BenchGenerator").Range("d5").Value
UserForm1.Label7.Caption = Sheets("BenchGenerator").Range("d6").Value
End Sub
What it doesn't currently do is update the captions when I input the "x" in the data tab.
As I mentioned, this is my first foray into userforms and looking through mountains of code trying to figure this out, it will not be my last as there is lots to accomplish with them.
Thanks in advance for any help!
You're pretty close to getting this to work. The problem is that your calling a new form every time a change occurs.
Declare your form as an object outside of the Sub that creates (Show) it.
You can then access it to update labels from another Sub that has the same scope.
Create an UpdateForm sub for example and call it from your Worksheet_Change event.
Try this, place the following code in a new Module:
Dim myForm As Object
Sub launchForm()
Set myForm = UserForm1
myForm.Show (vbModeless)
End Sub
Sub updateForm()
Dim wks As Worksheet
Set wks = Sheets("BenchGenerator")
'Update label values here
myForm.Label1.Caption = wks.Range("C4").Value
myForm.Label2.Caption = wks.Range("C5").Value
myForm.Label3.Caption = wks.Range("C6").Value
myForm.Label4.Caption = wks.Range("D3").Value
myForm.Label5.Caption = wks.Range("D4").Value
myForm.Label6.Caption = wks.Range("D5").Value
myForm.Label7.Caption = wks.Range("D6").Value
End Sub
If you use Worksheet_Change to update the form you'll want to verify the form exist or just skip any errors in the event if it doesn't.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
updateForm
End Sub
Not sure if there is an easier way to do it, but I made a concatenation routine to batch out my label setup and updates to quickly create/copy/paste all the code.
Just in case anyone didn't know how to do this
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().