0

I am trying to store the variables and actions in VBA macro for troubleshooting or debugging purposes like below. Is there any built-in method to do this in background for this instead using "If DEBUG_MODE Then" for every line I need to store?

Dim DEBUG_MODE As Boolean
Sub PopulateData()
Dim ws As Worksheet, cel As Range
DEBUG_MODE = True
For Each ws In ActiveWorkbook.Worksheets
    If DEBUG_MODE Then PrintToFile ws.Name & "," & Now
    For Each cel In ws.Range("A1:A1000")
        If cel.Value = cel.Offset(0, 1).Value Then
            If DEBUG_MODE Then PrintToFile "Value Matched at " & cel.Address
            cel.Offset(0, 2).Value = cel.Offset(0, 2).Value * 5
        End If
    Next cel
Next ws
End Sub
Dhay
  • 585
  • 7
  • 29
  • You have only `Debug.Print` built-in (and that will only handle up to 200 lines before you begin to lose content), but you can write content to a text file. There are plenty of examples here on SO - eg. https://stackoverflow.com/questions/49470859/two-ways-to-write-to-text-file-using-excel-vba-microsoft-standard-library-vs-mi – Tim Williams Aug 01 '23 at 05:28
  • @TimWilliams. I should have been clearer. Now updated the question content. – Dhay Aug 01 '23 at 05:30
  • 1
    https://bettersolutions.com/vba/debugging/conditional-compiling.htm but that's not much different from your example. – Tim Williams Aug 01 '23 at 05:42
  • 1
    You could also move the `If DEBUG_MODE` into `PrintToFile` – Tim Williams Aug 01 '23 at 05:43
  • *Is there any built-in method to do this in background for this instead using "If DEBUG_MODE Then" for every line I need to store?* - **No**. You're using the normal method. You could make it look better by following Tim's suggestion and making the first line of `PrintToFile` something like `If Not DEBUG_MODE Then Exit Sub` – CLR Aug 01 '23 at 07:07

0 Answers0