I am creating a quiz. It works well for the part I have but is a bit lengthy. The RadioButtons I am using are set at the start through the Properties window. I have a "Reset" CommandButton that I am using to clear the quiz.
' Question Block 1
' THIS method does work
ActiveSheet.OptionButton1.Enabled = True
ActiveSheet.OptionButton2.Enabled = True
ActiveSheet.OptionButton3.Enabled = True
ActiveSheet.OptionButton4.Enabled = True
ActiveSheet.OptionButton1.Value = False
ActiveSheet.OptionButton2.Value = False
ActiveSheet.OptionButton3.Value = False
ActiveSheet.OptionButton4.Value = False
' Question Block 1 (Alternate Method)
' THIS method does NOT work
ActiveSheet.OptionButton1.GroupName = "Q1"
ActiveSheet.OptionButton2.GroupName = "Q1"
ActiveSheet.OptionButton3.GroupName = "Q1"
ActiveSheet.OptionButton4.GroupName = "Q1"
ActiveSheet.OptionButton.GroupName("Q1").Enabled = True
ActiveSheet.OptionButton.GroupName("Q1").Value = False
The first block of code works fine. The second one does not. What am I doing wrong with trying to assign the RadioButtons to a group in this manner? I've tried numerous approaches and most end with error messages saying object does not support this property or method, method or member not found, object required and on and on. Scratching my head. --Edited I am NOT using this in a UserForm. The RadioButtons are just placed on the Excel Sheet.
Found an answer that seems to work really well for my purposes. I was looking to do this by GroupNames property but as long as it works there are a number of ways to solve any problem. Hope someone else can benefit from this code.
' Defines OptionButtons as Objects
Dim OptBtn As OLEObject
' Re-Enables RadioButtons
For Each OptBtn In Sheet1.OLEObjects
If TypeName(OptBtn.Object) = "OptionButton" Then
OptBtn.Object.Enabled = True
End If
Next
' Resets RadioButtons to False (removes the dot)
For Each OptBtn In Sheet1.OLEObjects
If TypeName(OptBtn.Object) = "OptionButton" Then
OptBtn.Object.Value = False
End If
Next
Related
I've been learning today some VBA basics to apply in powerpoint, but I have some experience in some other languages. As title says, I want a picture to be shown after I click on 3 other pictures before that. When any of this 3 pictures are clicked, they trigger a tick to be shown above that image, and I'm using those as a refer to code my macro. I have the following:
Sub Condicion()
Set Diapo14 = ActivePresentation.Slides(14)
If Diapo14.Shapes("tick1").Visible = True And _
Diapo14.Shapes("tick2").Visible = True And _
Diapo14.Shapes("tick3").Visible = True Then
Diapo14.Shapes("FlechaDer").Visible = True
End If
End Sub
I have the picture I want to show (FlechaDer) with a disappear effect as soon as the slide starts, but no matter what I do, when I test the slide, the picture is always there. Maybe I'm not applying the correct approach, hope someone can help me. I'm not even sure if this can be done in PowerPoint.
I got it working with the code below. I had to replace the "ticks" in the If cycle, and instead of them I used the pictures that once clicked popped up the ticks:
Sub Can12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Can").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
Sub Wool12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Wool").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
Sub Pencil12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Pencil").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
It worked fine on activating an animation (Right Arrow popping up) when all 3 items were clicked. I had to add the corresponding macro to the picture. (Can, wool and pencil)
I have a report in which I am asking the users to click buttons to reveal where they need to add their commentary. I have it working but wanted to put in an If statement in case they have already expanded the row.
I have two macros, the first relates to the button they push and sends to the main macro the name of the button and a row number which is part of the section that is either expanded or collapsed
Sub ROccupancy()
'
Dim RecName As String
RecName = "ROccupancy"
Dim RowNum As Integer
RowNum = 27
Call ToogleRec(RecName, RowNum)
End Sub
The next macro is where I am having the trouble
Sub ToogleRec(RecName, RowNum)
'
Dim Toogle As String
Dim MyObj As Object
Set MyObj = ActiveSheet.Shapes.Range(Array(RecName))
Toogle = Left(MyObj.TextFrame2.TextRange.Characters.Text, 4)
TextName = Mid(MyObj.TextFrame2.TextRange.Characters.Text, 5, 100)
If Toogle = "Show" Then
MyObj.ShapeStyle = msoShapeStylePreset9
MyObj.TextFrame2.TextRange.Characters.Text = _
"Hide" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = False Then
Rows(RowNum).ShowDetail = True
End If
Else
MyObj.ShapeStyle = msoShapeStylePreset11
MyObj.TextFrame2.TextRange.Characters.Text = _
"Show" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = True Then
Rows(RowNum).ShowDetail = False
End If
End If
Range("C" & RowNum).Select
End Sub
The issue is the Rows(RowNum).ShowDetail is always TRUE, no matter if it's expanded or collapsed. I can remove the If section and set it to TRUE or FALSE using "Rows(RowNum).ShowDetail = False" or "Rows(RowNum).ShowDetail = TRUE". However, if the user has manually expanded or collapsed the row it causes an error (which freaks them out)
This question and answer seemed promising but Rows(RowNum).ShowDetail always seems to be TRUE
I put the MsgBox in there for error checking. I'll remove it in the final version.
Have you tried using Hidden property? Something like:
With Sheet1.Rows(5)
.ShowDetail = .Hidden
End With
Take note though that for you to use .ShowDetail method, you'll need to Group the rows first (needs to be in outline form).
True if the outline is expanded for the specified range (so that the detail of the column or row is visible). The specified range must be a single summary column or row in an outline.
Above code toggles hiding/unhiding a grouped row 5. You don't even need an If statement for the toggling. HTH.
I am writing a VBA code that is opened from a UserForm, which includes 2 ComboBoxes and 2 TextBoxes to form a 2D matrix by user's choice.
What I'm trying to do is, have a variable get the combobox value (one variable for each combobox) and if the selection is null, then the variables get the value of the textboxes.
I've googled the expression I need to use for that and stumbled upon IsNull, but the code doesn't work. What I tried was:
If IsNull(Matrix_Size_1.value) = False And IsNull(Matrix_Size_2.value) = False Then
lWidth = Matrix_Size_1.value
lHeight = Matrix_Size_2.value
ElseIf IsNull(Matrix_Size_1.value) = True And IsNull(Matrix_Size_2.value) = True Then
lWidth = Matrix_Hand_1.value
lHeight = Matrix_Hand_2.value
End If
Of course I also have to filter more plausible situations as: if one combobox is empty then msgbox, if everything is empty then msgbox but I'll handle it once I'll understand why my code doesn't work.
But the error I get if I DO NOT choose anything from the comboboxes is Type mismatch even though the If code has an option for both of the comboboxes to be empty.
I'd appreciate your help.
If Comboboxes .Style property is set as fmStyleDropDownList then use this
If Matrix_Size_1.ListIndex = -1 And Matrix_Size_2.ListIndex = -1 Then
If Comboboxes .Style property is set as fmStyleDropDownCombo then use this
If Len(Trim(Matrix_Size_1.Value)) = 0 And Len(Trim(Matrix_Size_2.Value)) = 0 Then
You can check the Style property in the design mode by right clicking on the combobox and clicking on properties.
Edit
From the comments, I have understood that you actually want this
Dim lWidth As Single, lHeight As Single
If Len(Trim(Matrix_Size_1.Value)) = 0 And Len(Trim(Matrix_Size_2.Value)) = 0 Then
lWidth = Val(Matrix_Hand_1.Value) '<~~> Val will take care of mismatch caused by empty boxes
lHeight = Val(Matrix_Hand_2.Value)
Else
lWidth = Val(Matrix_Size_1.Value)
lHeight = Val(Matrix_Size_2.Value)
End If
I am trying to create a clickable toggle button that hides a year's worth of data if it is currently unhidden and unhides it if it is hidden. I know this syntax is incorrect but I am not sure how to make it work. Any help would be appreciated.
Sub Hide_2012()
Dim Yr2012 As Range
Set Yr2012 = ThisWorkbook.Worksheets("Open Jobs Calculations").Range("AI:AT")
If Yr2012.Visible = False Then
Yr2012.Visible = True
Else If Yr2012.Visible = True Then
Yr2012.Visible = False
End If
This works for me. Apparently hidden only works on entire columns.
Dim Yr2012 As Range
Set Yr2012 = ThisWorkbook.WorkSheets("Open Jobs Calculations").Range("A1:B10") 'change range
If Yr2012.EntireColumn.Hidden = False Then
Yr2012.EntireColumn.Hidden = True
ElseIf Yr2012.EntireColumn.Hidden = True Then
Yr2012.EntireColumn.Hidden = False
End If
Edit per Scott's comment:
Yr2012.EntireColumn.Hidden = Not Yr2012.EntireColumn.Hidden
much more elegant.
This modification of #findwindow's answer worked for me:
Set Yr2012 = ThisWorkbook.Worksheets("Open Jobs Calculations").Columns("AI:AT") ' <- (ie, set range to the desired columns)
then you can still reference the "Yr2012" range, without needing ".EntireColumn":
If Yr2012.Hidden = False Then ...
Just another take on the same idea, to reduce typing :)
I am VERY new to VBA (and know of it only within excel).
I am trying to cycle through some (but not all) checkboxes. They are currently named CheckBox1 through CheckBox15. Howe do I cycle through say for instance CheckBox5 through CheckBox10?
I guess I am hoping there is a 'method' similar to 'CheckType' for controls that will allow me to check name?
Here is what I have tried. Causes a Compile Error - Sub or Function not defined, and highlights Worksheet.
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 2 To 4
If Worksheets("ActiveX").Controls("Checkbox" & i).Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
While the above doesnt work, what is below does:
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 1 To 2
If Sheet2.CheckBox2.Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
Of course, the loop has no affect on the outcome, but it compiles and atLeastOneChecked turns from False to True when Checkbox2 is True. Note that Sheet2 has been named ActiveX. I clearly don't understand how Worksheet and Controls work. Could someone help?
After fixing the error described below, this still won't work. I simplified to the following:
Private Sub BoxCheck()
Dim ole As OLEObject
atLeastOneChecked = False
Set ole = Sheets("ActiveX").OLEObjects("Checkbox2")
If ole.Value = True Then
atLeastOneChecked = True
End If
End Sub
This doesn't work. It fails at:
If ole.Value = True Then
The error states: Object Doesn't support this property or method
This is true for OLEObjects, but not for Checkboxes. When I look at the properties of ole, I see that its Object property is set to Object/Checkbox and that this Object has a value. I guess that is what I should be referencing in my if statement, but I don't know how.
I think I solved the problem.
The value of the Checkbox is accessed by Referencing the Object property within the OLEObject I set...Like this:
If ole.Object.Value = True Then
Thanks for all your help. If someone has a more elegant solution, I would still like to see it.
Use CheckBox.Name
Example:
For Each cb In ActiveSheet.CheckBoxes
If cb.Name = "CheckBox5"
' Do stuff
End If
Next cb
To expand on #Parker's answer:
Private Sub BoxCheck()
atleastonechecked = False
Dim oles As OLEObject
For i = 2 To 4
'If you're using Shapes Controls:
If ThisWorkbook.Worksheets("ActiveX").Shapes("Check Box " & i).Value = True Then
atleastonechecked = True
End If
' If you're using ActiveX Controls
Set oles = ThisWorkbook.Worksheets("ActiveX").OLEObjects("CheckBox" & i)
If oles.Value = True Then
atleastonechecked = True
End If
Next i
End Sub
Sorry, just put together the previous answer without testing - that always fails. Just use the If loop based on what type of control you're using.