Type mismatch error when counting words/text with more than 255 characters

Nick70

Active Member
Joined
Aug 20, 2013
Messages
306
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a code that looks at text in a range in column B and then counts how many times this text is found in column D (see code below)

VBA Code:
Sub FindTotal()
    Application.ScreenUpdating = False
    Dim rng As Range, total As Long
    For Each rng In Range("B4", Range("B" & Rows.Count).End(xlUp))
        total = total + WorksheetFunction.CountIf(Range("D4", Range("D" & Rows.Count).End(xlUp)), rng)
    Next rng
    MsgBox total
    Application.ScreenUpdating = True
End Sub

I have found that if a text in column B is more than 255 letters (so 256 letters or more) then the code does not work and gives and gives a Run-time error '13': Type mismatch.

Does anybody know how I can fix the code so that it also counts text with more then 255 letters and I no longer get the type mismatch error?

Thanks,
Nic :unsure:
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi Nic,

On your worksheet you could try a formula like this...or you could possibly adapt it to VBA.
Excel Formula:
=COUNTA(FILTER($D$4:$D$80,ISNUMBER(SEARCH(B4,$D$4:$D$80,1))))
I tested on a very small sample and it seemed to work for long strings.

Hope that helps,

Doug
 
Upvote 0
Thanks Doug but when I tried it it still stops at 255 characters after that it stops counting.
 
Upvote 0
Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.

Thanks,

Doug
 
Upvote 0
You could try to use SUMPRODUCT like below...
Book_2023-09-10.xlsm
BCDEFG
2Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.353Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.3532
3please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.345please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.3454
4Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.353
5please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.345
6please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.345
7please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information. Can you please provide some sample data? If you can, use the XL2BB tool to share the data. That way we can copy and paste it into our Excel. and work with the same information.345
Sheet10
Cell Formulas
RangeFormula
C2:C3,E2:E7C2=LEN(B2)
G2:G3G2=SUMPRODUCT(--($D$2:$D$7=$B2))


Hope that helps,

Doug
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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