VBA Run ToggleButton Sub from an Active Worksheet in a Modul - excel

I have been searching for a solution for the following problem for days now, but I just can't figure it out.
I have a Excel Workbook with multible sheets. Some sheets contain code and there are also multible modules with code. Keep in mind, that the sheet names, postition and quantity can be changed by the end user.
In the sheet with the code.name "Tabelle 1" I have the following code:
Sub Reset_ToggleButton1()
If ToggleButton1.Value = True Then
ToggleButton1.Value = False
End If
End Sub
As far as I found out, I can only activate ToggleButtons with code in their respective sheet. If this is not true, this might be a possible solution to my problem.
Further I would like to call the sub Reset_ToggleButton1() from a modul. But, as there can be multible copies of the sheet with the toggle button and the respective code, I would like to referece the sub in the active sheet.
The following code worked, but only for the sheet named.
Sub test()
Application.Run "Tabelle1.Reset_ToggleButton1"
End Sub
I think what I need is to replace the "Tabelle1" with the code name of the active worksheet. I know, I can get the Code.Name with of the active worksheet with the following code:
Dim SheetCode As String
SheetCode = ActiveSheet.CodeName
But I don't know how to insert it into the Sub test() from above.
Your help is very much appreciated!
Best wishes
Anne

you can use
ActiveSheet.Reset_ToggleButton1
or
Worksheets("Tabelle1").Reset_ToggleButton1
to run it.

Worthy of mention, you can specify worksheet:
Sub Test(WS As Worksheet)
WS.Reset_ToggleButton1
End Sub

Related

How to assign macro to button without using "active sheet" in VBA?

I am trying to change the macro assigned to a button from a different sheet than where the button is. This is how far I've gotten:
Sub Macro1()
ActiveSheet.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, the above code requres me to be on the active sheet. I have tried the following:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, this will give me an "Object doesn't support this property or method" error.
Why doesn't it work when "ActiveSheet" is replaced with "Sheet1"?
And why can't I collect the two lines of code into one line?:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).OnAction = "Macro2"
End Sub
Any help would be appreciated!
Please, simple try:
Sheet1.Shapes("Button 1").OnAction = "Macro2"
Of course, a macro named "Macro2" should exist in a standard module. If in a sheet code module, the sheet CodeName is necessary in front of the macro name ("Sheet1.Macro2")...

Keep a sheet in first position in a workbook

I have a workbook with a cover sheet and the remaining sheets are not visible. A tick-box displays tabs if ticked.
The cover sheet gets moved around in the book. I would like the cover sheet to always be in the first position.
Is there a way to do this?
Sub CheckBoxA1_Click()
Sheets("A").Visible = Not Sheets("A").Visible
End Sub
Sub CheckBox2_Click()
Sheets("B").Visible = Not Sheets("B").Visible
End Sub
The macro works well though the cover sheet extends up in different places well other sheets become visible. I would like it to be always in the first position. Is this possible?
Use the Workbook.Open event to move that particular sheet to the first position when the workbook is opened.
Add this code to the ThisWorkbook module and change Sheet1 to the codename of that sheet:
Private Sub Workbook_Open()
Sheet1.Move Before:=Me.Sheets(1)
End Sub

How to get the last sheet created in excel?

How to get the last sheet created in excel ?
I used GetSheets.Last, and it work but he found me the last sheet what it is in queued, it’s right, but if my last sheet doesn’t follow the order of the queued for example it is in the middle, the function GetSheets.Last doesn’t work.
Exist some function where the robot can found or understand which sheet has been created for last ?
Thank you very much
best regards
On the workbook, there is a NewSheet event that can help you get the sheet at the very moment it is created. The complete signature is Private Sub Workbook_NewSheet(ByVal Sh As Object)
Once you close/reopen the workbook, the information will be lost though, so you need to save the sheet's name in some cell for future use.
It can be:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Worksheets(1).Cells(1, 1).Value = Sh.Name
End Sub

