简体   繁体   中英

Excel VBA - Checking a Sequence of Numbers and Replacing Next or Last Value if Necessary

I am having an extreme difficulty trying to solve this problem. I've tried using case statements and if/else statements with no luck. Here is my problem! I have a sequence of numbers in one column such as 42,43,49,50 where 42 is always followed with the value 43 and where 49 is always followed with a value of 50.

Sometimes I have a sequence of numbers where they are out of order such as 42,43,49,43. In this case I need to change the pattern to 42,43,49,50. In another case I have a sequence of numbers such as 42,50,49,50 where I need to change the pattern to 49,50,49,50.

Here is my logic: 1st case... If cell = 50 go back to last cell and if that cell = 43 change that cell value to a 49. 2nd case... If cell = 49 go to next available cell and if that cell = 43 change that value to a 50. Keep in mind there are going to be empty cells that have to be dealt with like in the following column.

A 42 "empty" 43 "empty" 49 "empty" "empty" "empty" 43

Let me know if you all need more clarification and thank you for looking and helping! I hope I followed the question format and sorry if I did not!

Thank you, thank you!!!

Something like this...

Sub Tester()

Dim c As Range, i As Long, vLast

    Set c = Cells(Rows.Count, 1).End(xlUp)

    Do While c.Row >= 2
        If Len(c.Value) > 0 Then
            If Len(vLast) > 0 Then
                If vLast = 50 Then c.Value = 49
                If vLast = 43 Then c.Value = 42
            End If
            vLast = c.Value
        End If
        Set c = c.Offset(-1, 0)
    Loop

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM