Latest Modification Time Stamp for Excel [duplicate] - excel

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Date Modified Time Stamp of an Excel Sheet Using VBA
Hi I've an excel workbook...I want to jot down the time stamp in cell A2 of sheet 1 for the latest modification made in any row or column of sheet2 of the workbook.
So my problem is two fold:
1. Want to trace the latest modification time.
2. Want to make the cell A2 of sheet1 as user protected so that no one can tamper with the traced time stamp.
Here is the code on which i was workung...and its not correct as its not doing anything..if it would have bben doing anything then definitely there was not any problem for me in sharing it but as its not doing anything so its a bit useless.
Private Sub worksheet_change(ByVal Target As Range)
Dim Row, Col
For Row = 2 To Sheet2.UsedRange.Rows.Count
For Col = 1 To Sheet2.UsedRange.Columns.Count
If Target.Cells(Row, Col) Then
Application.EnableEvents = False
Sheet1.Cells("A2") = now()
Application.EnableEvents = True
End If
Next Col
Next Row
End Sub

To get you headed in the right direction:
Right Click on the sheet tab for sheet 2.
Select "View Code".
You'll see two drop-downs at the top of the module. In the first one select "Worksheet". In the second one select "Change".
This will create the template for the Worksheet_Change procedure, which will fire whenever the user changes a value in Sheet2.
Use that to write the time stamp into cell A2 of sheet1.
For the second part of your problem, protect sheet 1. In code, that's:
ThisWorkbook.Worksheets("Sheet1").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
And before you write to cell A2, unprotect it first:
ThisWorkbook.Worksheets("Sheet1").Unprotect
Look up the Protect and Unprotect methods in help if you want to add a password.
(Bear in mind that sheet protection is pretty weak, even with a password. For casual work it's OK, but it CAN be cracked by users if they're determined to do it. A better approach might be to write the value into a sheet which has a visible property of xlVeryHidden so that most users won't even know it's there (if you apply protection to the VBA project). Again, not uncrackable but better than having it in a visible sheet.)

Related

Managing Excel Worksheet_Change Feature

I have created a spreadsheet which uses the Worksheet_Change feature and the code associated with that works very well. I can stop it firing unnecessarily when inside the module by using Application.EnableEvents = False.
However, while I've created a form to enter data directly into the next available row (again, that works fine in terms of entry) it doesn't cause the formulae in the sheet to calculate (even though auto calculation is on and re-enabled within the module). If I manually place my cursor in the row, hit F2 and simply press enter, everything then fires up.
I have tried to enter data directly into the cells, but of course the Worksheet_Change feature then kicks in again and the cursor isn't simply moving to the next adjacent cell ....
I've tried to check firs for any direct entry with the code below and if it looks like the user isn't entering directly into the cell, the Worksheet_Change is disabled:
Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo eventhandler
Sheets(1).Range("a1").Select
LastCell2 = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Dim intersection As Range
Set intersection = Intersect(Target, Range("A3:F" & LastCell2))
If intersection.Row = LastCell + 1 Then
Exit Sub
End If
Application.EnableEvents = False
The code above is simply checking to see if data is being entered into the next empty cell and if that's the case I want it to just exit there but it isn't working.
So I actually have 2 problems :
the first is why this formula isn't triggering after entry via a vba form - I've used INDIRECT since there are other macros that delete rows by moving the remaining cells up and that was causing the count in the $A$3:$A$500 to reduce to $A$499 and then 498 etc - the addition is done depending on the system date and the transaction date so I get a current value and a future value using a standard sum statement:
=AD1-(SUMIF(INDIRECT("$A$3:$A$500"),"<="&TODAY(),INDIRECT("$E$3:$E$500")))+(SUMIF(INDIRECT("$A$3:$A$500"),"<="&TODAY(),INDIRECT("$F$3:$F$500")))
The second is why I can't enter data directly into the spreadsheet and trap the fact that I don't want it to do anything and simply allow the user to hit enter and move to the next adjacent cell to the one they just entered data into.
Is this a lost cause and am I trying to do too much here? I'm relatively new to coding and teaching myself so apologies if the standard and style isn't to everyone's taste.
Thanks in advance for any replies.

VBA/Excel: Flow Chart Formula

I'm sorry if this question is vague or answer exists, but struggling to find what I'm looking for.
I have created a sheet (VBA generated) where the user is required to fill in certain cells in a table. As multiple users are going to be editing the document, I want to automate a 'flow chart' type answer to standardize the document.
I.e. if in the first cell the user input is "No", then the remaining cells in the table default to "N/A". If the input is "Yes", then the user moves on to filling out the next box in the table. I was also going to use a drop down list to ensure the correct user input.
Does anyone have any suggestions of what I can research for a method to achieve this? Was thinking of having the cell formula, but this will delete after user input so wanted a method where the formula would remain even if the user input is deleted.
Thanks for any help.
The answer would be to use the worksheet_change event. However your problem is you are creating the sheet programatically. If you are using a template for your sheet you can just attach the code to the template, other wise you will have to create the code in your workbook and use the Workbook_sheetchange event
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "sheet1" Then
If Target.Address = "$A$5" Then 'I'm assuming cell A5 is your first cell, change as required
If UCase(Sh.Range("a5")) = "NO" Then
Sh.Range("a10") = "#N/A" 'repeat for all cells you want to be NA
End If
End If
End If
End Sub

