Last Save Date and Time VBA for a specific worksheet - vba

Hi there i have come across of last save options for a workbook, whereby the code allows to track the date and time of it being last modified and displayed in a cell .I was wondering if vba could allow to track the data and time of it being last modified on a specific worksheet, lets say Sheet1. So everytime changes has been made and saved in sheet 1, it would reflect the time and date saved of that sheet only. Here is the code i have for the workbook so far, tried adding .Sheets("Sheet 1") to the code but it tracks the time i visited the page and not that of i edited. These are the codes in my workbook.
Private Sub Workbook_Open()
Call starttheClock
End Sub
Sub Workbooky()
ActiveWindow.ScrollRow = 1
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Name = "Index" Then Exit Sub
Application.EnableEvents = 0
i = ActiveSheet.Index
With Sheets("Index")
.Cells(i, 1) = ActiveSheet.Name
.Cells(i, 2) = Now
End With
Application.EnableEvents = 1
End Sub

Just to add a bit more confusion to the above answer, if you do have a sheet named "Index" and want to have a date that is added to a sheet every time there is a change to it, possibly a code that will go into the workbook module. Then you will have only one code to check whenever any of the worksheets have a change.
This is where the workbook module is located and the code belongs there.
This assumes you have a sheet named "Index", name it whatever you want once you get it going properly. The actual sheet name and the sheet name in the code have to match exactly though.
Here is the code that will go in the workbook module.Copy and paste it into the Workbook Module
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Name = "Index" Then Exit Sub
Application.EnableEvents = 0
i = ActiveSheet.Index
With Sheets("Index")
.Cells(i, 1) = ActiveSheet.Name
.Cells(i, 2) = Now
End With
Application.EnableEvents = 1
End Sub

You can't save a worksheet. Always you have to save the whole workbook. That is the reason why it comes up with errors.
If you want to know the exact time and date you edited each sheet, you can find as per below.
http://www.ozgrid.com/forum/showthread.php?t=46624
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Index").Range("B2") = Now
End Sub

Related

Change active sheetname based on a cell value of that sheet automatically

I found this code on google which can help me to change current tab name based on a cell value of this sheet. However, I have to run this macro code manually each time. How can I modify this code to make it change automatically after entering value in a cell or at least tab's name changes as typing. Here is the code:
Sub myTabName()
ActiveSheet.Name = ActiveSheet.Range("C3")
End Sub
place this in the sheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "C3" Then ActiveSheet.name = ActiveSheet.Range("C3")
End Sub

How do I hide a sheet with a button in Excel?

I would like to create a button to hide the sheet. Ideally, it would hide the sheet where the button is located.
To be simple, sheet 1 has a form to fill out. The typical name, address, phone number, etc. Sheet 2 and 3 also have the same fields that is going to be referenced to the 'Data' tab as well. On a sheet named 'Data', I will add fields that will populate simply with the = option like (=Data!...)
However, I do not want the Data page in view once the information is added. We all know the simple right click and hide sheet. But sometimes that's too much for some people that will use this sheet and a pretty button would work better.
I was successful using:
Module 1
Sub SheetCommand()
If Worksheets("Sheet1").Range("C2").Value <> vbNullString Then
Worksheets("Sheet2").Visible = False
Else
Worksheets("Sheet2").Visible = True
End If
End Sub
Sheet1 Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$C$2" Then Exit Sub
Run "SheetCommand"
End Sub
However, I am not very VB savvy and have hit a dead end. Could someone help show me how to apply this to a button? I don't want it to reference the $C$2 field as noted on the example, but just when someone presses the button, the sheet goes away. I'm not worried about getting it back as someone can be ready to get it the old fashioned way. This would help the data entry process for this manual form so much easier.
Edit: basically, I need help creating a vba code where I can hide the page. I'd like to create a button where once clicked, it hides that page. I showed an example of a code where I got it to work but it only works if that cell is populated. How do I make it work on button click?
I found this code that does what I need but I would like to tell it a specific Sheet Name instead of having to type it in B6 and B7.
Sub ShowHideWorksheets()
Dim Cell As Range
For Each Cell In Range("B6:B7")
ActiveWorkbook.Worksheets(Cell.Value).Visible = Not
ActiveWorkbook.Worksheets(Cell.Value).Visible
Next Cell
End Sub
It's actually not very clear to me what's your exact goal
for instance, assuming "Sheet 2" and "Sheet2" would point to the same sheet, in your question you seem to reference it as being both a "Form" sheet ("...sheet 1 has a form to fill out....Sheet 2 and 3 also have the same fields...") and "Data" sheet (Worksheets("Sheet2").Visible = False)
so here follow some possible solutions:
1) you want to hide "Data" sheet before closing the workbook it's contained in
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Data").Visible = False
End Sub
2) you want to hide "Data" sheet once some cells have been filled up in ANY sheet other than "Data"
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name <> "Data" Then
With Sh
'here follows code to check if "the information is added"
'for instance:
If WorksheetFunction.CountA(.Range("A2:C2")) = .Range("A2:C2").Count Then ThisWorkbook.Worksheets("Data").Visible = False 'check if all cells in range "A2" to "C2" has been filled with some data
End With
End If
End Sub
3) you want to hide "Data" sheet once some cells have been filled up in ALL sheets other than "Data"
then place the following code in "ThisWorkbook" code pane of the workbook containing "Data" sheet
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim sht As Worksheet
Dim hideBool As Boolean: hideBool = True 'set initial value as true
If Sh.Name <> "Data" Then
For Each sht In ThisWorkbook.Worksheets
With Sh
'here follows code to check if "the information is added"
'for instance:
hideBool = hideBool And WorksheetFunction.CountA(.Range("A2:C2")) = .Range("A2:C2").Count ''check if all cells in range "A2" to "C2" of current sheet has been filled with some data
End With
If Not hideBool Then Exit For
Next sht
ThisWorkbook.Worksheets("Data").Visible = Not hideBool ' hide if ALL sheets met "information is added" condition
End If
End Sub
4) otherwise give more info about the desired behavior

