Reference another cell value within if/then statement

rjh3m8

New Member
Joined
Jan 27, 2022
Messages
20
Office Version
  1. 365
  2. 2021
Platform
  1. MacOS
Hi all,
I am trying to write a code that looks at a specific range, finds the maximum value in the range, then pops up a messagebox containing the value two cells ABOVE the cell with that max value.

So far, I have the max value calculating in a single cell on the sheet (in cell V12) and have a variable that looks to match it. I am struggling with the "if it matches, look up two cells and report that cell's value" part. I tried using "ActiveCell.ValueR1C1 = "=R[-2]C[0]", but that didn't work out.
Any ideas?

'Determine Freq Bandwidth
Dim rng As Range
Dim cell As Range
Dim loc
Dim num

Set rng = Range("C12:S12")

For Each cell In rng
On Error GoTo err_chk
loc = cell.Value
On Error GoTo 0

If loc = Range("V12").Value Then 'Range V12 is MaxValue of rng
num = ????
'ActiveCell.ValueR1C1 = "=R[-2]C[0]"
MsgBox num & " found in cell " & cell.Address(0, 0)
End If
Next cell



Additionally, I'm looking at the same range to find what cells in the range are greater than a given value. I was able to make a conditional formatting rule on the sheet itself, but need to know the value of the cell two above the first cell that meets the criteria and the last cell that meets the criteria in the specific range. Any help on this would be nice too.
err_chk:
loc = 0
Err.Clear
Resume Next
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
try this

VBA Code:
Sub test()
'Determine Freq Bandwidth
Dim rng As Range
Dim cell As Range
Dim loc
Dim num
Dim rowno As Integer
Dim colno As Integer
Set rng = Range("C12:S12")

For Each cell In rng
loc = cell.Value

If loc = Range("V12").Value Then 'Range V12 is MaxValue of rng
rowno = cell.Row
colno = cell.Column
num = Range(Cells(rowno - 2, colno), Cells(rowno - 2, colno))
'ActiveCell.ValueR1C1 = "=R[-2]C[0]"
MsgBox num & " found in cell " & Range(Cells(rowno - 2, colno), Cells(rowno - 2, colno)).Address
Exit For
End If
Next cell
End Sub
 
Upvote 0
Solution
try this

VBA Code:
Sub test()
'Determine Freq Bandwidth
Dim rng As Range
Dim cell As Range
Dim loc
Dim num
Dim rowno As Integer
Dim colno As Integer
Set rng = Range("C12:S12")

For Each cell In rng
loc = cell.Value

If loc = Range("V12").Value Then 'Range V12 is MaxValue of rng
rowno = cell.Row
colno = cell.Column
num = Range(Cells(rowno - 2, colno), Cells(rowno - 2, colno))
'ActiveCell.ValueR1C1 = "=R[-2]C[0]"
MsgBox num & " found in cell " & Range(Cells(rowno - 2, colno), Cells(rowno - 2, colno)).Address
Exit For
End If
Next cell
End Sub
This worked great. Thanks so much!!
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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