Determine if Cell Value is +1 or -1 Value to any Cell Values in Row Above

Poker Joe

New Member
Joined
Feb 12, 2015
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
I'm hoping to get some advice on what functions are best to use or a subroutine to determine if a cell value is one value more (+1) or less (-1) any of the cell values in the row above it.

If true, I simply want to identify it by placing the letter "A" in a cell, else, leave blank. The "A" or blank cells would be in the same row but adjacent to the cells that the subroutine is running to determine whether it is true.

Below is an example of the end result I desire. Rows would be added hourly but I have a backlog of data I need to examine so I was hoping to find a solution with an autofill function or subroutine.



ABCDEFGHIJKL
1TestG1G2G3G4G5A1A2A3A4A5
21258912
3215101113AAA
433781520
541591213A
6548101517AAA
761591316AAA
871822232526
98712131620
109813141721AAAAA
111016101123
1211512212223AAA
13122791524A

<tbody>
</tbody>
Template (3)


<tbody>
</tbody>
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I think this is what you want, based on your explanation. Some of my results are different to yours though, e.g G4 in row 2 returns "A"...
=IF(COUNTIF($B2:$F2,B3-1)+COUNTIF($B2:$F2,B3+1),"A","")
 
Upvote 0
I think this is what you want, based on your explanation. Some of my results are different to yours though, e.g G4 in row 2 returns "A"...
=IF(COUNTIF($B2:$F2,B3-1)+COUNTIF($B2:$F2,B3+1),"A","")


Thank You! Your results are correct, my example was incorrect.
I just need to figure out how to put that formula into a subroutine.
 
Upvote 0
That's sounds great. I just can't get the lightbulb to turn on that allows me to see how to do that.
 
Upvote 0
Suggest something like this...

Code:
Sub Test()

Dim LastRow As Long
Dim c As Range, d As Range
Dim TestValue As Long
Application.ScreenUpdating = False
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For Each c In Range(Cells(3, 2), Cells(LastRow, 6))
    TestValue = c.Value
    For Each d In Range(Cells(c.Row - 1, 2), Cells(c.Row - 1, 6))
        If d = TestValue + 1 Or d = TestValue - 1 Then
            c.Offset(, 6) = "A"
            Exit For
        End If
    Next d
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
THANK YOU Neil! This helps me wrap my mind around how things need to work in VBA. For some reason I have a difficult time connecting the dots but when I have a specific routine I need and see how it needs to be written my mind absorbs it better than someone giving me a generic routine. Simple ones, no problem but the more complex they become, the faster I find myself lost in the weeds.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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