VBA CountIf and Match Formulas not returning correct results (matching values)

brewitbold

New Member
Joined
Oct 13, 2016
Messages
5
Hi!

I have been using this forum for so long - yet never had the occasion to post myself.

I am working on a macro that will (among other tasks) determine if a string is present in a range located within the same sheet. I have attempted using the Application.Worksheetfunction.Match and Application.Worksheetfunction.CountIf functions to no avail. Here is the issue I am encountering: the below formula returns "Not New" for every record, even though I know that some of the strings DO NOT currently appear in the range below. This means that the CountIf function is returning > 0 for all records.

When I use the COUNTIF formula within the sheet it confirms this - some records return 1 and other records return 0. I have also tested this with MATCH and sure enough, the correct records will return the relative position of the string with the range, while others return an expected error.

I suspect the issue stems from how VBA reads the string vs. how the formula reads the string, but I have been banging my head against the wall on this error all day.

Thank you in advance.


Code:
Sub test_concatenation()


Dim i As Integer
Dim leaveUniqueId As String
Dim rng As Range
Dim idInArray As Integer
Dim recordStatus As Variant


Set rng = ActiveSheet.Range("o5:o50")


idInArray = Application.WorksheetFunction.CountIf(rng, leaveUniqueId)

For i = 2 To 18
    leaveUniqueId = (Cells(i, 1) & Cells(i, 4) & Cells(i, 5) & CLng(Cells(i, 7)))
    Cells(i, 8).Value = leaveUniqueId
        If idInArray = 0 Then
            recordStatus = "New Record"
        Else
            recordStatus = "Not New"
        End If
    Cells(i, 9).Value = recordStatus
        
 
Next


End


End Sub
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Welcome to the forum.

Your COUNTIF line needs to be inside the loop and after the line that assigns a Value to leaveUniqueId.

Also, you should probably remove that End line unless you put it there for a good reason.
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,677
Members
449,092
Latest member
tayo4dgacorbanget

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