LinkedCell to Checkbox calling onAction Subroutine in VBA? - vba

I have a great number of checkboxes calling a subroutine whenever a checkbox is ticked/unticked. Now I would like to get the address to the linked cell of the checkbox that called the sub. I've tried
Application.Caller.LinkedCell
but I get "Object required" error, I guess because Application.Caller returns a string.
Is there some way I can get the address of the linkedCell?
Thank you for any help!

This assumes you are using a Checkbox from the Forms menu, as I don't think Application.Caller recognizes an ActiveX control. It uses the oddly-named ControlFormat property of a Shape (A Checkbox is a Shape):
Sub GetLinkedCell()
Dim shp As Shape
Dim chk As ControlFormat
Set shp = ThisWorkbook.Worksheets(1).Shapes(Application.Caller)
Set chk = shp.ControlFormat
MsgBox chk.LinkedCell
End Sub

Related

Use button caption as variable excel

This has to be an obvious thing to do, but I want to use the name of a command button that is pressed in excel as a variable in a macro. The macro would be as simple as setting the value of a cell to the name of the button; So if button captioned "10" is pressed the cells value would be "10", the same macro needs to work for all numeral button captions/names.
Again, sorry if this is obvious!
Try this, works with Forms buttons but not ActiveX buttons.
Sub Button1_Click()
If Not IsError(Application.Caller) Then
Dim obj2 As Object
Set obj2 = ActiveSheet.Shapes.Item(Application.Caller)
Debug.Print obj2.AlternativeText
End If
End Sub
but your question asked about command buttons (the ActiveX variety) and this is more involved, we need to find the shape and then drill in via OLEFormat and two layers of IDispatch to get a reference to the command button, then we use WithEvents to fire event handler.
Option Explicit
'* Inside Sheet module
Private WithEvents mcmd As MSForms.CommandButton
Private Sub Initialise()
Dim obj As Object
Set obj = Me.Shapes.Item("CommandButton1")
Set mcmd = obj.OLEFormat.Object.Object
End Sub
Private Sub mcmd_Click()
Debug.Print mcmd.Caption
End Sub
Sadly you need to initialise code like this for every command button I think.

VBA: how to use class module to change value of checkbox in userform

I have a userform with a lot of emailaddresses. I want the user to be able to select who to send an email to. I do so with checkboxes which are created on run time. To make it easier to use, I have also added a checkbox which allows the user to select of deselect all checkboxes.
This works perfectly as I want it to, but there is one problem I'm breaking my head over. If all checkboxes are checked and one gets unchecked, I want the "Select all" checkbox to be unchecked as well - and vice versa, if not all checkboxes were checked and the final checkbox is being checked by the user I want the "Select all" checkbox to be checked as well.
I try to do this using a class module. My overall knowledge of vba is pretty okay, but class modules are new territory for me, so excuse me if my language gets a bit fussy now.
In the initialize event of the userform I create a new collection and I assign clicks to these specific checkboxes. That works perfectly, as it doesn't give any errors in the initialize event of the userform and an event is triggered once I click one of these checkboxes. The problem I'm having is that I can't get a grip on the "Select all" checkbox (chkSelAll) in the userform. I've tried creating a public object for this checkbox in the userform (Public objSelAll As MSForms.CheckBox), but still it gives me the "Variable not defined" error once I click one of the checkboxes.
Here's the code for the class module (cls_RIRI):
Private WithEvents chkBox As MSForms.CheckBox
Public Sub AssignClicks(ctrl As Control)
Set chkBox = ctrl
End Sub
Private Sub chkBox_Click()
If chkBox.Value = False Then objSelAll.Value = False
'^This is where the error occurs: variable not defined
End Sub
And here's the relevant part of the Userform_Initialize event:
Private colTickBoxes As Collection
Public objSelAll As MSForms.CheckBox
Private Sub UserForm_Initialize()
Dim ChkBoxes As cls_RIRI
Dim ctrl As Control
Set objSelAll = Me.Controls.Item("chkSelAll")
Set colTickBoxes = New Collection
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" And Left(ctrl.Name, 1) = "M" Then
Set ChkBoxes = New cls_RIRI
ChkBoxes.AssignClicks ctrl
colTickBoxes.Add ChkBoxes
End If
Next ctrl
Set ChkBoxes = Nothing
Set ctrl = Nothing
End Sub
As you can see I didn't get to the point yet where I let the code check if all checkboxes are checked so that the select all checkbox can be checked as well. I'm not really looking for this code, I'll probably manage it once I get grip on the select all checkbox from the class module, so please don't worry about this part! :)
Private WithEvents chkBox As MSForms.CheckBox
private strParentFormName as string
Public Sub AssignClicks(ctrl As Control,strFormName as string)
strParentFormName=strFormName
.....
end sub
Private Sub chkBox_Click()
dim f as userform
set f=userforms(0) <--- or loop the userforms to get form name
If chkBox.Value = False Then f.controls("objSelAll").Value = False
'^This is where the error occurs: variable not defined
End Sub
and something like this
Public Function GET_USERFORM(strUserform As String) As UserForm
Dim i As Integer
For i = 0 To UserForms.Count - 1
If UserForms(i).Name = strUserform Then
Set GET_USERFORM = UserForms(i)
Exit For
End If
Next i
End Function
You'll need to pass in the form also, as the variable doesnt exist in the class. Add a property for the formname or if you'll only have 1 form open, then use the open form or reference the form if multiples are open. Your class exists on it's own as a check box. I am not sure, but you may be able to get the parent object from the checkbox. Hope this helps.
private strFormName as string
Public Property Let ParentForm(value as string)
strFormname=value
End Property
then...
userforms(strFormname).controls("objSelectAll").value=true