Excel VBA: Move ActiveCell to Row of Newly Inactive Sheet

When I move from Sheet1 to Sheet2, what VBA can I use to have the activecell of Sheet2 be the same row as was active on Sheet1 when I switched?
For example: I have Cell B7 active on Sheet1. When I switch to Sheet2, the activecell moves to the 7th row, (and does not change columns from what it was the last time I was on Sheet2).
After really debugging hard on event sequences, I said "Eureka!". The following does what you ask:
Private activeRow As Integer, activeCol As Integer
Private sema4 As Integer
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
If (sema4 > 0) Then Exit Sub
sema4 = 1
Sheets(Sh.Name).Activate
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If (sema4 = 1) Then
activeRow = Selection.row
activeCol = Selection.Column
sema4 = 2
Exit Sub
ElseIf (sema4 = 2) Then
sema4 = 3
Sheets(Sh.Name).Activate
Exit Sub
ElseIf (sema4 = 3) Then
ActiveSheet.Cells(activeRow, activeCol).Select
sema4 = 0
End If
End Sub
Again, attach in VB editor to the Workbook.
Although the question received a downvote, it is absolutely not trivial. I have only been able to research a partial answer.
Attach the following code to the Workbook (double click on ThisWorkbook in VBA Project Explorer):
Private activeRow As Integer, activeCol As Integer
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
activeRow = Selection.Row
activeCol = Selection.Column
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
ActiveSheet.Cells(activeRow, activeCol).Select
End Sub
The intention is clear: get the selection on the sheet being deactivated and then set the selection on the sheet being acivated.
There only are two problems:
Excel has only one Selection and that is the current selection on the active sheet.
The deactivate event occurs after the sheet is deactived and the new sheet activated.
As a result, it is not possible to get the last position of the user on the sheet that got deactivated and so we can't set it on the sheet being activated.
Anyone any ideas?

VBA to format cells based on another cell not working

SOLVED: FOUND MY OWN WORKSHEET ERROR
The problem I was having was trying to use two worksheet_change events in the same workbook. Because I thought that was possible, I was just renaming the worksheet event in question when I received an error, thinking nothing of it. Both my original code and the answer provided work, when combined with my other worksheet_change event.
Thanks everyone.
Original Request:
I am trying to run a macro that does this:
every time cell r6 changes, run a macro that looks to see if the value in cell s9 is > or < 1, then format cells s9:t100 based on that.
I have the macro on its own to do the second part:
sub macro1()
If Range("S9").Value < 1 Then
Range("S9:S100,T9:T100").Select
Selection.NumberFormat = "0.0%"
Else
Range("S9:S100,T9:T100").Select
Selection.NumberFormat = "#,##0"
End If
end sub
This macro run on its own, works exactly as I want and formats the cells.
Then I have the worksheet event to call up that macro:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$R$6" Then
Call Macro1
End If
End Sub
When it is run to call up the same macro, it does not format the cells. They just stay as a % regardless of when cell r6 changes.
Any ideas why the worksheet event causes the macro to not work?
Try passing the worksheet object to your macro. This fully qualifies the Ranges to make sure you're working on the right area.
Also, you don't need to Select at all. Just use the range and directly change the settings.
Public Sub Macro1(ws as Worksheet)
If ws.Range("S9").Value < 1 Then
ws.Range("S9:S100,T9:T100").NumberFormat = "0.0%"
Else
ws.Range("S9:S100,T9:T100").NumberFormat = "#,##0"
End If
end sub
Sub test()
Macro1 ActiveSheet
End Sub
And in your Worksheet_Change...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$R$6" Then
Macro1 Target.Worksheet
End If
End Sub

Pop up a message in vba

I am working on a file which in one sheet lets call it summary, I have formula that calculate the values which Ienter manually in other sheets, so I want to have an vba code to notify me by pop up message that the calculation result in summary sheet is <=0 when doing data entry in other sheets.
I have found below code which works fine with only one cell but if I want to extend it to other cells in the same row results in error. Suppose I want to extend it to B9:CZ9.
Private Sub Worksheet_Calculate()
If Me.Range("B9").Value <= 0 Then _
MsgBox "Leave is finished!"
End Sub
Possible background:
Private Sub Worksheet_Calculate()
With Me.Range("B9:CZ9")
If Application.CountIf(.Cells, "<=0") = .Cells.Count Then _
MsgBox "Leave is finished!"
End With
End Sub
Private Sub Worksheet_Calculate()
Dim RNG As Range
Set RNG = Selection
For Each c In RNG
If c.Value <= 0 Then
MsgBox "Leave is finished!"
End If
Next c
End Sub
If I understood you correctly, maybe this code helps you (you have to put this code to the source of the correct worksheet you want to work this code on):
Private Sub Worksheet_Calculate()
Dim leave As Boolean: leave = False
For Each c In Me.Range("B9:CZ9").Cells
If c.Value <= 0 Then: leave = True
Next
If leave Then: MsgBox "Leave is finished!"
End Sub
This code works when something is calculated in a cell, for example when you type =0 into any of them, and don't give you lots of messageboxes.
If you want this to work when anything changes, use Private Sub Worksheet_Change(ByVal Target As Range)
Remember that extension xlsx cannot contain VBA codes. Therefore after implementing the code to the worksheet you want this code works on, you have to save it as macro-enabled workbook.