Select linked cell, cell next to checkbox, or get the checkbox name - vba

I have a document full of Checkboxes and I dont want to write specific VBA code for each checkbox because the file size needs to stay relatively small. What I am trying to do with my code is when the checkbox is checked, it automatically selects the cells next to it(not hard coded in using "Range") and then perform the rest of the programed VBA function.
How do I either get the name of the checkbox, select the linked cell, or select the cell next to the checkbox using some kind of "offset" property? I am completely stumped!
Thanks for your help in advance.

Use Form Controls instead of ActiveX Controls for Check Box.
Following code will not be work with check box from ActiveX Controls. Also, you need to assign macro to the checkbox, simply trying to run this code from VBEditor will give error.
Assuming all the checkboxes are on same sheet, select all your checkboxes and assign them same macro, something like this
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
MsgBox shp.Name 'Name
MsgBox shp.TopLeftCell.Offset(1).Address ' 1 Rows below checkbox
ActiveSheet.Range(shp.ControlFormat.LinkedCell).Select ' Select linked cell.
Set shp = Nothing
End Sub
here Application.Caller helps VBA to identify which checkbox is being clicked.

Related

Checkbox placement issue while filtering data in protected worksheet excel

I have a protected worksheet, with cell protection as both Locked and Hidden. In first column I have Check Box (Form Control) against each row.
Problem is when I try to filter data, there is a extra check box in last row.
I tried linking cell to check box and enabled option 'Move but don't size with cells' but still behavior is same. Is there a way to resolve this?
well seems like checkboxes are bad guys
but there's a prison even for them
make sure your sheet has at least one cell that calculates
make sure each checkbox is completely inside its cell, i.e. its
borders (which appear at selecting it) must entirely fall within the cell
where you chose it to be
set your sheet calculation as "automatic" (Formulas->Calculation Option-> Automatic)
place the following code in the sheet code pane
Private Sub Worksheet_Calculate()
Dim shp As Shape
For Each shp In Me.Shapes
shp.Visible = shp.TopLeftCell.EntireRow.Height <> 0
Next
End Sub

How to select an ActiveX Option/Radio Buttons in Form Controls

I have a Form Control with ActiveX Radio/Option Buttons in it.
The Form Control name is Side and contains Option/Radio Buttons with names xOption, oOption, and randomSide.
How would I be able to create a Macro that would allow me to set the radio buttons to a certain value upon opening the workBook. Recording a Macro of me clicking options results in a blank Macro. I've Already tried:
ActiveSheet.Shapes.Range(Array("Side")).Select
ActiveSheet.Shapes.Range("xOption").OLEFormat.Object.Value = 1
But this gives me error 1004 and other codes give me error 91. I'm really new to VBA so if I look stupid you know why.
Try something like this, using Worksheets instead of ActiveSheet:
Private Sub Workbook_Open()
Worksheets("your sheet name here").OLEObjects("xOption").Object.Value = 1
End Sub
As you want it to be selected after opening the sheet. Place this on ThisWorkbook.
You may try something like this...
ActiveSheet.OLEObjects("xOption").Object.Value = 1

Word VBA: Accessing clicked ActiveX Checkbox

I have multiple ActiveX checkboxes in my document in a table. They're all calling the same function.
With Selection.Cells(1).RowIndex and Selection.Cells(1).ColumnIndex I can find out the table cell of the checkbox.
Is it possible that I can get the value of the clicked checkbox aswell?
I could only find this code: ActiveDocument.Shapes(1).OLEFormat.Object.Value. But this code is referencing an individual checkbox.
I need to reference the checkbox that was just clicked.
Is that possible and if yes how?
Try something like:
Dim c As Cell
Set c = Selection.Cells(1)
Debug.Print c.Range.InlineShapes(1).OLEFormat.Object.Value

Clear contents of combo box

I have an invoice spreadsheet with comboboxes to select a product which fills out the product number and name in the invoice. Then I have a clear button that clears the information out to do another invoice, but the combo box and the linked cells don't get cleared. I tried adding the code ComboBox2.Clear or ComboBox2.Value="", or DropDown2.Clear, but I keep getting a run-time 424 error object required. What am I doing wrong.
Sub ClearIncoive()
ClearIncoive Macro
'Clear the invoice
Range("G6:G9,F16,G16:H16,F17,G17:H17,F18:H28").Select
Range("F18").Activate
Selection.ClearContents
ComboBox2.Clear
End Sub
Any help?
I played around with a few things and got the same error you did, so I figured I was on the right track. The combobox I created, with Excel 2010, was a Form Control. I then created another combobox using the ActiveX Control and used
Sheets(1).ComboBox2.Value = ""
Note that I set the contents of the combobox to A1:A8, which contained the data for the combobox.

Manipulating excel "autoshapes" with VBA

I am trying to write a macro in VBA (Excel) that is assigned to a Checkbox. Whenever the checkbox is clicked, an "autoshape" will change its "order" from "Send to Back" to "Send to Front".
Basically, I am trying to create a dashboard with multiple panels, so that users can access information without moving between sheets. Each panel will have a rectangular autoshape as its background and the components of the panel will be "grouped" within the autoshape.
Can this be done? I would greatly appreciate any ideas into writing the code.
Thanks,
I'm not quite following your larger goal, but in order to bring a shape to the front use this:
If MyCheckBox.Value = True Then
MySheetName.Shapes("MyShapeName").ZOrder msoBringToFront
End If
You should select your desired ZOrder movement from the MsoZOrderCmd enumeration and your code should be in the Change event routine for your checkbox control.
EDIT:
You could also refer to the shape by its index number. For example:
MySheetName.Shapes(0).ZOrder msoBringToFront
Also, to get the name of a shape, either click it and look in the Name Box in the upper left corner of Excel (below the toolbars), or iterate through all the shapes like so:
Sub Macro1()
Dim MyShape As Shape
For Each MyShape In Sheet1.Shapes
Debug.Print MyShape.Name
Next MyShape
End Sub