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

[TABLE="width: 500"]
<tbody>[TR]
[TD="align: center"]81357641212[/TD]
[TD][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]81357641212[/TD]
[TD="align: center"][/TD]
[TD="align: center"]81357641213[/TD]
[/TR]
[TR]
[TD="align: center"]81357641000[/TD]
[TD="align: center"][/TD]
[TD="align: center"]81357641213[/TD]
[/TR]
[TR]
[TD="align: center"]81357641212[/TD]
[TD="align: center"]AND[/TD]
[TD="align: center"]81357641000[/TD]
[/TR]
[TR]
[TD="align: center"]81357641000[/TD]
[TD="align: center"][/TD]
[TD="align: center"]81357641000[/TD]
[/TR]
[TR]
[TD="align: center"]81357641000[/TD]
[TD="align: center"][/TD]
[TD="align: center"]81357641213[/TD]
[/TR]
[TR]
[TD="align: center"]81357641212[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]81357641212[/TD]
[TD="align: center"][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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,223,566
Messages
6,173,095
Members
452,501
Latest member
musallam

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