Hide/unhide multiple shapes Excel VBA - excel

I am very new to excel VBA & macros and I am trying to do something that I think is very simple, but I cannot for the life of me figure it out.
I have a shape ("shape 1") that when clicked should show/unhide two shapes ("shape 2" and "shape 3").
By default "shape 2" and "shape 3" should be hidden and only appear when "shape 1" is selected.
Any help will be much appreciated, remembering I am a complete novice here!
Edit:
I have managed to use the below, essentially a copy paste from here, I dont know what it means but it works for the single button. I cant figure out how to extend the code to include multiple objects being shown/hidden. An example of a second object to be shown/hidden at the same time as "july_2022" is "august_2022".
Public HIDE As Boolean
Sub fy ()
ActiveSheet.Shapes("july_2022").Visible = HIDE
If ActiveSheet.Shapes("july_2022").Visible = False Then
HIDE = True
Else
HIDE = False
End If
End Sub

ActiveSheet.Shapes("july_2022").Visible = HIDE is the part that sets the visibility of
a shape (july_2022). Another identical line but with something other than july_2022 would affect a second shape. The rest of the code (If.. Then.. Else.. End If) could be replaced with HIDE=Not(HIDE).
For example, the following code when run will 'toggle' the visibility of two shapes on the Active Sheet called 'Shape2' and 'Shape3'.
Public HIDE As Boolean
Sub fy()
ActiveSheet.Shapes("Shape2").Visible = HIDE
ActiveSheet.Shapes("Shape3").Visible = HIDE
HIDE = Not (HIDE)
End Sub

Related

Problem with multiline string in userform

I am having a issue with multiline stings in a userform.
When a user selects a option, the code checks if the selected answer matches the correct answer and then shows if right or wrong. But in either case the code says it is wrong.
Example of a option is:
If you see the string, brush it off sideways
Place icepack/cold flannel to reduce swelling
Elevate area to reduce bloodflow
Private Sub OptionButton1_Click()
rowNum = Selection.Row - Selection.ListObject.Range.Row
DeclareVars
Column = examtable.ListColumns("Right ans").DataBodyRange(rowNum)
CorrectAns = examtable.ListColumns("Right ans").DataBodyRange(rowNum).Offset(0, Column)
RightWrong.Visible = True
If OptionButton1.Caption = CorrectAns Then
RightWrong.BackColor = &HFF00&
RightWrong.Caption = "Right"
Else
RightWrong.BackColor = &HFF&
RightWrong.Caption = "Wrong"
End If
End Sub
What i am expecting is that if correct, shows right, or wrong if incorrect
If I understand correctly you have multiple option buttons below one another? Option button one will always have the same caption. If it is selected it can become true (indicated by the black dot), but the caption will not change.
Lets say it looks like this
two option buttons
Then the upper is called OptionButton1, the lower OptionButton2.
You can check
If OptionButton1 then
RightWrong.BackColor = &HFF00&
RightWrong.Caption = "Right"
Else
RightWrong.BackColor = &HFF&
RightWrong.Caption = "Wrong"
End if
You could use a combobox (these are the drop down menus).
You could populate it with an array of the strings you want to test and ask if the answers concur.
When loading the user form use
Private Sub UserForm_Activate()
ComboBox1.list = Array("brush it off sideways", "Place icepack/cold flannel to reduce swelling", "Elevate area to reduce bloodflow")
End Sub
this will look as followed
The user form with the list open
Then you can say combobox.value = CorrectAns

Can two checkboxes be linked in a way that only one will ever be checked?

