VBA to log to tx file

32CARDS

Board Regular
Joined
Jan 1, 2005
Messages
123
Concept: To log a series of processes as required to a single txt file, similar to a windows log file.
Currently I use this code to "auto-save" when I require it to save and not the regular settings based on time. I need this to save the workbook when the value reaches a predtermined number, in this case it is 10.
Is there a way to have a code that will append the folowing info to a txt file.

Sub SAVE_COUNTER()

Sheets("Sheet1").Select
Sheets("Sheet1").Range("H13").Select
Sheets("Sheet1").Range("H13").Value = "AUTOSAVE COUNTER"
Sheets("Sheet1").Range("G14").Select
Sheets("Sheet1").Range("G14").Value = Sheets("ZEROWUN").Range("G14").Value + 1
If Sheets("Sheet1").Range("G14").Value = "10" Then

Sheets("Sheet1").Range("H15").Value = "SAVING WORKBOOK"

ActiveWorkbook.Save

Sheets("Sheet1").Range("H6").Value = "WORKBOOK SAVED"

Sheets("Sheet1").Range("G14").Value = "0" 'RESET TO ZERO

End If
End Sub

I am hoping the txt file to look something like this:

>20110907:11:10:28: Sub SAVE_COUNTER
>20110907:11:10:29:SAVING WORKBOOK
>20110907:11:10:30:ActiveWorkbook.Save
>20110907:11:10:45:WORKBOOK SAVED
>20110907:11:10:46:"0" 'RESET TO ZERO

It does take about 15 seconds to save this workbook, so in this case until the workbook has finished saving, the code then may continue it's Run.

So the concept would look something like,

Sheets("Sheet1").Range("G14").Select
Sheets("Sheet1").Range("G14").Value = Sheets("ZEROWUN").Range("G14").Value + 1
If Sheets("Sheet1").Range("G14").Value = "10" Then
ActiveWorkbook.Save

Log_Message 'to txt file instead "log" to a cell range


But there may be other instances to log other processes, not just the Workbook.Save
The Workbook.Save example is simply 1 of many sections of the process I need to log in txt rather than cell ranges, but the text I want logged, is in the VBA code itself

Log_Message, is the VBA "call" to start the log process

Is there a way to do this?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Writing to a text file is pretty simple. Have a go at ammendin this:

Sub logUse()

On Error Resume Next

Dim MyFile As String, logEntry as String
Dim fnum As Variant
MyFile = "c:\log.txt"
logEntry = "TEXT"
fnum = FreeFile()
Open MyFile For Append As fnum
Print #fnum, logEntry
Close #fnum

End Sub
 
Upvote 0
Thanks, I tested it and retested it did exactly as I required, where it made only 1 text file, yet tested the process 5 times. So I got 1 single file, @ 5 processes-5 loggings.
Good.

But,
I need this to process text log, many many and many times. To give an example there are 12 Modules, with heaps of VBA code.
Does that mean, if I need lets say 20 different logs on the same text file, I have to add your code 20 times?
Or is there a way for example in VBA coding
where I could simply just write for example

Sub My_Process

logEntry = "TEXT"logEntry = "TEXT-abc"
My codes
My codes
My codes
logEntry = "TEXT"logEntry = "TEXT-def"
My codes
My codes
My codes

logEntry = "TEXT"logEntry = "TEXT-xyz"
My codes
My codes
My codes

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

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