How to get an excel cell to show the time it was selected

I am trying to create a table that tracks downtime for a production machine. the operator will be using a table with the columns down time start and down time stop. each time something happens that they have to leave the station I want them just to have to click the empty cell under downtime stat title and the time will appear/ log itself in the cell then the same for downtime stop.
I under stand how =now() works but then it shows the time when the file is opened, i want it to display only when the cell is selected.
Any help is much appreciated! I have never had to do anything like this in excel before.
You'll need some VBA for this. In your VBE (alt+f11) go to your worksheet and use something like the following:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Check to see if the click/selected cell is in columns A or B
If Not Intersect(Target, Range("A:B")) Is Nothing Then
'Make sure just one cell is selected:
If Target.Cells.Count = 1 Then
'Update the value
Target.Value = Now()
End If
End If
End Sub
Here we are using the Worksheet_SelectionChange() event. This event will fire any time a selection change is detected on the worksheet in which this code is placed. When the change is detected it will test to see if the selection was in columns A or B. It will also test to insure that only one cell was clicked (otherwise highlighting those columns would cause every row in the column to update with the time, which would be bad). If that all passes, then it just sets the selected cell's (target) value to the current time.

Linking cells to each other; having cells update linked cells and vice versa

I have two ideas that could lead to more or less the same result.
I am trying to have similar cells or tables update themselves to whatever the most recent entry was in the linked system. For example, cell A1 is linked to cell B2 (in this system that I am trying to create). I would enter something like "1" or "Text" or whatever in A1 and the system would update cell B2 to whatever I entered in cell A1. However the opposite must work in the same manner. If I changed whatever in cell B2 to, say, "5", cell A1 would also display "5". Also, note that I have Excel 2013.
I need this to work with a cell or a table. So, getting to the possible solutions...
A subroutine in VBA that automatically updates all the linked cells or tables.
Some sort of mechanic unknown to me that uses VBA or another Excel aspect to do this, like a function or tool.
In your answer or solution, please be mindful to my inexperience with VBA. Thanks in advance.
You can try a worksheet change function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address then
ActiveSheet.Range("B1").Value = ActiveSheet.Range("A1").Value
ElseIf Target.Address = Range("B1").Address then
ActiveSheet.Range("A1").Value = ActiveSheet.Range("B1").Value
End If
End Sub
Although this seems like it might create an infinite loop (the update from the change causes another change) it DOES work for me in Excel 2010..
There are other Worksheet functions you can try as well (e.g. Worksheet_SelectionChange)
This macro needs to be placed/entered as a WORKSHEET macro on the sheet where you desire to use it..it will not work in a Module.
To install:
1) Save your workbook as a macro-enabled file.
2) Close Excel, reopen file, and enable macro security
3) Type Alt-F11
4) In the project explorer view on the left, look for your sheet name. Double click it
5) In the code entry area on right (big window) paste the example code above
6) Return to your worksheet and try it.

How to Protect the sheet name not the entire sheet?

I need to protect the sheet name by preventing
any change to the sheet name, or
the sheet being deleted.
This must be done without protecting the entire sheet using the Protect Sheet or Protect Workbook options.
Is there any way to do this with VBA?
Right click the sheet tab that you wish to protect
View Code
Copy and paste in the code below
This code disables the delete control on the sheet (but not right click on cell) menu when the sheet is activated. The control is enabled when the sheet is de-activated
The code will also name the sheet "NameOfSheet" when the sheet is de-activated. This is a workaround to prevent the sheet being renamed
Private Sub Worksheet_Activate()
Application.CommandBars.FindControl(ID:=847).Enabled = False
End Sub
Private Sub Worksheet_Deactivate()
Application.CommandBars.FindControl(ID:=847).Enabled = True
Me.Name = "NameOfSheet"
End Sub
I don't think you can. What you can do, you can make a worksheet a very hidden one (accessible only from VBA) and in case of the deleted sheet, you can copy it and make a copy visible.
Would this approach work?
Select all cells in the sheet, then UN-lock all cells with "Lock
Cells" (yellow background of padlock turns white).
Write the name of the sheet in a (fixed or named) cell, then lock
this cell ONLY ("Lock Cells", padlock background turns yellow).
Then Protect workbook, but allow every action, except the first one
"Select Locked Cells".
The user can do everything except selecting the cell with the sheetname (and delete rows/columns).
Now write a VBA to compare the actual sheetname with the data in the protected named cell (or fixed reference e.g. A1).
Run this script either on every change (probably too much) or at least on close of
the workbook.
As long as the sheetname is always in the same cell (e.g. A1), you can then loop through all sheets, compare their name with the data in cell A1 and correct the sheet name if required.

Resources