(Cross-posted to Microsoft: Office Forum, which I believe is a distinct audience from this)
I'm looking for a C.Y.A. routine to alert me when I have Excel columns that are too narrow, causing ##### to print. I'm hoping that someone has an ingenious solution, though this might require hooking O/S internals; I'm looking for a V.B.A. solution, not C code for this.
For an example of the process, help yourself to this *deeply* invaluable code below which I use to "proofread" spreadsheets before release. You will probably want to think about which way to set the 2 Booleans. (I manage them via Form input, stripped from the code below.)
The code below has saved me countless times. If you have a ##### solution or other useful "C.Y.A." techniques, feel free to holler in.
Option Explicit
Function FindErrorsCurrentSheet() As Boolean
'Search every cell in this worksheet; stop code on first hit of #REF or any other error
'Preserves selection if no errors, but loses it if it finds errors outside current selection
Dim c As Range, bIGNORE_NOT_APPLIC As Boolean, CARR_RET As String * 2 '(carriage return)
CARR_RET = Chr(10) ' & Chr(13) unnecessary
FindErrorsCurrentSheet = True
'bIGNORE_NOT_APPLIC=true 'toggle this sucker here or immediate win. when too many Stops hamper you
For Each c In ActiveSheet.UsedRange.Cells
If IsError(c) Then
'c.Interior.ColorIndex = 7 'highlight all matches in purple
c.Activate
If (Not bIGNORE_NOT_APPLIC) Or (bIGNORE_NOT_APPLIC And (c.Text <> "#N/A") Then
'comment next if bothersome - pauses execution
If vbCancel = MsgBox("Found - hit enter or click OK to" & CARR_RET & "continue to next; else escape key" _
, vbOKCancel, "Error found" Then FindErrorsCurrentSheet = False: Exit Function
End If
End If
Next
End Function
Sub FindErrorsAllSheets()
'Search all or rest of worksheets in workbook; stop code on first hit of #REF or any other error
'Preserves selection on sheets without errors, but loses it on those that have errors outside current selection
'Checks entire sheets regardless of selection; active sheet loses focus
Dim wksht As Worksheet
Dim bDoAllSheets As Boolean 'TRUE to start from sheet 1, else from current sheet
If bDoAllSheets Then
For Each wksht In Sheets
wksht.Activate
If Not FindErrorsCurrentSheet Then Exit Sub
Next wksht
Else
While True
If Not FindErrorsCurrentSheet Then Exit Sub
On Error GoTo err1FindErrorsAllSheets
ActiveSheet.Next.Select
On Error GoTo 0
Wend
End If
Set wksht = Nothing
Exit Sub
err1FindErrorsAllSheets: 'don't leave mad - just leave
End Sub
I'm looking for a C.Y.A. routine to alert me when I have Excel columns that are too narrow, causing ##### to print. I'm hoping that someone has an ingenious solution, though this might require hooking O/S internals; I'm looking for a V.B.A. solution, not C code for this.
For an example of the process, help yourself to this *deeply* invaluable code below which I use to "proofread" spreadsheets before release. You will probably want to think about which way to set the 2 Booleans. (I manage them via Form input, stripped from the code below.)
The code below has saved me countless times. If you have a ##### solution or other useful "C.Y.A." techniques, feel free to holler in.
Option Explicit
Function FindErrorsCurrentSheet() As Boolean
'Search every cell in this worksheet; stop code on first hit of #REF or any other error
'Preserves selection if no errors, but loses it if it finds errors outside current selection
Dim c As Range, bIGNORE_NOT_APPLIC As Boolean, CARR_RET As String * 2 '(carriage return)
CARR_RET = Chr(10) ' & Chr(13) unnecessary
FindErrorsCurrentSheet = True
'bIGNORE_NOT_APPLIC=true 'toggle this sucker here or immediate win. when too many Stops hamper you
For Each c In ActiveSheet.UsedRange.Cells
If IsError(c) Then
'c.Interior.ColorIndex = 7 'highlight all matches in purple
c.Activate
If (Not bIGNORE_NOT_APPLIC) Or (bIGNORE_NOT_APPLIC And (c.Text <> "#N/A") Then
'comment next if bothersome - pauses execution
If vbCancel = MsgBox("Found - hit enter or click OK to" & CARR_RET & "continue to next; else escape key" _
, vbOKCancel, "Error found" Then FindErrorsCurrentSheet = False: Exit Function
End If
End If
Next
End Function
Sub FindErrorsAllSheets()
'Search all or rest of worksheets in workbook; stop code on first hit of #REF or any other error
'Preserves selection on sheets without errors, but loses it on those that have errors outside current selection
'Checks entire sheets regardless of selection; active sheet loses focus
Dim wksht As Worksheet
Dim bDoAllSheets As Boolean 'TRUE to start from sheet 1, else from current sheet
If bDoAllSheets Then
For Each wksht In Sheets
wksht.Activate
If Not FindErrorsCurrentSheet Then Exit Sub
Next wksht
Else
While True
If Not FindErrorsCurrentSheet Then Exit Sub
On Error GoTo err1FindErrorsAllSheets
ActiveSheet.Next.Select
On Error GoTo 0
Wend
End If
Set wksht = Nothing
Exit Sub
err1FindErrorsAllSheets: 'don't leave mad - just leave
End Sub