How to use the Substitute Function in VBA - I'm struggling to get code right

netrixuser

Board Regular
Joined
Jan 21, 2019
Messages
77
Office Version
  1. 365
Platform
  1. Windows
I receive a csv file on a daily basis where I need to count the number of times a user has an entry where there are 4 or more commas in a particular column.
I received help from this site in another thread where I used the Substitute function to to replace Commas with "nothing" and then subtracted the original Length from the Substituted length using Len. I went on to shade cells where the Comma count was 4 or more. - All good.
That was in Excel I now need to use similar code in VBA to keep a running total for users where a cell has 4 or more commas in it.

For mock up sheet below:
For each user in column C (User) I need to look in Column D (Call ID) and count the number of times that there are 4 or more commas.
For Clarity - the actual csv file will have hashed numbers separated by a comma, not just commas - similar to this:
17f4ccadbb664c8988f2d275ad545335:8DAF21CD89D89A5, 319fe73aa0aa44fd8aea47c060b841b2:8DAF21D0454EC02, 3aeec5b125dd4ea5acaa3b0caac7762f:8DAF219692151C4, 489928d43e2f4f129027ae58988416f4:8DAF218F7C57D72, 551edc48b19b45f1ab52aea502b0a58f:8DAF21A6386C63C, 8d39898ed8124d9aa299168b5987e1ee:8DAF218AF85095B, b5acbf712a064538a354c84ff7ee74e4:8DAF21D2FD38695
Once I have the count I need to place the number, as a running total, in Column H (Commas) - next to the User Name.
RecordingUserCall IDActive UsersMP4NothingCommasCdT
aswed.mp4Bill,,,,,,Chris
.mp4Bill,,Barry
.opusCharlie,,Bill2
.mp4Charlie,,,,Nicolas
.opusCharlie,,,,Charlie1
.mp4Harry,Frank
.opusSophie,Robin
.opusSophie,,,,Gill1
.Gill,,Hasan
.Pete,,,Harry1
.mp4Barbara,,,,Sophie
.Barbara,,,,Paul
.Barbara,,,,Pete1
.Barbara,George
.mp4Barbara,Barbara23
.mp4Cyril,,Cyril12

I have this working code that Counts the number of times a user has a Filename that ends in ".mp4" (Filename is in Column B - "Recording") and places a running total in column F (MP4)

VBA Code:
For Each U In UsIDRnge
            
If Right(U.Offset(0, -1), 4) = ".mp4" Then
  UsidPlace = "F" & Application.Match(U, LookupRange, 0)
   CntMP4 = CntMP4 + 1
       Range(UsidPlace).Value = Range(UsidPlace).Value + CntMP4
   CntMP4 = 0
End If
 Next U


As you can see in the spreadsheet - the users who have ".mp4" in Column B have numbers, against their names, in Columns F (MP4)
This is a running total, so if I use the code on another csv file, Bill, for example, who has 2 entries of .mp4 above, might have 4 more instances of .mp4 so he would then have a running total of 6
As mentioned the above code is working

When I try to modify the IF statement above to use the Substitute function I get an error
I am trying to count the number of Commas in Column D and keep the total of those in Column H

VBA Code:
        For Each U In UsIDRnge
        
If Len(U.Offset(0, 1)) - Len(WorksheetFunction.Substitute(U.Offset(0, 1), "", "", """")) > 4 Then
  UsidPlace = "H" & Application.Match(U, LookupRange, 0)
     CntComma = CntComma + 1
        Range(UsidPlace).Value = Range(UsidPlace).Value + CntComma
     CntComma = 0
End If

 Next U


I tried to use hard coded cell address instead of Variable and Offset but get the same error

VBA Code:
If Len("D2") - Len(Application.WorksheetFunction.Substitute("D2", "", "", """")) > 4 Then



The error is Runtime Error 1004

"Unable to get the Substitute property of the WorkSheetFunction class" at the line of code above

Wondering if it cannot be done this way in VBA and I need another approach to count the number of times the cells containing Commas have more than 4 of them ?

All help gratefully received,

below is the full code including DIM and SET statements

[The two working IF statements are currently "remarked out"]
VBA Code:
Sub Test_Count_Number_of_Alerts()
Dim U As Variant
Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim UsID As Variant 'User

Dim lastRow As Long
    lastRow = Cells(Rows.Count, 4).End(xlUp).Row
Dim UsIDRnge As Range
    Set UsIDRnge = ws.Range("C2:C" & lastRow) 'range containing End User IDs
         
Dim LookupRange As Range
Set LookupRange = ws.Range("E1:E50") 'Range containing All users
    

Dim UsidPlace As Variant 'Variable - to store location of count
'Dim i As Long

Dim CntMP4 As Long, CntBlank As Long, CntCDT As Long, CntComma As Long
CntMP4 = 0
CntBlank = 0
CntCDT = 0
CntComma = 0

        For Each U In UsIDRnge
        
        
'
'
'               If Right(U.Offset(0, -1), 4) = ".mp4" Then
'                    UsidPlace = "F" & Application.Match(U, LookupRange, 0)
'                        CntMP4 = CntMP4 + 1
'                             Range(UsidPlace).Value = Range(UsidPlace).Value + CntMP4
'                        CntMP4 = 0
'                End If
'

'                If Right(U.Offset(0, -1), 4) <> ".mp4" And Right(U.Offset(0, -1), 4) <> "opus" Then
'                     UsidPlace = "G" & Application.Match(U, LookupRange, 0)
'                        CntBlank = CntBlank + 1
'                            Range(UsidPlace).Value = Range(UsidPlace).Value + CntBlank
'                        CntBlank = 0
'                End If

                If Len(U.Offset(0, 1)) - Len(WorksheetFunction.Substitute(U.Offset(0, 1), "", "", """")) > 4 Then
                    UsidPlace = "H" & Application.Match(U, LookupRange, 0)
                        CntComma = CntComma + 1
                            Range(UsidPlace).Value = Range(UsidPlace).Value + CntComma
                        CntComma = 0
                End If

        Next U

Set ws = Nothing
Set UsIDRnge = Nothing
Set LookupRange = Nothing
End Sub
 
I went with the Substitute option for now as it will enable me to quickly move onto finish the code, plan to try out the Split method as I think that may be quicker when I get sheets with a couple of thousand rows to process.
Thanks again
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,223,948
Messages
6,175,571
Members
452,652
Latest member
eduedu

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