Problems with obtaining modified date - excel

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

Related

why do i run into a runtime error (range) when im trying to execute a timestamp user defined function

I have a sheet which i want people to populate manually
One of these columns is for a date and a timestamp, rather then manual entry for this as its pretty awkward for noobs on excel i have made a user defined function and assigned to a button... select cell you want timestamp in ...click button i have made..timestamp appears in cell...all works perfectly
HOWEVER
I need to lock and protect some columns on a sheet and leave just a table for people to populate
When i do this it runs into a runtime error about range?! i dont understand why as it is only one cell within the unprotected table that needs updating?
Code to user defined function below
Sub Timestamp()
Dim ts As Date
With Selection
.Value = Now
.NumberFormat = "dd/mm/yyyy h:mm:ss AM/PM"
End With
End Sub
any help appreciated
Thanks a million
I will always recommend to protect your worksheet using VBA if you need execute VBA code on the protected sheet, so that it can bypassed the protected range and update your timestamp successfully:
Copy this code to ThisWorkBook will protected specific WS but allow VBA to write value on locked cell
Sub Workbook_Open()
Sheet1.Protect Password:="xxx", UserInterfaceOnly:=True,
End Sub

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

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()

Delay Macro Run, Excessive Countifs

I am running a macro that opens a file referencing the one I am working in, pastes the relevant items as values into a separate sheet and makes a workbook out of that sheet.
The reason why I am doing this is because there are several thousand countifs, averageifs, and processor-intensive ilk.
The program runs from start to finish, just fine. The issue is that only a few of the items are calculated before the copy/paste operation and so I get a lot of #VALUE errors on the copy of the sheet with the formulas--even though the formulas are calculating correctly on further inspection.
I suspect the correct course of action is to delay the run until the sheet finishes calculating. Any and all help would be appreciated.
EDIT: I've tried all manner of application.calculations and nothing seems to be working. The links and items calculate normally if I open manually and let the processor do its thing. The only items that calculate are the ones that contain "COUNTA" somewhere in it. Is it possible that the application calculation methods don't work with Countifs and the like?
Shouldn't be that hard to do - the Worksheet object has a Calculate property that fires after it calculates. You can add a custom property to the worksheet that exposes a flag that you set after it is done calculating. In the worksheet code that has the time consuming calculation...
Option Explicit
Private can_copy As Boolean
Public Property Get CopyOK()
CopyOK = can_copy
End Property
Private Sub Worksheet_Calculate()
can_copy = True
End Sub
Private Sub Worksheet_Activate()
can_copy = False
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
can_copy = False
End Sub
'For volitile functions.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
can_copy = False
End Sub
...and in the calling code:
Dim book As Workbook
Set book = Application.Workbooks.Open("C:\foobar.xlsm")
Do While Not book.Worksheets("Sheet1").CopyOK
DoEvents
Loop
'Do your thing...
Note that I likely missed some events that would trigger a recalculation, but this should cover the scenario of just opening it.
So, I found a means for this to work:
Do Until Application.CalculationState = xlDone
Application.Calculate
While Application.CalculationState <> xlDone
MsgBox Application.CalculationState
DoEvents
Wend
Loop
It was a solution I sort of applied from Siddharth Rout : Wait until Application.Calculate has finished
Thank you everyone for your help!

Resources