I am working on moving a form over to Excel. Due to the style of form I have to replicate the form with no changes.
On the form there are two check boxes, one for Full and one for partial.
I have the code spit out a warning box if both are selected. I think it would be easier to make it so that if one is checked the other will automatically uncheck.
' ERROR CHECK FULL/PARTIAL
If [F8] = True And [F9] = True Then
MsgBox ("FAI can not be both a Full and Partial")
ELSE
'actual code in middle.
END IF
There are two ways to do this. With Radio buttons, or checking if the other box is checked.
With radio buttons is simpler if one of them must be selected no matter what. You can enclose radio buttons on different frames to "group" them together as in the screenshot below. You can see that one on each frame is selected and you can only select one of the two on each frame.
If you really want to use a Checkbox, you can use the code below.
Private Sub CheckBox1_Click()
If Me.CheckBox1 = True Then
If Me.CheckBox2 = True Then Me.CheckBox2 = False
End If
End Sub
Private Sub CheckBox2_Click()
If Me.CheckBox2 = True Then
If Me.CheckBox1 = True Then Me.CheckBox1 = False
End If
End Sub
The first If checks if this is a click to Check the box, if it is, then it will check if the second box is already checked, if it is, it will un-check it.
This is assuming you are creating a UserForm, and these are not CheckBoxes on an Excel Sheet. The implementation would be the same, but the code will be slightly different.

Excel VBA Calling the Calendar via Command Button in a form

I have just added in Calendar 12.0 from the tools > Additional Controls. Calendar works fine and I have it spitting the date out to the right cells. What I would like, however, is to really make the calendar visible from a command button as my form contains a bunch of fields and I don't want to bog up the form with this calendar. I have tried Calendar1.show but the .show isn't an option.
So ultimately I need a command button to show the calendar, allow the user to select (I have that) and then close the calendar. Any help? I thank you in advance!!
bdubb
In this snippet, CommandButton1 is from the ActiveX controls, not the form controls. It requires that you click the button to show the calendar (which pops up next to the button you clicked), and click the button again to hide the calendar.
Private Sub CommandButton1_Click()
If Not Calendar1.Visible Then
Calendar1.LinkedCell = "A1"
Calendar1.Top = Sheet1.CommandButton1.Top
Calendar1.Left = Sheet1.CommandButton1.Left + Sheet1.CommandButton1.Width + 1
Calendar1.Visible = True
Else
Calendar1.Visible = False
End If
End Sub
Obviously, different buttons would require different linked cells, but it does mean that you could have a single calendar control that it displyed by multiple buttons (if that is what you want).
Unfortunately, it would appear that you cannot hide the control while any of its events are firing (e.g AfterUpdate). It just doesn't want to disappear!!
Hide/Close a calendar control still not works (new year 2015 = almost four years later) but I think I found a workaround to hide the control after firing events.
I have a Calendar1_AfterUpdate(), which launches before Calendar1_Click(). Code is placed directly in a worksheet and NOT in a module.
Private Sub Calendar1_AfterUpdate()
Range("a1") = Me.Calendar1.Value
' Next two lines does not work within AfterUpdate
' When running step by step it seems to work but the control is
' visible when End Sub has run
Me.Calendar1.Visible = True
Me.Calendar1.Visible = False
End Sub
To that I simply added
Private Sub Calendar1_Click()
Me.Calendar1.Visible = True
Me.Calendar1.Visible = False
End Sub
Please note that the control for some reason must be made visible before hiding.
Why it does not work directly in Calendar1_AfterUpdate() is a mystery to me.
Next problem is to hide the control when I remove the mouse. Mouse-events seems to be impossible in a calendar control ...

Disable button on a userForm

