Mismatch Error in vba code

Shen1986

New Member
Joined
Apr 13, 2021
Messages
5
Office Version
  1. 2016
Hi,

I have created this macro:

VBA Code:
Sub Macro

    Dim wsData As Worksheet
    Dim wsResult As Worksheet
  
    Set wsData = ThisWorkbook.Worksheets("DATA")
    Set wsResult = ThisWorkbook.Worksheets("Result")
 

      counter1 = 2
                       

    If wsData.Cells(counter1, 2).Value = "Problem" OR wsData.Cells(counter1, 2).Value = "Bad" Then
          wsSheet1.Cells(wsResult, 4).Value = "NOT"
                             
  
                           End If


End Sub

I always get a mismatch. The goal of the macro is to get from one sheet where are in column 2 are many many words like BAD, PROBLEM, STEVE, ROGER etc.. and only those with BAD and PROBLEM should transfer to the next sheet. The problem is that I get a mismatch in this line: If wsData.Cells(counter1, 2).Value = "Problem" OR wsData.Cells(counter1, 2).Value = "Bad".

The code is actually longer and it works for other parts in other columns where I used the command Like but when I try it for that column it does not work and I get a mismatch even when I use like for only one word example BAD.

I hope someone can help me.

Thank you for reading this and your time.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
It's more likely this line will give you a type mismatch, since wsResult is not a Long type variable.
Rich (BB code):
Set wsResult = ThisWorkbook.Worksheets("Result")
wsSheet1.Cells(wsResult, 4).Value = "NOT"
 
Upvote 0
What do you define as a mismatch? Is it a type mismatch error as @GWteB has identified or is it simply not doing as expected?

One thing to bear in mind, noting the differing cases of the examples in your post is that If in vba is case sensitive so "Bad" and "BAD" are not the same.
 
Upvote 0
Thanks for the replies figured it out, stupid me.

It only needed to be changed from Value to Text and now it works perfectly.

Thanks a lot for the replies once more sorry for that stupid mistake.
 
Upvote 0
That would suggest that you have error values in col B, such as #N/A, #VALUE etc.
 
Upvote 0

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

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