VBA Macro to type a formula in a cell throws Type missmatch error

ChrisMd

New Member
Joined
Jan 5, 2025
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to type the below formula to a cell :
VBA Code:
 "=TEXTJOIN("","",TRUE,IF(ISNUMBER(" _
                & "SEARCH(B" & i & "," & descRng & ")),ROW(" & descRng & _
                "),""""))"
The formula returns the row index separated by commas of all the occurrences found.

Captura de pantalla 2025-01-05 182954.png

The searched values are in column B of the below screenshot ("HTS Codes for PNs wo HTS.xlsx") while the second screenshot ("HTS Codes for PNs wo HTS.xlsx") shows the range in which the values should be searched.

"HTS Codes for PNs wo HTS.xlsx" "HTS Codes for PNs wo HTS.xlsx"
Captura de pantalla 2025-01-05 182930.png
Captura de pantalla 2025-01-05 182913.png


I am getting a Type missmatch error. I cannot figure what is causing the error. I would appreciate some guidance towards the solution.

Thanks

VBA Code:
Sub test ()
    Dim bomWb As Workbook, htsWb As Workbook
    Dim bomWs As Worksheet, htsWs As Worksheet
    Dim bLrow As Long, hLrow As Long

'lrow and i are private variables declared within the same module

    Dim descRng As Range
    Set bomWb = Workbooks("BOM_1926_GM_09.11.24.xlsx")
    Set bomWs = bomWb.Worksheets(1)
    Set htsWb = Workbooks("HTS Codes for PNs wo HTS.xlsx")
    Set htsWs = htsWb.Worksheets(1)
    bLrow = 755
    hLrow = 103
    Set descRng = bomWs.Range(Cells(2, 6), Cells(bLrow, 6))
    With htsWs
        For i = 2 To hLrow
            .Cells(i, 5).Formula = "=TEXTJOIN("","",TRUE,IF(ISNUMBER(" _
                & "SEARCH(B" & i & "," & descRng & ")),ROW(" & descRng & _
                "),""""))"
        Next i
    End With
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Welcome to the Forum!

Given that descRng points to another workbook/worksheet, you need to fully qualify the reference.

One way to do this would be be to redefine descRng as follows (and also note the use of .Formula2 rather than .Formula):

VBA Code:
Dim descRng As String

'....
    
descRng = "'[" & bomWb.Name & "]" & bomWs.Name & "'!F$2:F$" & bLrow

With htsWs
    For i = 2 To hLrow
        .Cells(i, 5).Formula2 = "=TEXTJOIN("","",TRUE,IF(ISNUMBER(" _
            & "SEARCH(B" & i & "," & descRng & ")),ROW(" & descRng & _
            "),""""))"
    Next i
End With

And you don't need to loop through each row:

Code:
htsWs.Range("E2:E" & hLrow).Formula2 = "=TEXTJOIN("","",TRUE,IF(ISNUMBER(" _
    & "SEARCH(B2," & descRng & ")),ROW(" & descRng & "),""""))"
 
Upvote 0
Solution
Welcome to the Forum!

Given that descRng points to another workbook/worksheet, you need to fully qualify the reference.

One way to do this would be be to redefine descRng as follows (and also note the use of .Formula2 rather than .Formula):

VBA Code:
Dim descRng As String

'....
   
descRng = "'[" & bomWb.Name & "]" & bomWs.Name & "'!F$2:F$" & bLrow

With htsWs
    For i = 2 To hLrow
        .Cells(i, 5).Formula2 = "=TEXTJOIN("","",TRUE,IF(ISNUMBER(" _
            & "SEARCH(B" & i & "," & descRng & ")),ROW(" & descRng & _
            "),""""))"
    Next i
End With

And you don't need to loop through each row:

Code:
htsWs.Range("E2:E" & hLrow).Formula2 = "=TEXTJOIN("","",TRUE,IF(ISNUMBER(" _
    & "SEARCH(B2," & descRng & ")),ROW(" & descRng & "),""""))"

Thank you for your help, Stephen.

I need to loop because I want the formula to be in every cell of the range. The searched value "B2" should change in each iteration.
 
Upvote 0
Thank you for your help, Stephen.

I need to loop because I want the formula to be in every cell of the range. The searched value "B2" should change in each iteration.
Nevermind, I just tried it. I did not know the cell updates automatically. Thank you!
 
Upvote 0

Forum statistics

Threads
1,225,234
Messages
6,183,761
Members
453,188
Latest member
amenbakr

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