how to replace a cel value based on adjecent cell values of previous and next rows?

Nite0wls

New Member
Joined
May 21, 2014
Messages
33
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi All,

I have a table where for whatever reason there is some discrepancy in the data

81357641212
8135764121281357641213
8135764100081357641213
81357641212AND81357641000
8135764100081357641000
8135764100081357641213
81357641212
81357641212

<tbody>
</tbody>

I am trying to automate the replacement of the discrepancies based on the values of the previous and next adjacent cells using a macro.

I have a formula that does this =IF(A6282=81357641000,IF(A6281<>81357641000,A6281,A6283)) but I need to manipulate much more then this alone and most of it is done by a Macro

So I am looking for something similar to this:

Code:
Sub Modify()
    Dim Cell As Range
    Set TelRange = ActiveWorkbook.ActiveSheet.Columns("A").SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues)
    For Each Cell In TelRange
        With Cell
            If .Value = 81357641000# Then
                If  [previous cell] .Value <> 81357641000 Then
                  .Value = [previous cell value]
                Else .Value = [next cell value]<next cell="">
            End If
        End With
    Next Cell
End Sub
</next>

Any suggestions appreciated!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Can you explain what the discrepancies are? What are you trying to change, and what should it become?
 
Upvote 0
The discrepancies are the red values and when they are between the black values they need to be replaced with the black value (that's what the formula does)

However when I wrote the post I investigated a bit more and came up with this:
Code:
    Dim Cell As Range
    Set TelRange = ActiveWorkbook.ActiveSheet.Columns("A").SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues)
    For Each Cell In TelRange
        With Cell
            If .Value = 81357641000# Then
                If ActiveCell.Offset(-1, 0).Value <> 81357641000# Then
                   .Value = ActiveCell.Offset(-1, 0).Value
                ElseIf ActiveCell.Offset(1, 0).Value <> 81357641000# Then
                   .Value = ActiveCell.Offset(1, 0).Value
                End If
            End If
        End With
    Next Cell
End Sub

unfortunately that replaces the red marked values with the description in the first row (being DNIS) what is not quite what I expected to happen, maybe one can explain why that happens.
 
Upvote 0
Hi all,

I found the problem after more digging around.

The issue was with the way I used the
Code:
ActiveCell.Offset(-1, 0)
as that is relative to the selected cell (the one the cursor is in when the macro is started) and it needed to be to the cell examined in the macro, so I have replaced it with
Code:
Cell.Offset(-1, 0)
and now I get the expected result.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,013
Members
448,935
Latest member
ijat

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