Forgive my ignorance (newby and little knowledge of VBA)...
I have developed some macros that are attached to buttons, and working in one worksheet in a workbook. The macros perform various jobs on a calendar. There is one calendar for each of 10 bedrooms in the wing of a hospital.
I now want to make identical worksheets with the same buttons and macros for each bedroom i.e. 10 worksheets.
But try as I might I cant get the macros to work in the other worksheets.
The macros are in the VBA code editor for the first worksheet (Bed1). I have copied the code into the "This Workbook" page within the VBA editor - but that had no effect, other than to stop them working at all.
This is a typical macro:
'============================================
Private Sub Prevw1_Click()
'============================================
' DAILY PATIENT TIMETABLE
' PRINT PREVIEW
'============================================
ActiveSheet.Select
ActiveSheet.AutoFilterMode = False
Range("_Daily").Select
ActiveSheet.PageSetup.PrintArea = "_Daily"
'
Call page_SetUp
'
' Variations for page setup
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(1.5)
.RightMargin = Application.InchesToPoints(0.9)
.Zoom = 75
End With
ActiveSheet.PrintPreview
ActiveSheet.PageSetup.PrintArea = ""
Range("H126, H126").Select
End Sub
Q. What have I done wrong that makes this only work in the Bed1 worksheet where it was developed first?
Kind regards
Russ
Take the code out of the ThisWorkbook module and put it in a normal code module. In Design Mode, in the Excel window (not VBE), right-click the button and do Assign Macro, then choose the macro "Prevw1_Click". That should work. You'll have to assign the macro to each button, or you could simply copy/paste the button to the other sheets.
If your button is an ActiveX Control, then I think you may need to have the subroutine for each button in the worksheet where the button resides. So, each worksheet may have an activeX command button called "CommandButton1", then each Worksheet code module should have a subroutine like:
Sub CommandButton1_Click()
Call ClickTheButton
End Sub
You will basically put all of this same code in each of the 10 worksheet code modules. Then, rename your routine in the ordinary code module, like:
Private Sub ClickTheButton()
'============================================
' DAILY PATIENT TIMETABLE
' PRINT PREVIEW
'============================================
ActiveSheet.Select
ActiveSheet.AutoFilterMode = False
Range("_Daily").Select
ActiveSheet.PageSetup.PrintArea = "_Daily"
'
Call page_SetUp
'
' Variations for page setup
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(1.5)
.RightMargin = Application.InchesToPoints(0.9)
.Zoom = 75
End With
ActiveSheet.PrintPreview
ActiveSheet.PageSetup.PrintArea = ""
Range("H126, H126").Select
End Sub
The reason I would do this, instead of copying the existing macro to each of 10 worksheets is simple: If you ever need to modify your subroutine, you only need to modify it in one place. Likewise, if you add a new worksheet(s) you need only copy 3 lines of code instead of 20. It's just easier to maintain this way, since each sheet's button is calling the same code, each sheet's button should just have a simple sub that calls the "main" procedure.
Related
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")...
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
I'm a VERY new learner to VBA trying to decypher functions and build an interactive file. With help from one user here (who will certainly know who he is ;)), I could learn quite a few things.
Now I'm stuck at one ridiculous piece of code : I want to 1/unhide a sheet, 2/go to that sheet and 3/re-hide the sheet when the user selects the "back to the start page-button".
So I made this code :
Sub FRtoEN()
'
' FRtoEN Macro
' Emmène au Glossaire FR ==> EN
'
Sheets("synthèse_FR&EN").Visible = True
Sheets("Synthèse_FR&EN").Select
End Sub
and it works well. But I cannot find out how to tell excel in VBA-language that I want it to re-hide the tab once the user is done and clicks the exit button.
Could you help me?
Ferndiando's answer is brilliant when you want to have one button where you first show the hidden sheet and next time you click on the same button it hide the same sheet.
Making one button to show a sheet and another button to hide the same sheet, do the following;
In the first button you will make the code visible:
Sub FRtoEN()
'
' FRtoEN Macro
' Emmène au Glossaire FR ==> EN
'
Sheets("synthèse_FR&EN").Visible = True
Sheets("Synthèse_FR&EN").Activate
End Sub
In the second button that which will take the user back to the "Main Page" you can add this code:
Sub StartPage()
Sheets("Start Page").Activate 'First go to Start page
Sheets("synthèse_FR&EN").Visible = False 'Then hide the sheet they currently visited, that makes the experience a little bit more "working in background"
End Sub
If I assume you use this "back to the start page-button" for several sheets, you could hide other sheets too every time someone goes to start page.
Sub StartPage()
Sheets("Start Page").Activate 'First go to Start page
Sheets("synthèse_FR&EN").Visible = False
Sheets("synthèse_FR&DE").Visible = False 'Example 1 - No matter which sheet you visit, it will hide this sheets.
Sheets("synthèse_FR&SP").Visible = False 'Example 2 - No matter which sheet you visit, it will hide this sheets.
End Sub
If you want the code to perform things on hidden sheets, while they still are hidden for the user (for example background filtering/calculations/copy data etc..) this will give the user a smooth experience:
Sub StartPage()
Application.ScreenUpdating = False 'Turn of all visual updates the macro does. Macro works in background without showing every step visually in Excel.
Sheets("synthèse_FR&EN").Visible = True 'Unhide the sheet you want to work at.
'Do some filtering stuff // copy stuff
Sheets("synthèse_FR&EN").Visible = False 'Re-hide the sheet again.
Application.ScreenUpdating = False 'Turn ON all visual updates the macro does. Macro now works and shows every step visually in Excel.
End Sub
:)
If i understood your question you can use this code:
Sub myButton()
'Hide and Show Sheet2 with same button
' you can change the sheet name as you prefer
If (Sheets("Sheet2").Visible) Then ' control if the sheet is visible
Sheets("Sheet2").Visible = False ' hide sheet2 because before was showed
Sheets("Sheet1").Select ' select sheet1
Else
Sheets("Sheet2").Visible = True ' show sheet2 because before was hidden
Sheets("Sheet2").Select 'select sheet2
End If
End Sub
I hope this help you.
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
My problem are command buttons that fail to respond when I've selected multiple sheets in a workbook. The workbook I'm testing here is meant only to study and troubleshoot this problem, which I originally found in a much more complicated workbook.
My workbook has a total of 5 worksheets. There are four ActiveX command buttons on sheet1. These four buttons launches code to either select several worksheets together or just sheet1.
After selecting multiple sheet together, only the last button clicked actually can be clicked again, the rest of the buttons on the sheet1 don't respond anymore, like they're disabled or something. Once I manually deselect the sheets so that just sheet1 is selected, the controls start working normally.
Weird.. I think it must be some sort of bug in Excel. I also can replicate the problem if I manually select multiple worksheets rather than let the code do it.
Some of my findings so far...
It doesn't seem to matter how many sheets I select so long as its two or more.
VBA code selection or manual SHIFT-CLICK doesn't matter.
The last button activate still runs once the other buttons gets locked up.
I only get this with Excel 2010, Excel 2007 didn't have this problem.
I've replicated the problem in a isolated workbook, so I don't think this is corruption issue.
The 4 command buttons execute the functions shown below. Each button marks adjacent cells if the code runs. I put a 1 second delay to verify clicking a button twice in a row was working.
CMD 1: Select sheet1 only
CMD 2: Select sheet1 only
CMD 3: Select sheet1 and sheet2
CMD 4: Select sheet1 through sheet4 via sub routine in module1
Here is my code attached to sheet1....
Option Explicit
Private Sub CommandButton1_Click()
Call MarkCmdsAsInactive
Me.Select
Call WaitSeconds(1)
Range("E6").Value = "CMD 1 Works"
End Sub
Private Sub CommandButton2_Click()
Call MarkCmdsAsInactive
Me.Select
Call WaitSeconds(1)
Range("E10").Value = "CMD 2 Works"
End Sub
Private Sub CommandButton3_Click()
Call MarkCmdsAsInactive
Sheets(Array("Sheet1", "Sheet2")).Select
Call WaitSeconds(1)
Range("E14").Value = "CMD 3 Works"
End Sub
Private Sub CommandButton4_Click()
Call MarkCmdsAsInactive
Call SelectSomeSheets
Call WaitSeconds(1)
Range("E18").Value = "CMD 4 Works"
End Sub
Private Sub MarkCmdsAsInactive()
Range("E6").Value = "Inactive"
Range("E10").Value = "Inactive"
Range("E14").Value = "Inactive"
Range("E18").Value = "Inactive"
End Sub
Private Sub WaitSeconds(waitInSeconds As Variant)
Dim newHour As Variant
Dim newMinute As Variant
Dim newSecond As Variant
Dim waitTime As Variant
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + waitInSeconds
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
End Sub
In module1 I have...
Option Explicit
Sub SelectSomeSheets()
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
End Sub
Update 2012-10-09
Here is a simple way to replicate this bug in Excel 2010...
Place 4 command buttons on sheet1.
Copy the code below to sheet1.
Put breakpoints on each of the "End Sub" statements.
Try clicking the buttons with just sheet1 selected. All buttons launch routines.
SHIFT-CLICK to select a group of sheets.
Try buttons again with the sheet group selected. Only the last used button works.
Private Sub CommandButton1_Click()
End Sub
Private Sub CommandButton2_Click()
End Sub
Private Sub CommandButton3_Click()
End Sub
Private Sub CommandButton4_Click()
End Sub
I guess it has to do with the "scope" of the ActiveX buttons (i.e. scope is sheet1 and not sheet1+sheet2+...). It seems that the selection of multiple sheets deactivates command buttons in sheet 1, even if sheet 1 remains the "active" sheet. because the ActiveX components are private to sheet1.
As a workaround, I moved your Sub CommandButtonX_Click to Module 1, removed the Private keyword, created a custom ribbon MyTools with the 4 Sub's as selectable elements. This way I moved the visibility of the sub's from sheet level to application level and all works.
Of course I had also to change Me.Select into Sheets("Sheet1").Select (allthough I hate hard codings like this ....)
Hope that helps ...