I'm trying to figure out how to disable a button within my userForm if a certain cell within my spreadsheet equals a certain number. I tried the code stated below, but it isn't working.
Private Sub UserForm_Initialize()
Label2 = Sheets("DATA").Range("AM2").Value
Label4 = Sheets("DATA").Range("AO2").Value
Label7 = Format(Sheets("DATA").Range("R8").Value, "Currency")
If Sheets("DATA").Range("AL10").Value = 10 Then
ActiveSheet.Shapes("CommandButton1").Select
UserFormact_Upgrade.CommandButton1.Enabled = False
Else
End If
End Sub
Your code should be working, as you're on the right path.
To test it, simply create a new form and add this code, you'll see it should work. Maybe you're having problems within the IF clause?
Besides, you don't need to select the shape prior to disabling it; just disable it right away.
Private Sub UserForm_Initialize()
CommandButton1.Enabled = False
End Sub
I know this is old, but got to this thread trying to solve my problem, and found a solution that wasn't mentioned here. So in case someone gets here like I did, and this didn't quite get them where they needed to go, I thought this might help.
I had a userform with a drop down box called cmdADAMFields, and I didn't want my submit button called FieldsSubmitButton to be enabled until I selected something from the dropdown box.
I had to break up my argument into two different private subs vs one larger If-Then-Else statement.
First, I put:
Private Sub UserForm_Activate()
If cmbADAMFields.ListIndex = -1 Then FieldsSubmitButton.Enabled = False
End Sub
Then when for my pulldown's private sub when it's value changed I wrote:
Private Sub cmbADAMFields_Change()
FieldsSubmitButton.Enabled = True
End Sub
The proper place for setting Enabled property is in Activate event (associated with Show method) and not Initialize event (associated with Load instruction).
The below code disable the button CommandButton1 when AL10 cell >= 10.
Private Sub UserForm_Activate()
CommandButton1.Enabled = ( Sheets("DATA").Range("AL10") < 10 )
End Sub
For buttons you can choose between normal buttons (property Enabled=False and property Visible=true), disabled buttons (property Enabled=False and property Visible=true) and invisible buttons (property Enabled=False and property Visible=False), that it is a cleaner interface, in most cases.
Concerning text boxes, besides normal, disabled and invisible status, there is a locked status, that is enabled and visible, but cannot be user edited. (property Locked = True)
A locked control only can be changed by VBA code. For instance, someone can includes date text boxes, that it's filled using a secondary popup date form with Calendar control.

Can't close userform

Let me set up the environment.
This is VBA code running in Excel.
I have a userform that contains a msflexgrid. This flexgrid shows a list of customers and the customer', salesperson, csr, mfg rep, and territories, assignments. When you click in a column, let's say under the Territory column, another userform opens to show a list of Territories. You then click on the territory of your choice, the userform disappears and the new territory takes the place of the old territory.
This all works great until you click on the territory of your choice the 'Territory' userform does not disappear (it flickers) and the new territory does not transfer the underlying userform.
I should mention that when I'm stepping through the code it works great.
I'm assuming it has something do to with the flexgrid as all the other userform (that don't have flexgrids) that open userform work just fine.
Following is the some code sample:
** Click event from flexgrid that shows Territory userform and assignment of new territory when territory userform is closed.
Private Sub FlexGrid_Customers_Click()
With FlexGrid_Customers
Select Case .Col
Case 0
Case 2
Case 4
Case 6
UserForm_Territories.Show
Case Else
End Select
If Len(Trim(Misc1)) > 0 Then
.TextMatrix(.Row, .Col) = Trim(Misc1)
.TextMatrix(.Row, .Col + 1) = Trim(Misc2)
End If
End With
End Sub
** The following Subs are used in the Territory userform
Private Sub UserForm_Activate()
Misc1 = ""
Misc2 = ""
ListBox_Territory.Clear
Module_Get.Territories
End Sub
Private Sub UserForm_Terminate()
Set UserForm_Territories = Nothing
End Sub
Private Sub ListBox_Territory_Click()
With ListBox_Territory
Misc1 = Trim(.List(.ListIndex, 0))
Misc2 = Trim(.List(.ListIndex, 1))
End With
Hide
UserForm_Terminate
End Sub
I know this a long winded explanation but I'm a fairly decent VBA programmer and this has me stumped.
Any help would be greatly appreciated.
I'm not going to say what you're doing is wrong (in that it won't ever work), but it scares the heck out of me. This is not the way I'd deal with forms.
Firstly, you're using UserForm_Territories (the class/form name) to refer to an implicitly-created instance of the form. This is something I've always avoided doing. I would always create an instance of a form explicitly, so instead of:
UserForm_Territories.Show
I would do:
Dim oTerritoriesForm As UserForm_Territories
Set oTerritoriesForm = New UserForm_Territories
oTerritoriesForm.Show vbModal
' get the values from the form here
Unload oTerritoriesForm
Next, and much more worryingly, you're subverting the UserForm_Terminate behaviour by calling it explicitly. Why you're doing this I can't imagine, unless you thought that it would work around your stated problem. My advice: don't do that.
Worse, you're attempting to assign to the implicitly-created instance of the form within that Terminate method. You shouldn't be doing that, either. I'm surprised that even compiles.
It seems like you're trying to force the implicitly-created instance of the form to mimic an explicitly-created one. In which case, create it explicitly, as shown above.

Resources