Macro for formatting before print - excel

I've seen similar questions to this but haven't found a good answer. I am trying to have a macro run automatically that formats the footer date and font before printing.
This code doesn't work, but is close on the date/font formatting:
Sub Fix_Footer_Date()
ActiveSheet.PageSetup.CenterFooter = "&14&""Verdana,Bold" & Format(Now, "mmmm dd, yyyy")
End Sub
And something like this will automatically run it before it prints?
Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Your code here
End Sub
How can I combine these into one that works? Any help greatly appreciated.

First thing is that I strongly encourage you to qualify the sheet you wish to operate on versus using ActiveSheet, as ActiveSheet may not always be the sheet you desire to work with.
Second is, I fixed the syntax to get your Page Footer to work as intended. I simply recorded a macro to get the correct syntax (and adjusted it for your formula).
Sub Fix_Footer_Date()
Dim ws as Worksheet
Set ws = ThisWorkbook.Worksheets("mySheet") 'change as needed
ws.PageSetup.CenterFooter = "&""Verdana,Bold""&14" & Format(Now, "mmmm dd, yyyy")
End Sub
Then, just call the macro in your Before_Print event.
Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Fix_Footer_Date 'you could also write the code directly into this event, if you wish.
End Sub

Related

VBA moving specific worksheet before/ after selected worksheet takes too long - why?

I have simple code which is moving worksheet "Data" to after activated worksheet.
Sub Workbook_SheetActivate(ByVal Sh As Object)
Worksheets("Data").Move After:=Worksheets(Sh.Name)
Worksheets(Sh.Name).Activate
End Sub
I am using
Worksheets(Sh.Name).Activate
because without this line worksheet "Data" remains selected after the move which is not the intention.
The problem I have is when this code is run it takes about 2-3 seconds for excel to think about it before seeing result.
I don't understand why. Without chaining these 2 operations together it takes milliseconds. Could someone please explain how to improve this and why is this happening?
This should be quicker:
Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim nam As String
nam = Sh.Name
Application.EnableEvents = False
Worksheets("Data").Move After:=Worksheets(nam)
Worksheets(nam).Activate
Application.EnableEvents = True
End Sub
This happens because of recursive calling: when you use .Activate, then your Sub Workbook_SheetActivate is called again and you're stuck in an endless loop.
If you simply want to deselect the data range, you can use Cells(1,1).Select and you can directly use Sh instead of using Worksheets(Sh.Name), since they're equivalent.
So your final code would be:
Sub Workbook_SheetActivate(ByVal Sh As Object)
Worksheets("Data").Move After:=Sh
Cells(1,1).Select
End Sub
Hope this helps.
Thanks for your input guys. Turning off/on events helped to get out of the loop.
Application.EnableEvents = False
Worksheets("Data").Move After:=Sh
sh.Activate
Application.EnableEvents = True
I should have probably explained what the code was being used for in my question.
The user had 50+ Worksheets in the Workbook (please don't ask me why :)). They had one in particular ("Data") they wanted to be able to click in and out whilst working with other worksheets. So this code was simply causing the Worksheet "Data" to "follow them"
The line:
sh.Activate
Was used to get back to the Worksheet they have just clicked on as otherwise they were getting stuck on "Data" Worksheet.

VBA Run ToggleButton Sub from an Active Worksheet in a Modul

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

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.

clock in and clock out system

hi there im creating a spreadsheet to use for clocking in and out of work and have a simple wee GUI set up already. along with this i have the current time showing as soon as i start up the worksheet but it shows on every page i was wondering how to stop this?? sample code below:
Global clockOn As Boolean
Sub runClock()
Range("M15").Value = Now()
If clockOn = True Then
Application.OnTime Now + TimeValue("00:00:01"), "runClock"
End If
End Sub
Sub Auto_Open()
clockOn = True
runClock
End Sub
Sub stopClock()
clockOn = False
End Sub
on top of this i will be doing a macro to put the information onto a specific page all going well as this will be depending on the day selected from the drop down menu any help with this would be greatly appriciated :) as in maybe an if statement in the VBA code to select the correct page and leave the data there.
You need to specify the sheet that you are putting the value on; currently you only specify the range, but you don't specify the sheet. Can be done multiple ways, such as:
Sheets(1).Range("M15").Value = Now()
This selects the first sheet in the workbook. If you have multiple workbooks, you will need to specify that somehow as well (by referring to the active workbook, or the name of a workbook, etc.). For example:
Activeworkbook.sheets(1).Range("M15").Value = Now()

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