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")...
Related
I am trying to automate data with a master wookbook. When I open the workbook I want a msg box to appear and clear the contents of specific columns in tables in one of the worksheets. I keep getting the "Application-defined or object-defined error". This is the code in my "This workbook" section:
Option Explicit
Sub Workbook_Open()
Dim answer As Integer
answer = MsgBox("Do you want to clear the totals?", vbYesNo + vbQuestion, "Clear Totals")
If answer = vbYes Then
Call Sheet1.ClearContents_1
End If
End Sub
This is my Sheet1 code:
Sub ClearContents_1()
Call Loop_Clear_C
Call Loop_Clear_M
Call Clear_S
End Sub
Sub Loop_Clear_C()
For i = 1 To Range("UserTable79").Rows.Count
Range("UserTable79[Total]")(i) = 0
Next i
End Sub
Sub Loop_Clear_M()
For i = 1 To Range("ServiceTable79").Rows.Count
Range("ServiceTable79[Total]")(i) = 0
Next i
End Sub
Sub Clear_S()
Range("TotalTable79[Actual Total]").ClearContents
End Sub
They worked separately but not together. The msg box comes up but doesn't run the Sheet1 code. How do I call upon this sheet code?
Edit: The Sheet1 code no longer works either.
Often the "Application-defined or object-defined error" message comes up when the range that you're referring to doesn't exist. If you click on Sheet1 to activate it and then try to use the Immediate panel to perform an operation on the ranges you're referring to (e.g. try entering Range("UserTable79").Select) do you get an error?
Explicitly specifying the worksheet when calling the function from the "ThisWorkbook" section (e.g. if the worksheet is named "SheetName" then specifying Call ThisWorkbook.Sheets("SheetName").ClearContents_1 rather than Call Sheet1.ClearContents_1) can help.
Also - note that you can clear the entire table ranges or entire columns in Loop_Clear_C and Loop_Clear_M with ClearContents or EntireColumn.ClearContents if that's easier.
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
I was wondering how can i one use VBA/macros to lock certain excel cells that are selected/highlighted by the user.
The code im using right now is locking the entire sheet.
Sub Macro4()
'
' Macro4 Macro
'
'
Worksheets("Sheet1").Activate
ActiveSheet.Unprotect
Cells.Select
Selection.Locked = True
ActiveSheet.Protect
End Sub
Any ideas on what im doing wrong?
Thank you for your time.
If you want to perform any actions on the selected cell(s) every time a new selection occurs, you should rely on the code being triggered when this happens:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Locked = True
End Sub
This inside the file with the code for the given sheet; that is, if you want to consider Sheet1, the file where you have to write this code is: Microsoft Excel Objects/Sheet1 (Sheet1).
UPDATE AFTER YOUR COMMENT
Sub Button1_Click()
Selection.Locked = True
End Sub
This code locks all the cells selected when the Button1 is clicked.
i've created a macro to create a button when the sheet is activated. then i called it using the codes below: But everytime i go to different worksheet then back again to the sheet containing this button the macro does its job. i just want the macro to work when i clicked the button
Private Sub Worksheet_Activate()
Call sortData
End Sub
here's the code for macro that i've created:
Sub sortData()
'
'
'
'
ActiveSheet.Buttons.Add(689.25, 59.25, 133.5, 30).Select
Selection.OnAction = "sortData"
Selection.Characters.Text = "Sort Data"
With Selection.Characters(Start:=1, Length:=28).Font
.Name = "Times New Roman"
.FontStyle = "Bold"
.Size = 12
End With
Range("A1").Select
End Sub
now my problem is where will i put the codes shown below when this button is click? or simply how can i make this button worked?? i tried to put the codes in the same sheet where i called the macro but it is not working.
Sub sortData_Click() 'did i call the button right? but it is not working when i us it
'codes here
End Sub
in the VBA Editor, click insert new module (or you can just put this in the same module where your sortData is) then write this code into your new module:
Sub procedureName() 'procedure name is exactly the same as what you named in Selection.OnAction = "sortData"
'codes here
End Sub
what you actually done is making the sortData() call itself because you have set the Selection.OnAction = "sortData" ->>> change this name to whatever procedure name you want to perform
I'm curious as to whether it's possible to pass the protection status of an excel worksheet to a cell of that worksheet.
e.g.
Sheet1 is locked for editing...cell A1 would be programmed to say "locked"
Sheet1 is unlocked...cell A1 would say "unlocked".
A button on the sheet would be used to toggle worksheet protection on and off.
My sheet will be locked upon opening using a workbook_open event.
This is for a sheet where I don't want the formulae getting all mucked up upon use, but where full access might be required. Its more as a reminder to the user that they are in "Unlocked" Mode so to be extra careful.
Is using VBA a foregone conclusion?
I'm a VBA noob but don't mind using code as a solution for this
Any thoughts or suggestions welcome
You could use code in an ActiveX button on Sheet1 to do this simply
Const strPAss = "test"
Private Sub CommandButton1_Click()
If ActiveSheet.ProtectContents Then
ActiveSheet.Unprotect strPAss
[a1].Value = "unlocked"
Else
[a1].Value = "locked"
ActiveSheet.Protect strPAss
End If
End Sub
Put this in the worksheet's code module, which will place a reminder in the Status Bar (this avoids needing to lock/unlock the sheet in order to write the status in to cell A1).
Put this in Sheet1 code module. The macro will execute every time sheet1 is activated.
Private Sub Worksheet_Activate()
If ActiveSheet.ProtectContents then
Application.StatusBar = "This sheet is protected"
Else:
Application.StatusBar = "This sheet is unprotected"
End If
End Sub
Private Sub Worksheet_Deactivate()
Application.StatusBar = False
End Sub
To protect/unprotect the worksheet you could add this to an Insert>Module. Then attach these macros to separate command buttons, or run from the Developer>Macros ribbon.
Const myPassword as String = "password" '<-- replace "password" with your password
Sub Sht1Protect()
Sheet1.Protect myPassword
End Sub
Sub Sht1Unprotect()
Sheet1.Unprotect myPassword
End Sub
To ensure the sheet is always protected when you close the file, insert this in the Workbook code module
Private Sub Workbook_Close()
Sht1Protect
End Sub
You may need additional handling to control whether the file is saved/not saved etc.