VBA not performing due to data format mismatch

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
See below code to check for the existence of "mid". My problem is that "mid" is a whole number, and vba is picking up "C" as a scientific number, effectively triggering the MsgBox and Exit Sub regardless if a match is found or not.
How can I have vba pick up "C" as a whole number?

VBA Code:
Sub findmid()

    Dim lastrow As Long
    Dim mid As Variant
    Dim C As Range
    
    With ActiveSheet
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    mid = Application.InputBox("Enter mid or select cell containing the mid.", "Analyze it")
    For Each C In Range("$B$2:$B$" & lastrow)
        If C.Value = mid = False Then
            MsgBox "Invalid Mid or Mid not found!", vbOKOnly, "Analyze it"
            Exit Sub
        End If
    Exit For
    Next C

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Firstly, do NOT use reserved words like "MID" as names of variables, functions, or procedures. Reserved words are words already used by Excel for functions, properties, and methods.
Using reserved words can lead to errors and unexpectd results.

Can you provide us with a small sample of what the entries you have in column B look like, and what you are entering in to your Input Box?
 
Upvote 0
Have you tried using .Formula instead of .Value?
VBA Code:
If C.Formula = mid = False Then
 
Upvote 0
Solution
Firstly, do NOT use reserved words like "MID" as names of variables, functions, or procedures.
Lol, totally didn't realize I'm using something reserved, I mean MID as the acronym for "Merchant IDentification number"
 
Upvote 0
Lol, totally didn't realize I'm using something reserved, I mean MID as the acronym for "Merchant IDentification number"
You never heard of the "MID" function (similar to the LEFT and RIGHT functions)?
Just something you need to be aware of. I often preface my variables with the prefix "My" to avoid that from happening.
 
Upvote 0
You never heard of the "MID" function (similar to the LEFT and RIGHT functions)?
Just something you need to be aware of. I often preface my variables with the prefix "My" to avoid that from happening.
I did, and I've used it too... I just didn't realize when I wrote it.
Its actually an error I make quite often with other reserved words... Using "My" sounds like a great idea.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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