VBA puts -@ at the beginning of formula instead of =

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have a VBA code that generates a filter formula. The code will update the formula if a new column is added to a table. When the code runs, it changes the = at the begging of the formula to =@. Does anyone know why this is happening or how to fix it? Below is the code:

VBA Code:
Sub UpdateFilterFormula()
    Dim wsRawData As Worksheet
    Dim wsFiltered As Worksheet
    Dim lastColumn As Integer
    Dim formula As String
    Dim colName As String
   
    ' Set references to the "Raw Data" and "Filtered" sheets
    Set wsRawData = ThisWorkbook.Worksheets("Raw Data")
    Set wsFiltered = ThisWorkbook.Worksheets("Filtered")
   
    ' Find the last column in "Table1" in the "Raw Data" sheet
    lastColumn = wsRawData.ListObjects("Table1").ListColumns.Count
   
    ' Initialize the formula without the "=" sign
    formula = "FILTER(Table1, "
   
    ' Loop through the columns in "Table1" and add them to the formula
    For i = 1 To lastColumn
        colName = wsRawData.ListObjects("Table1").ListColumns(i).Name
        formula = formula & "ISNUMBER(SEARCH(B2, Table1[" & colName & "]))"
       
        ' Add a "+" if it's not the last column
        If i < lastColumn Then
            formula = formula & "+"
        End If
    Next i
   
    ' Add the final part of the formula
    formula = formula & ",""No Match"")"
   
    ' Set the Formula property with "=" to ensure it starts with "="
    wsFiltered.Range("B5").formula = "=" & formula
End Sub
 
Last edited by a moderator:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,215,085
Messages
6,123,030
Members
449,092
Latest member
ikke

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