EDIT: (v0.2) Now works for all sheets in the current workbook. (And was created for other books.)
You can do something hidden and connect the MsgBox function and analyze the data from its output.
Just do a global search of MsgBox in your code and replace it with, for example, MsgBoxInterceptor .
Then you write the function MsgBoxInterceptor() , oh, say, as shown below;)
Run RunMe() sub as normal, and voila! Instead of displaying on the screen, you get output on a new worksheet.
No need to even figure out what your original code does!
NB. The above function only draws use cases from the active book.
'v0.2 Private Function MsgBoxInterceptor _ ( _ Prompt, _ Optional Buttons As VbMsgBoxStyle = vbOKOnly, _ Optional Title, _ Optional HelpFile, _ Optional Context _ ) _ As VBA.VbMsgBoxResult Const i_TargetCell As Long = 1 Const i_TargetSheet As Long = 2 Const i_SourceCell As Long = 3 Const i_SourceSheet As Long = 4 Static slngState As Long Static srngDataRow As Range Static sstrTargetCell As String Static sstrTargetSheet As String Static slngClosedBookCount As Long Static slngOpenBookCount As Long Static slngSameBookCount As Long Static slngSameSheetCount As Long Dim f As WorksheetFunction: Set f = WorksheetFunction Dim lngBegin As Long Dim lngEnd As Long Dim i As Long Select Case slngState Case 0: ' Get counts and target Worksheets.Add After:=ActiveSheet Set srngDataRow = ActiveSheet.Range("A1:D1") srngDataRow.Value = Split("Target Cell:Target Sheet:Source Cell:Source Sheet", ":") Set srngDataRow = srngDataRow.Offset(1) lngBegin = InStr(1, Prompt, "]") + 1 lngEnd = InStr(lngBegin, Prompt, "'") sstrTargetSheet = Mid$(Prompt, lngBegin, lngEnd - lngBegin) srngDataRow.Cells(i_TargetSheet) = sstrTargetSheet lngBegin = InStr(lngEnd, Prompt, "$") + 1 lngEnd = InStr(lngBegin, Prompt, " ") sstrTargetCell = f.Substitute(Mid$(Prompt, lngBegin, lngEnd - lngBegin), "$", "") srngDataRow.Cells(i_TargetCell) = sstrTargetCell lngBegin = InStr(lngEnd, Prompt, ":") + 3 lngEnd = InStr(lngBegin, Prompt, " ") slngClosedBookCount = Val(Mid$(Prompt, lngBegin, lngEnd - lngBegin)) lngBegin = InStr(lngEnd, Prompt, ".") + 2 lngEnd = InStr(lngBegin, Prompt, " ") slngOpenBookCount = Val(Mid$(Prompt, lngBegin, lngEnd - lngBegin)) lngBegin = InStr(lngEnd, Prompt, ".") + 2 lngEnd = InStr(lngBegin, Prompt, " ") slngSameBookCount = Val(Mid$(Prompt, lngBegin, lngEnd - lngBegin)) lngBegin = InStr(lngEnd, Prompt, ".") + 2 lngEnd = InStr(lngBegin, Prompt, " ") slngSameSheetCount = Val(Mid$(Prompt, lngBegin, lngEnd - lngBegin)) slngState = slngState + 1 MsgBoxInterceptor = vbNo Case 1: ' Get same book sources lngEnd = InStr(1, Prompt, "[") For i = 1 To slngSameBookCount srngDataRow.Cells(i_TargetCell) = sstrTargetCell srngDataRow.Cells(i_TargetSheet) = sstrTargetSheet lngBegin = InStr(lngEnd, Prompt, "]") + 1 lngEnd = InStr(lngBegin, Prompt, "'") srngDataRow.Cells(i_SourceSheet) = Mid$(Prompt, lngBegin, lngEnd - lngBegin) lngBegin = InStr(lngEnd, Prompt, "$") + 1 lngEnd = InStr(lngBegin, Prompt, Chr$(13)) srngDataRow.Cells(i_SourceCell) = f.Substitute(Mid$(Prompt, lngBegin, lngEnd - lngBegin), "$", "") Set srngDataRow = srngDataRow.Offset(1) Next i For i = 1 To slngSameSheetCount srngDataRow.Cells(i_TargetCell) = sstrTargetCell srngDataRow.Cells(i_TargetSheet) = sstrTargetSheet lngBegin = InStr(lngEnd, Prompt, "]") + 1 lngEnd = InStr(lngBegin, Prompt, "'") srngDataRow.Cells(i_SourceSheet) = Mid$(Prompt, lngBegin, lngEnd - lngBegin) lngBegin = InStr(lngEnd, Prompt, "$") + 1 lngEnd = InStr(lngBegin, Prompt, Chr$(13)) If lngEnd = 0 Then lngEnd = Len(Prompt) + 1 srngDataRow.Cells(i_SourceCell) = f.Substitute(Mid$(Prompt, lngBegin, lngEnd - lngBegin), "$", "") Set srngDataRow = srngDataRow.Offset(1) Next i slngState = slngState + 1 MsgBoxInterceptor = vbOK Case 2: ' Just skipping through slngState = slngState + 1 MsgBoxInterceptor = vbYes Case 3: 'Get other book sources (STILL TODO) lngEnd = InStr(1, Prompt, "") For i = 1 To slngClosedBookCount srngDataRow.Cells(i_TargetCell) = sstrTargetCell srngDataRow.Cells(i_TargetSheet) = sstrTargetSheet ' lngBegin = InStr(lngEnd, Prompt, "]") + 1 ' lngEnd = InStr(lngBegin, Prompt, "'") ' srngDataRow.Cells(i_SourceSheet) = Mid$(Prompt, lngBegin, lngEnd - lngBegin) ' ' lngBegin = InStr(lngEnd, Prompt, "$") + 1 ' lngEnd = InStr(lngBegin, Prompt, Chr$(13)) ' srngDataRow.Cells(i_SourceCell) = f.Substitute(Mid$(Prompt, lngBegin, lngEnd - lngBegin), "$", "") Set srngDataRow = srngDataRow.Offset(1) Next i For i = 1 To slngOpenBookCount srngDataRow.Cells(i_TargetCell) = sstrTargetCell srngDataRow.Cells(i_TargetSheet) = sstrTargetSheet ' lngBegin = InStr(lngEnd, Prompt, "]") + 1 ' lngEnd = InStr(lngBegin, Prompt, "'") ' srngDataRow.Cells(i_SourceSheet) = Mid$(Prompt, lngBegin, lngEnd - lngBegin) ' ' lngBegin = InStr(lngEnd, Prompt, "$") + 1 ' lngEnd = InStr(lngBegin, Prompt, Chr$(13)) ' If lngEnd = 0 Then lngEnd = Len(Prompt) + 1 ' srngDataRow.Cells(i_SourceCell) = f.Substitute(Mid$(Prompt, lngBegin, lngEnd - lngBegin), "$", "") Set srngDataRow = srngDataRow.Offset(1) Next i slngState = slngState + 1 MsgBoxInterceptor = vbOK Case 4: ' Finished -> tidy up srngDataRow.EntireColumn.AutoFit slngState = 0 MsgBoxInterceptor = vbCancel Case Else End Select End Function
Explanation:
, Static . , VBA . , , .
- MsgBox .