VBA If statement not reading variable value correctly

epactheactor

New Member
Joined
Sep 9, 2015
Messages
38
I'm having an issue that I've never come across. For some reason it is as if VBA isn't reading what the value inside a variable is.


I've posted the variables and part of the code that is giving the grief below as the entire code is pretty long.

The code defaults to the first if statement. I've gone step by step and VBA will show the correct value of the manufYear variable, but it continues to default to the first if statement despite the value. I've seen it read that manufYear (1972) <= 1967.

What am I doing wrong? I would think it is the data type that is giving the issue, but I'm not having this issue with any other manuf in my code. I've changed the data type multiple time from Integer to Long, to Variant, etc.

Thank you for any help.

The values I've been using are:

seam = "HFERW"
manufYear = 1972
manuf = "J&L"

Code:
Dim manuf As String
Dim seam As String
Dim manufYear As Variant



ElseIf seam = "HFERW" And manufYear <= 1967 And manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l" Or manuf = "J & L" Or manuf = "j & l" Then
    A = 1
    B = 2.2
    C = 1
    D= 2.1
    E = 0.6
ElseIf seam = "HFERW" And manufYear >= 1968 And manufYear <= 1979 And manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l" Or manuf = "J & L" Or manuf = "j & l" Then
    A = 1
    B = 2.2
    C = 1
    D = 3.75
    E = 0.7
ElseIf seam = "HFERW" And manufYear >= 1980 And manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l" Or manuf = "J & L" Or manuf = "j & l" Then
    A = 1
    B = 2.2
    C = 1
    D = 4.6
    E = 0.7
ElseIf seam = "LFERW" And manufYear >= 1957 And manufYear <= 1965 And manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l" Then
    A = 1
    B = 1
    C = 1
    D = 1.5
    E = 0.2
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You need to be careful when you use AND and OR together! You will want to use parentheses, to determine the precedence. Otherwise, if it hits any of your OR statements, it will be treated as TRUE!
So, in your first line, the part in red is causing your entire thing to evaluate to TRUE:
Code:
ElseIf seam = "HFERW" And manufYear <= 1967 And manuf = "Jones & Laughlin" [COLOR=#ff0000]Or manuf = "J&L"[/COLOR] Or manuf = "j&l" Or manuf = "J & L" Or manuf = "j & l" Then
I am guessing that this is what you really meant:
Code:
If seam = "HFERW" And manufYear <= 1967 And (manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l" Or manuf = "J & L" Or manuf = "j & l") Then
    A = 1
    B = 2.2
    C = 1
    D = 2.1
    E = 0.6
ElseIf seam = "HFERW" And manufYear >= 1968 And manufYear <= 1979 And (manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l" Or manuf = "J & L" Or manuf = "j & l") Then
    A = 1
    B = 2.2
    C = 1
    D = 3.75
    E = 0.7
ElseIf seam = "HFERW" And manufYear >= 1980 And (manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l" Or manuf = "J & L" Or manuf = "j & l") Then
    A = 1
    B = 2.2
    C = 1
    D = 4.6
    E = 0.7
ElseIf seam = "LFERW" And manufYear >= 1957 And manufYear <= 1965 And (manuf = "Jones & Laughlin" Or manuf = "J&L" Or manuf = "j&l") Then
    A = 1
    B = 1
    C = 1
    D = 1.5
    E = 0.2
End If
Note the locations of the parentheses in all the IF statements.
 
Upvote 0

Forum statistics

Threads
1,224,910
Messages
6,181,680
Members
453,062
Latest member
blackyblack

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