Using .AddItem With Different Comboboxes in Excel VBA Userform

I have a userform with a lot of comboboxes. I am trying to use .AddItem when the userform is intialized, with no success.
Here is what I have tried so far:
Sub Userform_Initialize()
Dim cCont As MSForms.ComboBox
For Each cCont In Me.Controls
cCont.AddItem "Item Added"
Next cCont
End Sub
I usually get a runtime error 13 "type mismatch" come up. Not sure if that has anything to do with the button I have on the userform as well.
How do I loop through a userform and use .AddItem to each combobox? Once I get the code on how to do this, I'll wrap it in an If statement to only add it to certain comboboxes with certain words in the name, if that helps for context.
Thanks in advance,
-Anthony
Anthony,
You are trying to call the AddItem method on each control on the form. But not all controls provide the AddItem method. You need to check out whether a particular control is combobox or not. And only if the control is combobox you may call the AddItem method.
How do I loop through a userform and use .AddItem to each combobox?
It is not clear where you need to loop through. Could you be more specific?
The best way to add controls to a userform that I have found is to follow this procedure.
Dim NewComboBox as Control
Set NewComboBox = Me.Controls.Add("Forms.ComboBox.1")
With NewComboBox
'Inside of this part you can put .name or any other property in the
'activex controls properties part, .left .top .width and .height determine the size of the box
.Name = Whatever Name You Desire
End With
Put this in your userform_initialize() event and you should be good to go.
You code works fine for me adding two combo boxes to a userform. Only thing I can think of is the correct references are not enabled for some reason.

Get reference to Forms checkbox in VBA event handler

I have some Forms Checkboxes in Excel 2010. I need to perform some common code when they are clicked. To do this, I'd like to pass a reference to the Checkbox, but so far I'm only able to get it typed as a shape.
To preempt the question, yes, they need to be Form Checkboxes and not ActiveX Checkboxes.
I'm a novice with VBA, so any help is appreciated.
Sub CheckBox1_Click()
'I really want this reference to be a Checkbox, not a Shape
Dim shape As Shape
Set shape = ActiveSheet.Shapes("Check Box 1")
DoSomething(shape)
End Sub
Sub DoSomething(MSForms.CheckBox)
'I need the reference to be a checkbox as I need to check
'whether it's checked or not here
End Sub
In such a scenario, don't have different click event for all checkboxes. Have just one. And use Application.Caller to get the name of the ckeckbox which called it. Pass that as a String to the relevant sub and then work with it.
UNTESTED
Sub CheckBoxMain_Click()
Dim sName As String
sName = Application.Caller
DoSomething (sName)
End Sub
Sub DoSomething(sCheck As String)
Dim shp As shape
Set shp = ActiveSheet.Shapes(sCheck)
With shp
'~~> Do something
End With
End Sub
You could also combine the two into one as well and link it with all checkboxes.
Sub DoSomething()
Dim shp As shape
Set shp = ActiveSheet.Shapes(Application.Caller)
With shp
'~~> Do something
End With
End Sub
This is similar to Siddharth's but adds the ControlFormat property of the Shape. ControlFormat gets you the Intellisense for the CheckBox, in this case Value:
Sub CheckBox1_Click()
Dim chk As Shape
Set chk = ActiveSheet.Shapes(Application.Caller)
With chk.ControlFormat
If .Value = True Then
MsgBox "true"
Else
MsgBox "false"
End If
End With
End Sub

How do I display a message when clicking on an object in Visio using VBA?

I'm totally new on this so I want to start with something simple.
I have just an object in my Visio document and I want to display a Hello World! message whenever it's clicked.
I've done something similar to this using the SelectionAdded event on the Visio.Application class. I check, if the selection.count is 1, then logically that shape has just been clicked, and if the shape type matches what you want, then display your message:
In the ThisDocument module (any object module, really):
Private WithEvents VsoApp As Visio.Application
Private Sub VsoApp_SelectionAdded(ByVal Selection As IVSelection)
If Selection.Count = 1 Then
MsgBox "Hello World"
End If
End Sub
Hope that helps.
-Jon