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
Related
I have a code which looks like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Me.sheets("WAM Data").Range("BY5:HW35").Interior.Color = Me.sheets("WAM
Exception").Range("BO7:HM37").Interior.Color
End Sub
But its giving error in code.
What I want to do is, change the cell formatting (color) of "BY5 to HW35" as "BO7:HM37".
If anyone could help that would be great.
You can delete me. and use only Sheets..., or use ActiveWorkbook.Sheets.. to refer to the activeworkbook, or ThisWorkbook.Sheets... to refer to the workbook the macro is run from, or Workbooks("name").Sheets... to choose whichever workbook you want from the ones you have open.
However, your macro will just run on first click of a mouse, regardless where that happens on your spreadsheet, with no conditions add to it... is that what you want to do with your code?
I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub
Good day,
I am using Excel 2013 and I would like to hide and unhide my Sheets as I work with them. I spent some time Googling around and found plenty of ancient posts on forums about adding VBA to modules, but that's not quite what I'm looking for.
On a main page where I spend most of my time using data, I have a button that shows a UserForm with a list of sheets in a ListBox. I choose the Sheet from the ListBox, hit OK, and it runs the following;
Private Sub OKButton_Click()
ThisWorkbook.Sheets(JobListBox.value).Visible = xlSheetVisible
ThisWorkbook.Sheets(JobListBox.value).Activate
Unload Me
End Sub
I would like it so when I have my new sheets created via VBA, I can populate the sheet with the following subroutine;
Private Sub Worksheet_Deactivate()
Me.Visible = xlSheetVeryHidden
End Sub
If anyone can let me know how I can make a Subroutine to insert this code into my sheets, I would greatly appreciate it.
PS: My fallback method is to, of course, just copy/paste the code manually... But I would prefer to automate it if possible.
Instead of adding the same code to each sheet, since they are all inside the workbook and you are really trying to execute a hide once any sheet in the workbook is deactivated, put this in the code for ThisWorkbook.
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Sh.Visible = xlSheetVeryHidden
End Sub
You might be able to use more workbook events with your type of project.
Here is a list of the workbook events.
If you want to exclude your main page from this, you can modify this by adding an IF statement:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
IF Sh.Name <> "Main" Then
Sh.Visible = xlSheetVeryHidden
End If
End Sub
The main line of thinking being that if you have to put the same code into more than one object, let alone ALL of them, you are repeating yourself.
Check out the concept of DRY, or "Don't Repeat Yourself", unless you like it WET, "We Enjoy Typing". or "Write Everything Twice". Even if it's just going to be created programmatically, a chunk of code shouldn't have to exist in all your sheets exactly the same when you can have one piece of code have an incoming argument that is a worksheet.
This way, if you have to make a change to its behavior, you do it once. It's easily testable and less to keep track of or modify later.
So if you find yourself having to use the same code over and over, look to the parent object and try to find a way to pass the changing object or variable through as an argument to a singular piece of code, or module.
Also, this is probably why you aren't finding any results on inserting the same code into every sheet. It's not a good practice
Article on DRY
I have written a simple macro to clear the contents of column A. My spreadsheet has three sheets called "Input Screen", "Proposal Database", and "Rankings". When I am in the "Proposal database" screen the macro works fine. Howveer, the macro does nto work when it is run from other screens (even though there is nor error with the code). Why is this? Any help will eb much appreciated! The code is as follows:
Sub Clear_Contents_Column_A()
With Worksheets("Proposal Database")
Range("A3", Range("A3").End(xlDown)).ClearContents
End With
End Sub
Thanks,
Ollie
You need to use . before Range. It will specify that Range belongs to certain Worksheet:
Sub Clear_Contents_Column_A()
With Worksheets("Proposal Database")
.Range("A3", .Range("A3").End(xlDown)).ClearContents
End With
End Sub
You're question can be read in different ways. I suspect that #simoco has provided the answer you are looking for, but if you are asking how to use the macro to clear column A in your other worksheets, then you need to change the worksheet name in your macro. You're instructing the macro to run on Sheet ("Proposal Database"). Unless you change the name of the sheet it won't run on other worksheets. You could change
With Worksheets("Proposal Database")
to
With ActiveSheet
if you want the macro to run from any worksheet in the workbook.
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"