View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Jim May Jim May is offline
external usenet poster
 
Posts: 430
Default Finding constants in formulas

Norman:
This is soooo cool,,
Thanks for sharing the code.

Note my temporary modification of the output lines:
If Not rng2 Is Nothing Then
'do something e.g.:
MsgBox "Cells containing constants a " & rng2.Address
'Debug.Print rng2.Address
' rng2.Select ' use this line to highlight cells

Right now the above is producing the Messagebox with:

Cells containing constants a $B$10,$G$15,$G$20,$J$45

How could I change above lines to have to show in messagebox as:

Cells containing constants a
$B$10
$G$15
$G$20
$J$45

Is there a maximum a MsgBox can display (like this)?
Tks in advance,
Jim May


"Norman Jones" wrote in message
...
Hi Brian,

Try:

'=================
Sub ConstantsInFormulas()
Dim WB As Workbook
Dim SH As Worksheet
Dim rng As Range
Dim rng2 As Range
Dim rCell As Range
Dim arr As Variant
Dim sStr As String
Dim i As Long

Set WB = ActiveWorkbook '<<======== CHANGE
Set SH = WB.Sheets("Sheet1") '<<======== CHANGE
Set rng = SH.UsedRange '<<======== CHANGE

On Error Resume Next 'In case no formulas!
Set rng = rng.SpecialCells(xlFormulas)
On Error GoTo 0

arr = Array("/", "~*", "+", "-", "", "<", "=", "^", "[*]", "(")

If Not rng Is Nothing Then
For Each rCell In rng.Cells
For i = LBound(arr) To UBound(arr)
sStr = "*" & arr(i) & "[0-9]*"
If rCell.Formula Like sStr Then
If Not rng2 Is Nothing Then
Set rng2 = Union(rng2, rCell)
Else
Set rng2 = rCell
End If
End If
Next i
Next rCell
Else
'No formulas found
End If

If Not rng2 Is Nothing Then
'do something e.g.:
Debug.Print rng2.Address
rng2.Select
Else
MsgBox "No Formula constants found in " & SH.Name
End If

End Sub
'<<=================

---
Regards,
Norman



"Co-op Bank" wrote in message
...
Hello, I have a big spreadsheet which should be purely formula based
however
I need to check that there are no constants (i.e. hard coded values)
typed
into the formulas for example '=sum(a1:b10)+25000'.

Is there a way to check each cell reference to be totally sure that there
are no hardcodings? vb solution perhaps? I know you can switch to view
formulas on the spreadsheet but as there are so many and some are quite
long
it isnt always easy to spot via the naked eye. Any help much appreciated.

Thanks

Brian
Manchester, England