Macro to log macro usage?

Mr_Phil

Board Regular
Joined
May 28, 2018
Messages
147
Office Version
  1. 365
Hi. I think this has been asked before but I could not find the thread. I have a workbook with about 15 tabs and about 2000 (at least) lines of VBA macro code. I need to update the workbook and whilst doing that I'd like to delete out old macros that no longer apply. I am thinking that I'd like to be able to see a list of macros and then a number to tell me how many times they have run within a set period of time. It would be nice to know the last time it ran but that isn't essential. If it ran since implementing the "Log" it gets kept or it gets rewritten/updated or replaced with power querys. This is involved. So a pointer to a tutorial or article for study puposes would be awesome. Thanks for looking.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You would have to add code to each SUB that saved information to a Log.

With the code below, all you would have to do is add SaveToLog "SubName" to each SUB you want to log. Where SubName is the actual name of the Subroutine.

Book4
ABC
1SUB NameDate of RunUser Name
2FakeSubName2/27/2024 7:58Mahoney, Jeffrey
3FakeSubName22/27/2024 7:59Mahoney, Jeffrey
4FakeSubName32/27/2024 7:59Mahoney, Jeffrey
Log


VBA Code:
Sub SaveToLog(SubName As String)
  Dim Cel As Range
  Dim UN As String
  Dim LogSht As Worksheet
  
  UN = Application.UserName
  Set LogSht = Sheets("Log")
  
  Set Cel = LogSht.Cells(LogSht.Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
  
  Cel.Value = SubName
  Cel.Offset(0, 1).Value = Now()
  Cel.Offset(0, 2).Value = UN
  
  
End Sub

Sub TestLogSave()
  SaveToLog "FakeSubName3"
End Sub
 
Upvote 1
Solution
You would have to add code to each SUB that saved information to a Log.

With the code below, all you would have to do is add SaveToLog "SubName" to each SUB you want to log. Where SubName is the actual name of the Subroutine.

Book4
ABC
1SUB NameDate of RunUser Name
2FakeSubName2/27/2024 7:58Mahoney, Jeffrey
3FakeSubName22/27/2024 7:59Mahoney, Jeffrey
4FakeSubName32/27/2024 7:59Mahoney, Jeffrey
Log


VBA Code:
Sub SaveToLog(SubName As String)
  Dim Cel As Range
  Dim UN As String
  Dim LogSht As Worksheet
 
  UN = Application.UserName
  Set LogSht = Sheets("Log")
 
  Set Cel = LogSht.Cells(LogSht.Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
 
  Cel.Value = SubName
  Cel.Offset(0, 1).Value = Now()
  Cel.Offset(0, 2).Value = UN
 
 
End Sub

Sub TestLogSave()
  SaveToLog "FakeSubName3"
End Sub
I like it. This will help with the updating of the old workbook. Thank you very much.
 
Upvote 0
You would have to add code to each SUB that saved information to a Log.

With the code below, all you would have to do is add SaveToLog "SubName" to each SUB you want to log. Where SubName is the actual name of the Subroutine.

Book4
ABC
1SUB NameDate of RunUser Name
2FakeSubName2/27/2024 7:58Mahoney, Jeffrey
3FakeSubName22/27/2024 7:59Mahoney, Jeffrey
4FakeSubName32/27/2024 7:59Mahoney, Jeffrey
Log


VBA Code:
Sub SaveToLog(SubName As String)
  Dim Cel As Range
  Dim UN As String
  Dim LogSht As Worksheet
 
  UN = Application.UserName
  Set LogSht = Sheets("Log")
 
  Set Cel = LogSht.Cells(LogSht.Cells.Rows.Count, 1).End(xlUp).Offset(1, 0)
 
  Cel.Value = SubName
  Cel.Offset(0, 1).Value = Now()
  Cel.Offset(0, 2).Value = UN
 
 
End Sub

Sub TestLogSave()
  SaveToLog "FakeSubName3"
End Sub
Ok, if you ever thought that your advice was blithely dismissed or that you were not truly helpful ... neither is true. I did what you said, then I did a control H to insert the on liner into each of 57 Macros. Then I made a log sheet. Then added a column to list and count everything that gets logged. Over the next day or two I will be able to confidently delete the ones that have outlived their useful life. Please don't laugh too much at my Macro names. I am just a logistics guy with no formal training. I truly appreciate the help.

1709073413309.png
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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