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

#### Nite0wls

##### New Member
Hi All,

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

 81357641212 81357641212 81357641213 81357641000 81357641213 81357641212 AND 81357641000 81357641000 81357641000 81357641000 81357641213 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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

#### bschwartz

##### Well-known Member
Can you explain what the discrepancies are? What are you trying to change, and what should it become?

#### Nite0wls

##### New Member
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.

#### Nite0wls

##### New Member
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.

Replies
2
Views
162
Replies
2
Views
665
Replies
2
Views
435
Replies
4
Views
110
Replies
1
Views
735

1,191,529
Messages
5,987,116
Members
440,080
Latest member
drhorn4908

### 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.

### Which adblocker are you using?

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

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