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"
Related
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...
I have created a messagebox and coded so that the excel goes to the specific range where the button exists. But I do not know how to code so that the macro automatically clicks the button.
Sub Start()
Range("A4").Select
< >
End Sub
Sub MessageBox()
MsgBox "Hi" & vbCrLf & "Professor", _
vbInformation, "Greetings"
End Sub
I need something < > in this space but the process to automatically get linked.
Anyone have an idea?
In your example it looks like you have the name of the macro, and if that's the case it should be easy to just call it. See below
Sub Start()
Range("A4").Select
Call MessageBox
End Sub
Sub MessageBox()
MsgBox "Hi" & vbCrLf & "Professor", vbInformation, "Greetings"
End Sub
However, I'm guessing that doesn't help you because what you're asking doesn't truly make sense. I'm going to guess that maybe by MsgBox, you actually mean Form or Command Button. These look similar but they are distinctly different.
A MsgBoxis more of an alert to the user with the option to capture a small amount of information back (i.e. yes/no/ok/cancel etc). Clicking on a MsgBoxwill never directly launch another macro. An ANSWER to a MsgBox(i.e yes/no) MIGHT determine if another Macro is run based on an if-statement, or if the next line of code is simply just to execute another macro.
If you have a macro running distinctly based on a click, the button is likely a Form or an active X command Button. You need to figure out what macro this object is executing and then you just need to include this in your code. Google "how to see what macro a form button runs in VBA" or if it's an active X button, right click on it and hit "View Code". Both of these approaches should drive you to a macro name such as CommandButton1_Click.
Unfortunately, you might have to get more fancy as if it's a sheet Commandbutton1_Click you may have to make it public. Hopefully you can insert a call that code in your current macro.
Hope that helps.
Okay so I'm using Excel and trying to get a cell address based off the location of the command button I'm clicking. I want to store that cell address in a variable as I click the button to be used in the next code for the user form that pops up as a result of clicking that button. Ive looked at this link: Store location of cell address to variable in VBA , which is similar to what I'm trying to do but I cant seem to get it to work with the ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address that gives an address based off the top left corner of the command button. I'm trying to do it this way so I can use the same code from the next part for every similar button I add without having to manually change the code based off where the button is.
I'm pretty new when it comes to Excel and VBA so any help would be appreciated.
Edit:
I tried adding the code but I'm getting a Runtime error '1004' Unable to get Buttons property of the Worksheet class. I have this
Sub Button2_Click()
Dim BtRng As Range
Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
MsgBox Btnrng.Address
Btnrng.Offset(3, 3) = "Hello"
formAddBill.Show
End Sub
This button is also popping up a userform for information input. the error is happening on Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
If you are using application.caller then I assume your buttons are from the forms.control toolbox. This makes life a lot easier.
Assign each button with this code example:
Sub Rng_Butn_Clicked()
Dim BtRng As Range
Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
MsgBox Btnrng.Address
Btnrng.Offset(3, 3) = "Hello"
End Sub
If you are using command buttons from the activex toolbar, it can get a bit confusing. Doing it one at a time is easy enough
Private Sub CommandButton1_Click()
Me.CommandButton1.TopLeftCell.Offset(0, 1) = "Hi"
Me.CommandButton1.TopLeftCell.Offset(, 2).Interior.Color = vbBlue
MsgBox Me.CommandButton1.TopLeftCell.Address
End Sub
But assigning one code to many activex command buttons is not as easy, I would prefer to use the Forms Buttons if you want to assign one code to many buttons.
Edit: If you are getting this error
You probably put the code in a command button. Please reread my answer.
This is what I am referring to:Form Controls & ActiveX Controls
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.
i need to click a button(FormControl) on an excel sheet and run the macro assigned to it, through VBA code.
I tried what was suggested here
https://stackoverflow.com/questions/130166/clicking-command-button-from-other-workbook
but it didn't work for me.
Is there any other way to do this ??
Fairly easy method for doing this, even if it's not really documented.
CommandButton1.Value = True
Did you actually try? Because you want exaclty what is suggested there.
Application.Run Activesheet.Shapes(1).OnAction
If in a worksheet. I used this in the past and it worked!
Sheet1.CommandButton1_Click
else
Sheets("Sheet Name").CommandButton1_Click
In my case, I needed to add a button, say on Sheet1, to call the code of CommandButton1 on, say, Sheet2. First I had to delete the 'Private' statement that was in front of CommandButton1_Click(). Then I had to call Worksheets("Sheet2").Activate before calling Worksheets("Sheet2").CommandButton1_Click. Then it worked. You can then add Worksheets("Sheet1").Activate to get you back to your original sheet. So:
Private Sub CommandButton1_Click()
Worksheets("Sheet2").Activate
Worksheets("Sheet2").CommandButton1_Click
Worksheets("Sheet1").Activate
End Sub