1

Here is my code that I am working with. It is really simple, but still not working as expected.

Dim hey As Range
Set hey = Range("A1:A5")

For Each i In hey

If i.Value = "15" Then
i.Offset(0, 2).Value = "15" And i.Offset(0, 3).Value = "15"

ElseIf i.Value = 10 Then
i.Offset(0, 2).Value = "10" And i.Offset(0, 3).Value = "10"

ElseIf i.Value = 5 Then
i.Offset(0, 2).Value = "5" And i.Offset(0, 3).Value = "5"

ElseIf i.Value > 15 And i.Value = "*5" Then
i.Offset(0, 2).Value = "10" And i.Offset(0, 3).Value = "5"

Else: i.Offset(0, 2).Value = 10 And i.Offset(0, 3).Value = 10
End If
Next i

Right now, all it will do is offset by 2 columns and output 0. I cannot distinguish why and I am at the end of my mental rope with this. I'm sure it is something retardedly simple I am overlooking.

I've tried stepping through and it looks like it evaluates properly for each value in my sample, but still gives me the 0 value. Also, I was playing around with the quotations to see if that had to do with it, but they are strings I want to work with.

Thanks for any help.

4
  • 2
    Take out your And statements outside of the if statements. Break each piece into two lines. Commented Aug 16, 2016 at 13:15
  • I'm no VBA expert but your final Else clause is missing apostrophes for the values Commented Aug 16, 2016 at 13:15
  • 8
    Ok, first it look like a nice place where to use Select Case instead of If. Second you are mixing numeric values ant text value, are your cells filled with numbers or text? Could you give a sample of inputs? Third, i.Offset(0, 2).Value = "15" And i.Offset(0, 3).Value = "15" means put in i.Offset(0, 2).Value the result of ` "15" And i.Offset(0, 3).Value = "15"`, that is probably not what you try to do. Commented Aug 16, 2016 at 13:18
  • This question needs clarification on whether the values in the cells are real numbers (e.g. 15) or text-that-look-like-a-number (e.g. "15"). They are not the same thing and boolean comparisons may not produce the correct results unless the proper comparison is made. Commented Aug 16, 2016 at 15:19

2 Answers 2

6

This is what I mean by removing And Statements:

Dim hey As range
Set hey = range("A1:A5")

For Each i In hey

If i.Value = "15" Then
i.Offset(0, 2).Value = "15"
i.Offset(0, 3).Value = "15"

ElseIf i.Value = 10 Then
i.Offset(0, 2).Value = "10"
i.Offset(0, 3).Value = "10"

ElseIf i.Value = 5 Then
i.Offset(0, 2).Value = "5"
i.Offset(0, 3).Value = "5"

ElseIf i.Value > 15 And i.Value = "*5" Then
i.Offset(0, 2).Value = "10"
i.Offset(0, 3).Value = "5"

Else
i.Offset(0, 2).Value = 10
i.Offset(0, 3).Value = 10
End If
Next i

EDIT:

To a computer program, And is a comparison operator. It does not understand it the exact same way humans do. When discussing with another human, we can give a string of tasks linked together by 'and,' and there will be no confusion on what to do (unless the tasks themselves are confusing!). A computer uses And to check if multiple conditions are true at the same time. In your last Elseif you properly use this to tell the computer: "Check if i.Value > 15 AND Check if i.Value = "*5". If BOTH (because of the And statement) conditions are true, then run the following code."

Hopefully this helps explain the difference!

EDIT 2:

From @litelite 's suggestion, use the Like operator.

ElseIf i.Value > 15 And i.Value Like "*5" Then
i.Offset(0, 2).Value = "10"
i.Offset(0, 3).Value = "5"
Sign up to request clarification or add additional context in comments.

7 Comments

I think it would be a good idea to explain to OP what and really means.
Ah yes, I've done this before with the and statement. Logically to me it makes sense, of course, but to the code I remember why that doesn't work out. The code above worked just fine, except for the ElseIf i.Value > 15 And i.Value = "*5" Then line did not evaluate properly.
@litelite Thanks for the suggestion, added it in!
@SsaSnakebite What should that ElseIf do?
@SsaSnakebite You cannot use wildcard in equaliity. it will check if there is literraly a star. check out the like operator
|
1

Just to anwser off the back of @PartyHatPanda. You could use ElseIf i.Value > 15 And Right(i.Value, 1) = 5 Then. This Check if i.value's last digit is equal to 5. I this what you want?

2 Comments

I cant comment sorry but I'm pretty sure that what OP is after
I used the above suggestions of the Like operator and it works well enough for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.