r/vba•
SMGiven

Lookup cell value in worksheet range - run one of two if/else functions based on result

Solved

Hi all,

Title might be a little confusing but I essentially have an operation that copies in rows from a data file and runs calculations on them to fill cells in the row with values.

The cell in question is a divisor. Right now I'm performing the same operation on every row, but I need to update to take one of the values, look it up in a range in a worksheet, and if present, run a different version of this divisor.

So like, as each row is copied in, lookup static value as a reference in column E (Case is ColAttr35) to a range in another worksheet (ValueSheet). If present, divide by 5, if not, divide by 10

Here's what I want to run, if the reference cell value exists in the range.

If .Cells(r, ColAttr44) / ValueSheet.Range("H4") / .Cells(r, ColAttr16) > 1 Then
Sheet1.Range("K" & NewRow) = "Yes"
t.Range1("L" & NewRow) = Int(.Cells(r, ColAttr44) / ValueSheet.Range("H4") / .Cells(r, ColAttr16))
Else
Sheet1.Range("K" & NewRow) = "No"
Sheet1.Range1("L" & NewRow) = 0
End If

If not,

If .Cells(r, ColAttr44) / ValueSheet.Range("I4") / .Cells(r, ColAttr16) > 1 Then
Sheet1.Range("K" & NewRow) = "Yes"
t.Range1("L" & NewRow) = Int(.Cells(r, ColAttr44) / ValueSheet.Range("I4") / .Cells(r, ColAttr16))
Else
Sheet1.Range("K" & NewRow) = "No"
Sheet1.Range1("L" & NewRow) = 0
End If

This is part of a huge looping sub. I don't want to step out of it just to perform this one function.

Thanks in advance. I hope it's clear. It's just been kinda melting my brain a bit to lookup a value in a range.

Posted Jul 3, 2019, 5:18 PM UTC

13
HFTBProgrammer49•

If you're issue is that you're unclear on how to find a value in a range, maybe something like this will push you forward (note that I'm pulling A1:A10 out of thin air; I presume you can translate that to your actual range):

Dim cell As Range, Divisor As Long
Divisor = 10
For Each cell In Range("A1:A10")
    If ColAttr35 = cell.Value2 Then
        Divisor = 5
        Exit For 'no need to keep checking, right?
    End If
Next cell
1
SMGivenOP•

Hey, thanks for the reply! I got this figured out with something pretty similar. I'll mark as solved. Cheers.

1
HFTBProgrammer49•

Splendid! Sorry I didn't reply sooner, but we Americans have our holidays... 8-)

1