Issue with Z-Order - excel

I have multiple shapes and checkboxes in a spreadsheet. I want to create a function that places a particular shape to front (a higher Z-order than its peers) when its corresponding checkbox is clicked. This is the code that I currently have:
Sub CheckBox3_Click()
If CheckBox3.Value = True Then
Sheet1.Shapes("blueoval").ZOrder msoBringToFront
End If
End Sub
I get Run-time Error '424' whenever I run this code. I am new to VBA for excel, so any help would be greatly appreciated. What's wrong with this code? What's missing? etc.. Thanks!

Your code works for me.
Check that:
Your checkbox is an ActiveX control and not a Form control.
The checkbox name is CheckBox3.
You have a shape on Sheet1 called blueoval.
The sheet name with the blue oval is actually Sheet1 (check this in the Visual Basic Editor).
In the Visual Basic Editor, select Tools > References and make sure there are no references marked MISSING.
Your code is on the worksheet where the checkbox is and not in a separate module.

Related

Close Excel file after following link added to a Shape

I need help with a project that I want to have something like a "Main Menu" Sheet. I added some shapes to act like buttons linking them with other excel files. What I needed the shapes to do is after clicking them, close this "Main Menu" Sheet as it is only needed to choose what type of sheet I need to use. So the idea is:
I have this "main-menu.xlsm" opened in the first place. Inside it's first sheet named "Main" I have 3 shapes linking to three other excel files "sheet1.xlsx", "sheet2.xlsx"and "sheet3.xlsx". The linking works just fine, but I end up with two Excel's opened, I need to make it so after I click the shape that opens "sheet1.xlsx" it also closes the "main-menu.xlsm".
What I've come across is adding this code to Sheet1(Main):
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveWorkbook.Saved = True
Workbooks("main-menu.xlsm").Close SaveChanges:=False
End Sub
But unfortunately it is not working with shapes. If instead of a shape I create a Text, link it to one of the files I need and click it, then the code works just fine. Anyone can help me out with this? Thank you for your time and patience reading this and helping if you can.
Try the next approach, please:
Remove the existing link, but copy its reference (firstly, only for one shape...);
Create the next Sub in a standard module
Sub MyShape1_Click()
Workbooks.Open ("Full name of the previously linked workbook")
ThisWorkbook.Close SaveChanges:=False
End Sub
You need to name each Sub in a way to be relevant when you need to assign it to the appropriate shape.
Right click on your shape(s), choose "Assign Macro...", select 'Macros in:' ThisWorkbook and look for the above Sub. Select it and click `OK'.
Enjoy what it will do...

Is there a way for excel to recognize which shape having macro assigned was clicked?

I have added a few shapes to my worksheet. I have assigned macros to them. But I had to create separate macros for all.
Is there a way for excel to recognize which shape was clicked and based on its text value find the cell to copy. That way I need not have to create separate macros for each shape.
I have tried searching for it online, found a few things which I tried but did not work.
Any help would be greatly appreciated.
Yes, the Application.Caller property can be used here.
Option Explicit
Public Sub MyShapeMacro_Click()
Dim CalledByShape As Shape
Set CalledByShape = ActiveSheet.Shapes(Application.Caller)
'CalledByShape is your shape
Debug.Print CalledByShape.OLEFormat.Object.Text 'returns the text of the shape
End Sub
Link this macro MyShapeMacro_Click to all your shapes.
Note this macro cannot be started in the VBA editor (it will throw an exception) it can only be run by clicking the shape.

Userforms causing excel 2013 to crash

I have the following issue:
I create a userform in excel 2013 and add for starters one text box to show the date and right underneath a combobox which is reflecting a data validation list.
As soon as i write the code
Private Sub UserForm_Initialize()
Me.tbDate = Date
'fill combobox
For Each cell In [cartridges]
Me.cmbCartridges.AddItem cell
Next cell
End Sub
Can anyone help pretty please?
Thanks in advance
The code should work. I am running Excel 2013 as well and just tested your code with no problems.
Two things to check.
1
Make sure that the defined name is spelled exactly the same way in the VBA code as it is in the Name Manger.
2
Make sure that the defined name does not evaluate to an error.

Combobox with macro on selecting items

I'm very new to VBA and i searched and searched on Google, but can't find an example which deals with my problem.
I got a list of names which I want to put inside a selectable dropdownlist. When i click their name I want to run a different macro i made with their name on.
I tried a lot of things yesterday, but everytime it only succed me to assign 1 macro which was called no matter which name i pressed.
I think the solution is pretty simple, but i really got no clue how to do this the most simple way. So hopefully any of you got a link to a simple tutorial or can explain it to me in steps.
Thanks in advance
EDIT:
I got 2 names.
Birgitte = A:1
Thomas = A:2
I got a form comboxbox where both names are in.
When i press Birgitte i want a macro called BS_Opgave() to run and when i pres Thomas i want Macro TR_Opgave to run.
My problem is I'm not sure how to connect the combox selection to a Macro in the VBA editor. I'm acutyally confused about everything in the editor about comboxing.
Paste this code in a module. The Right Click on the Combobox and assign the macro DropDown1_Change to it :) And you are done.
Option Explicit
Sub DropDown1_Change()
With ThisWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat
Select Case .List(.Value)
Case "Birgitte": BS_Opgave
Case "Thomas": TR_Opgave
End Select
End With
End Sub
Sub BS_Opgave()
MsgBox "You selected Birgitte"
End Sub
Sub TR_Opgave()
MsgBox "You selected Thomas"
End Sub
ASSUMPTIONS
I am assuming the following
The name of the combobox is "Drop Down 1"
The combobox is in "Sheet1"

How to get the index of a combo box in Excel spreadsheet by using VBA?

I have an Excel 2003 file. In the first sheet, I put a combo box. After that, I assign a macro to handle the change event. The macro is located a module in the VB Editor's Project Explorer box. In the macro, I want to write some lines to get the index of the combo box's selected item index. What should I write?
I wrote the line
If ActiveSheet.cbFilter.Index = 1 Then
but it kept raising error (the name of the combo box is "cbFilter"). The error message is "Object doesn't support this property of method". Please help me.
Many thanks in advance,
Haris
Try removing the "ActiveSheet." part of the statement, my guessing is the combo is already inside that sheet.
Source: Experience
Regards
have you added items to your combo list?
try these pieces of code in your debugger: first call Preload() once, then select anything from the combo, then run Readout() .... this should give you a hint.
Sub Preload()
ActiveSheet.ComboBox1.AddItem "111"
ActiveSheet.ComboBox1.AddItem "222"
ActiveSheet.ComboBox1.AddItem "333"
End Sub
Sub ReadOut()
ActiveSheet.[A1] = ActiveSheet.ComboBox1.ListIndex
ActiveSheet.[A2] = ActiveSheet.ComboBox1
End Sub
also you should check that you have created a reference to MSForms 2.0 Object library (FM20.DLL - or similar)
EDIT:
I just tested the case of empty Combo ... index will be -1
Try this. If you look into Object brower cbFilter object is property of sheet1 and can be referred as below from module.
MsgBox Sheet1.cbFilter.ListIndex
If Sheet1.cbFilter.ListIndex = 1 Then

Resources