Worksheet_Deactivate() Cannot Use Active Sheet functions

I have 50 datasheets in the project, and nobody remembers to run the save macro when going to another sheet. The bright idea is to use a private sub Worksheet_Deactivate to do the necessary calculations when they select another worksheet. In addition to the 50 datasheets, there are two more worksheets in the workbook for which the calculation must not run. It would be nice if the sub could be put in "Worksheets" rather than replicated 50 times in individual worksheets, but the two other worksheets need to be excluded from processing.
Problem is, the sub defaults to the deactivating worksheet (such as an unqualified "Range.Value =" in the macro code), but the active worksheet is now the worksheet being navigated TO. So any ActiveXXXXX statement directs to the wrong worksheet. Worksheet.Name is disallowed.
Datasheets are numbered 1 to 50. What is needed is a statement early in the deactivate sub similar to
If DeactivatingWorksheet(X) = "BasicInfo" Or "Constants" Then GoTo EndSub
where X is the value of the deactivating worksheet. Of course, X is known only to Excel at the moment of processing.
I can't seem to figure out how to refer to the deactivating worksheet in the macro's IF statement. Any ideas?
Use Workbook_SheetDeactivate(ByVal sh as Object) instead of Worksheet_Deactivate(). The Workbook-level event supplies the name of the sheet being departed, even though in both cases the ActiveSheet has already changed when when event fires. Use sh just like a worksheet variable - sh.Name, sh.ProtectionMode, etc.
Now you don't need 50 subs; just one. Another thing that this allows is, you can "abort" the change to the now ActiveSheet by sh.Activate to the old one (but turn off events or you'll have a lovely infinite loop).
Me also gives the old sheetname and works for the worksheet event, if you still want to go that way. Me is the old one, ActiveSheet is the new one.
If you are using Worksheet_Deactivate and this calls a subroutine in a seperate module, you can pass the name of the deactivating worksheet to the subroutine.
For instance, if your subroutine is something like:
Sub test()
ActiveSheet.Range("whatever") = "something"
ThisWorkbook.Save
End Sub
And you call it from the worksheet like:
Private Sub Worksheet_Deactivate()
Module1.test()
End Sub
You can add a parameter to the subroutine to take the worksheet name, and add a test:
Sub test(worksheetname as string)
If worksheetname <> "dontsavethistab" then
ActiveSheet.Range("whatever") = "something"
'or... you could also do:
Sheets(worksheetName).Range("Whatever") = "something"
ThisWorkbook.Save
End If
End Sub
And call it from your Worksheet_Deactivate event like:
Private Sub Worksheet_Deactivate()
Module1.test (Me.Name)
End Sub
If you wanted to get a little cleaner, instead of the worksheet name you could pass the worksheet object:
Private Sub Worksheet_Deactivate()
Module1.test(Me)
End Sub
Sub test(ws as worksheet)
If ws.name <> "dontsavethistab" then
ws.Range("Whatever") = "something"
ThisWorkbook.Save
End If
End Sub
This way you have the entire worksheet object to do with as you please in your subroutine.

Problems with obtaining modified date

I have an opened workbook called "Visor Portafolio.xls" and I want to write its last modified date on my current workbook (the one running the macro) into cell H6. I want to put this at the end of a macro that I use.
I've tried a lot of different solutions but it won't seem to work. Can anyone help me with this?
If you are in a macro:
Sub Macro1()
Range("H6").Select
ActiveCell.FormulaR1C1 = LastSaveDate()
End Sub
Function LastSaveDate()
Application.Volatile True
LastSaveDate = FileDateTime(ThisWorkbook.FullName)
End Function
base on: http://answers.microsoft.com/en-us/office/forum/office_2010-excel/insert-the-date-an-excel-workbook-was-last/c0c7335e-fc0d-43c7-b32d-215f84b452cc

Resources