Adapting code to return cell values rather than cell references

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
I have been using the following code successfully to return any cell references in a formula that are "not allowed". Is there a way to adapt it so that it returns the values in the cell references that are "not allowed" rather than the cell references themselves?

Code:
Sub HelperCells(fCell As String, rCell As String)                 '
  Dim xRegEx As Object, xMatch As Object, d As Object
  Dim allowed As String, Sheetname As String, s As String
  
Dim addresses As String
Dim c As Integer
Dim r As Integer
Dim n As Integer
Dim addaray As Variant

allowed = "C9,E9,G9,C10,E10,G10,C11,E11,G11," & _
"C12,G12,C14,"

  Sheetname = ActiveSheet.Name & "!"
  Set d = CreateObject("Scripting.Dictionary")
  Set xRegEx = CreateObject("VBSCRIPT.REGEXP")
  With xRegEx
    .Pattern = "('?[a-zA-Z0-9\s\[\]\.]{1,99})?'?!?\$?[A-Z]{1,3}\$?[0-9]{1,7}(:\$?[A-Z]{1,3}\$?[0-9]{1,7})?"
    .Global = True
    For Each xMatch In .Execute(Replace(Replace(Replace(Replace(Range(fCell).Formula, ":", ","), "$", ""), " ", ""), Sheetname, ""))
      If InStr(1, "," & allowed & ",", "," & xMatch & ",") = 0 And Not d.Exists(CStr(xMatch)) Then
          s = s & ", " & xMatch
          d(CStr(xMatch)) = 1
      End If
    Next xMatch
  End With
  
  addresses = Mid(s, 3)
  
  If addresses = "" Then
    addresses = addresses
 
Else
addresses = "=SUM(" & Mid(s, 2) & ")"
End If
Range(rCell).Value = addresses
addaray = Split(addresses, ",")
End Sub

Original code provided by Peter_SSs at
(Thanks!)
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this:

VBA Code:
Sub HelperCells(fCell As String, rCell As String)                 '
    
    Dim xRegEx As Object, xMatch As Object, d As Object
    Dim allowed As String, Sheetname As String, s As String, addaray() As String
    
    allowed = "C9,E9,G9,C10,E10,G10,C11,E11,G11,C12,G12,C14"
    Sheetname = ActiveSheet.Name & "!"
    Set d = CreateObject("Scripting.Dictionary")
    Set xRegEx = CreateObject("VBSCRIPT.REGEXP")
    
    On Error Resume Next
    Range("MyChecks").ClearContents
    On Error GoTo 0
    
    With xRegEx
        .Pattern = "('?[a-zA-Z0-9\s\[\]\.]{1,99})?'?!?\$?[A-Z]{1,3}\$?[0-9]{1,7}(:\$?[A-Z]{1,3}\$?[0-9]{1,7})?"
        .Global = True
        For Each xMatch In .Execute(Replace(Replace(Replace(Replace(Range(fCell).Formula, ":", ","), "$", ""), " ", ""), Sheetname, ""))
            If InStr(1, "," & allowed & ",", "," & xMatch & ",") = 0 And Not d.Exists(CStr(xMatch)) Then
                s = s & "," & xMatch
                d(CStr(xMatch)) = 1
            End If
        Next xMatch
    End With
      
    If Len(s) Then
        addaray = Split(Mid(s, 2), ",")
        With Range(rCell).Resize(, UBound(addaray) + 1)
            .Value = addaray
            .Name = "MyChecks"
            .Cells(2, 1).Formula2 = "=CELL(""Contents"",INDIRECT(MyChecks))"
        End With
    Else
        Range(rCell).Value = "Looks OK"
        Range(rCell).Offset(1).ClearContents
    End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,197
Messages
6,123,585
Members
449,108
Latest member
rache47

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