Altering formula with VBA string functions

nyconfidential

New Member
Joined
Jul 22, 2015
Messages
49
Office Version
  1. 365
  2. 2016
Hi all - I'm trying to look at cell formulas in a few different worksheets and remove a file path reference if it exists(eg [=c:\test\Test.xlsm]Worksheet1'!rngTest would become =Worksheet1'!rngTest). Unfortunately, the string functions I normally use in Access vba do not work when dealing with a formula - I get a "Type Mismatch" error every time I encounter a cell with a formula in it. Is there a relatively simple way to edit the string within a formula without receiving that type mismatch error? Thanks in advance, code below...

Code:
Public Sub RemovePath()
Dim varray As Variant ' Variant array, cell data stored here
Dim WrkSht As Worksheet
Dim rRng As Range
Dim i As Long, j As Long
Dim SplitArray() As String ' Split array - will use to break up path string
Dim strFormula As String


For Each WrkSht In ActiveWorkbook.Worksheets
    Set rRng = WrkSht.UsedRange
    varray = rRng.Value
        For i = LBound(varray, 1) To UBound(varray, 1)
            For j = LBound(varray, 2) To UBound(varray, 2)
            Debug.Print varray(i, j)
             If InStr(varray(i, j), ".xlsm]") > 0 Then ' Look for "xlsm" as part of string
                SplitArray() = Split(varray(i, j), ".xlsm]") 'Split up string, use ".xlsm" as a delimiter
                varray(i, j) = Trim(SplitArray(UBound(SplitArray))) ' set the cell value to the workbook/docvar, without the file path
              End If
            Next j
        Next i
        rRng.Value = varray
    
    'Application.StatusBar = "Updating " & Str(WrkSht.Name)
    'Debug.Print WrkSht.Name
    WrkSht.Calculate
Next WrkSht




End Sub
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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