VBA help

jtodd

Board Regular
Joined
Aug 4, 2014
Messages
194
I have this code that creats a log of when a workbook is opened
VBA Code:
Private Sub Workbook_Open()
    Open "\\Dtc06\newheart\Jim Todd\Testing_Recovered\RDC_Excel_testing\UserLogs\testlog.log" For Append As #1
    Print #1, Application.UserName, Now, ThisWorkbook.Name
    Close #1
End Sub

How could I change this to
1. write to an excel file rather than a notebook file.
2. to show open and close times .

Any help would be appreciated .
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Writing to a textfile is less intrusive
Here is amended code to do that

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    WriteToFile ("Close")
End Sub

Private Sub Workbook_Open()
    WriteToFile ("Open")
End Sub

Private Sub WriteToFile(Status As String)
Const c = ","
    Open "\\Dtc06\newheart\Jim Todd\Testing_Recovered\RDC_Excel_testing\UserLogs\testlog.log" For Append As #1
    Print #1, Application.UserName & c & Now & c & ThisWorkbook.Name & c & Status
    Close #1
End Sub

The data is now comma delimited
How this next bit works for you may be different - it depends on which version of Excel that you are using
But in Excel365,
Create a new workbook
On Data tab \ Get Data from Text or CSV \ Excel brings in data as a query in a table like this
Book1
ABCD
1Column1Column2Column3Column4
2yongle15/07/2020 12:18:35Log to log.xlsmClose
3yongle15/07/2020 12:19:33Log to log.xlsmOpen
4yongle15/07/2020 12:19:36Log to log.xlsmClose
5yongle15/07/2020 12:22:51Log to log.xlsmOpen
6yongle15/07/2020 12:25:45Log to log.xlsmClose
7yongle15/07/2020 12:25:52Log to log.xlsmOpen
8yongle15/07/2020 12:25:55Log to log.xlsmClose
9yongle15/07/2020 12:29:26Log to log.xlsmOpen
10yongle15/07/2020 12:29:30Log to log.xlsmClose
Sheet2


Save and close the new workbook
Open up the one being logged and close it again etc (simply to amend the log file)

Open new workbook
Click on Data tab \ Refresh All to refresh the values in the table
Book1
ABCD
1Column1Column2Column3Column4
2yongle15/07/2020 12:18:35Log to log.xlsmClose
3yongle15/07/2020 12:19:33Log to log.xlsmOpen
4yongle15/07/2020 12:19:36Log to log.xlsmClose
5yongle15/07/2020 12:22:51Log to log.xlsmOpen
6yongle15/07/2020 12:25:45Log to log.xlsmClose
7yongle15/07/2020 12:25:52Log to log.xlsmOpen
8yongle15/07/2020 12:25:55Log to log.xlsmClose
9yongle15/07/2020 12:29:26Log to log.xlsmOpen
10yongle15/07/2020 12:29:30Log to log.xlsmClose
11yongle15/07/2020 12:32:34Log to log.xlsmOpen
12yongle15/07/2020 12:39:24Log to log.xlsmClose
Sheet2
 
Upvote 0
many thanks , very clear and comprehensive solution, i will try this ASAP and let you know how i get on.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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