In an Excel sheet, NOT on a UserForm - Trying to come up with a code to be able to drop down a combo box, i.e. to get it to open. Might use it to trigger with hover over a shape. But, get a "Compile error: Invalid use of Me keyword", and without "Me." get "Run-time error '424': Object required"
Sub ActivateCB1()
ActiveSheet.ComboBox1.Activate
Me.ComboBox1.DropDown
End Sub
Me only works in a code module attached to (eg) a worksheet or userform
Sub ActivateCB1()
With ActiveSheet.ComboBox1
.Activate
.DropDown
End With
End Sub
Related
I've been trying to reproduce a checkbox on Excel with 2 images (1 for Tick and 1 for Untick), but I got a problem because I simply never coded in VBA; apart from macros.
I got multiple checkboxes that are meant to be ticked/unticked.
I could record a macro for each of them, but doing so would be tedious and unefficient (there's 22 checkboxes in total)
The Macro Code looks like this.
Sub Tick()
ActiveSheet.Shapes.Range(Array("Picture 23")).Select
Selection.ShapeRange.ZOrder msoSendToBack
End Sub
Sub Untick()
ActiveSheet.Shapes.Range(Array("Picture 22")).Select
Selection.ShapeRange.ZOrder msoSendToBack
End Sub
My question is easy : How do I fetch the reference of the image I clicked in order to inject this same reference to the code and set its ZOrder ?
I can't use Excel's Checkboxes because they're too small.
Try following
Sub TickToggle()
On Error GoTo EH
Dim caller As Variant
caller = Application.caller
If Not IsError(caller) Then
ActiveSheet.Shapes(caller).ZOrder msoSendToBack
Select Case caller
Case "Checkbox1_tick":
' your code for when the checkbox1 is unticked
Case "Checkbox1_untick":
' your code for when the checkbox1 is ticked
End Select
End If
EH:
End Sub
Also, try using more meaningful names for those shapes (like above) in order to use select case on the caller value and do different things depending on what has been clicked. You can assign TickToggle to all checkbox shapes, this is preferred (re-usability).
I'm trying to unload a userform ("DataEntry") when the active cell is not in the appropriate sheet (index number 2). I am getting a "Type Mismatch '13" after the messagebox, and I don't know why, can anyone help?
This sub is in a module, not the userform. I have tried setting up a sub in the userform and calling that (single line sub "Unload Me", but I get the same result.
Sub unloadform()
If ActiveSheet.Index <> 2 Then
MsgBox "The schedule must be selected to enter data"
Unload DataEntry
Exit Sub
End If
End Sub
Update:
I occurred to me to put this code in the userform initialize sub, (I don't want the userform to load if the correct sheet is not selected). Now I get a Runtime ErrorĀ '91'
Tried replacing Unload Me with Unload DataEntry (the name of the userform), (same result), and replacing it with Unload VBA.Userforms("DataEntry"), which throws a Runtime '13' error.
Private Sub UserForm_Initialize()
If ActiveWorkbook.ActiveSheet.Index <> 2 Then
MsgBox "Schedule must be selected to enter data"
Unload Me
Else:
'other code if correct sheet is selected
End If
End Sub
I have a useform that uses a form, but it only populates when I click the 'clear' button.
Private sub userform1_Initialize()
'stuff populating a page
Then on the same page, I have:
Private Sub ClearButton_Click()
Call UserForm1_Initialize
End Sub
How do I go about using:
Call UserForm1_Initialize
somewhere that populates my form when the form pops up?
I tried putting it in the button to open the form (third line):
Private Sub CommandButton1_Click()
UserForm1.Show
UserForm1_Initialize
End Sub
But I get the error:
Compile error:
Sub or Function not defined.
I tried putting my initialize code in the module instead of in the form, but I get the error:
Run-time error '424'
Object Required
I can't put the entire code in the module, because it will not know where to look for cells, right? What am I doing wrong?
I also tried using public instead of private. Same error as above.
Thank you!
Here's an example of my code to activate a userform from my main macro:
UserForm1.Show
Then, the code for the userform itself (right-click UserForm1, then "View Code"):
Private Sub UserForm_Initialize()
ListBox1.List = ProgramScale
End Sub
This populates the listbox on the form automatically when the form appears.
I think Vincent G is right in the comments above in that the sub title for your userform should not have the form number in it, e.g. UserForm_Initialize not UserForm1_Initialize().
I'm trying to have a userform pop-up when I select a specific value from a drop-down menu. It's giving me a "Unable to get the Buttons property of the Worksheet Class" error. I can pull up the userform using a button. When I use the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G9")) Is Nothing Then
Select Case Range("G9")
Case "Sporadic (<5)"
spor_pay.Show
'Cells(2, 2) = "what's wrong"
End Select
End If
End Sub`
I get the error above. However, if I delete the userform (spor_pay is the userform), and try to just enter a value in another cell, the error doesn't pop up. What am I doing wrong with this? Again, if I create a macro from a button, and have the form pop up, there isn't a problem at all.
Thanks for the help!
I am trying to make a preferred non activex radio button to be selected by default when the excel document is opened.
What I was trying to do:
Private Sub Workbook_Open()
ActiveSheet.Shapes("Option Button 1").Value = True
End Sub
But I get this error message:
Run-time error '438':
Object doesn't support this property of method
Try this:
ActiveSheet.Shapes("Option Button 1").ControlFormat.Value = xlOn