Excel VBA

afeldsh

New Member
Joined
Apr 24, 2014
Messages
33
Hello, I am looking for some help with excel VBA. This forum has been very helpful to me in the past.

I have a bill of materials that represents structure with numbers. They can repeat anywhere on the structure. I would like select any one of those numbers in a cell and run VBA to tell me which row above this row is the next lower number. Any help is appreciated.

0
1
2
2
2
2
3
3
3
3
2
1
1
1
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Example file attached
 

Attachments

  • Screenshot 2023-09-15 113524.png
    Screenshot 2023-09-15 113524.png
    25.1 KB · Views: 9
Last edited:
Upvote 0
I don't understand your question (and perhaps others do not as well, which may be why you have not gotten any replies).
Can you walk us through an actual example, using the sample you posted in your last post?
 
Upvote 0
I don't understand your question (and perhaps others do not as well, which may be why you have not gotten any replies).
Can you walk us through an actual example, using the sample you posted in your last post?
Using the picture I provided above:

1) Example 1: Lets say I select cell A25 which has a value of 3 in that cell, I need a macro to tell me which row has the next lower number in column A above the selected row. In this case the response should be row 14 - which has the next lower value of 2 above this row.

2) Example 2: Lets say I select cell A44 which has a value of 1 in that cell, I need a macro to tell me which row has the next lower number in column A above the selected row. In this case the response should be row 2 - which has the next lower value of 0 above this row.

I want to select any cell in column A and have the macro do the same as in the examples above

Hope it makes more sense on what I am after. Thank you
 

Attachments

  • Screenshot 2023-09-15 113524.png
    Screenshot 2023-09-15 113524.png
    25.1 KB · Views: 6
Upvote 0
Try this:
VBA Code:
Sub FindLowerValue()

    Dim r As Long
    Dim ar As Long

'   Check to see that you are in column A
    If ActiveCell.Column <> 1 Then
        MsgBox "You have not selected a cell in column A!", vbOKOnly, "TRY AGAIN!"
        Exit Sub
    End If
    
'   Get row number of active cell
    ar = ActiveCell.Row
    
'   Loop through all rows backwards
    For r = ar To 2 Step -1
'       Check value
        If Cells(r, 1) < Cells(ar, 1) Then
            MsgBox "Row number " & r & " has lower value", vbOKOnly, "ROW NUMBER FOUND!"
            Exit Sub
        End If
    Next r

End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub FindLowerValue()

    Dim r As Long
    Dim ar As Long

'   Check to see that you are in column A
    If ActiveCell.Column <> 1 Then
        MsgBox "You have not selected a cell in column A!", vbOKOnly, "TRY AGAIN!"
        Exit Sub
    End If
   
'   Get row number of active cell
    ar = ActiveCell.Row
   
'   Loop through all rows backwards
    For r = ar To 2 Step -1
'       Check value
        If Cells(r, 1) < Cells(ar, 1) Then
            MsgBox "Row number " & r & " has lower value", vbOKOnly, "ROW NUMBER FOUND!"
            Exit Sub
        End If
    Next r

End Sub

Exactly what I was looking for. Thanks so much for your help! Have an awesome day!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,232
Members
449,092
Latest member
SCleaveland

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