Putting Date In Last cell

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
When I close a worksheet I would like todays date automatically put in the last cell in column H. It cant be after the last used cell as it will add it on the end each time whereas I need it overwritten each time. Also the code will be used on 20 different files which have different ranges in each file. Would I have to put it in each file or will the PWB do it? Thanks.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try this in the ThisWorkbook module of each workbook. Change Sheet1 to suit.

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Sheet1").Range("H" & Rows.Count).End(xlUp).Value = Date
End Sub
 
Upvote 0
Will that not add the date each time? For example it will add 01/07/11 in cell H500 then next day I close it it will add 02/07/11 to cell H501 and so on? I need it to overwrite each time.
 
Upvote 0
That will overwrite. Be sure to enter something in H500 or whatever first otherwise the first time it runs it will overwrite whatever is in the current last cell.
 
Upvote 0
Thanks that works but is there anyway I can get away from having to save the files as macro-enabled workbooks by somehow putting the code in my PWB?
 
Upvote 0
You can try but I don't think it would work in the PWB. You would need to amend the code like this (also I've noticed an oversight)

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ActiveWorkbook.Sheets("Sheet1").Range("H" & Rows.Count).End(xlUp).Value = Date
ActiveWorkBook.Save
End Sub
If it did work (which I doubt) the code would run when you closed any workbook.
 
Upvote 0
Also I have recorded this macro to run before close but what if the rows vary each time? At the moment there are 5238 but I may add rows from time to time and basically the code counts the amount of rows and puts the figure in column A

Code:
Sub Macro1()
Range ("A2:A3").Select
Selection.AutoFill Destination:=Range("A2:A5238")
Range ("A2:A5238").Select
Range("A2").Select
End Sub
 
Upvote 0
Try this

Code:
Sub Macro1()
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("A2:A3").AutoFill Destination:=Range("A2:A" & LR)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,741
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top