Maintain cell value of referenced cell after sorting

gaudrco

Board Regular
Joined
Aug 16, 2019
Messages
203
I have a list of values in range B6:B52; each value represents a report. In column D, I have the name of the reports going down the column referencing column B such that:

D6 = B6
D57 = B7
D108 = B8
D159 = B9
.. and so on

As I add values to column B, the value automatically gets referred to the next cell of the reports in column D. For example, if a type "Apple" in cell B10, then in cell D210 the value will be "Apple".

But if I sort the list of values in column B alphabetically, then all of the values in column D change as well. I need the values in column D to remain where they are after sorting. Somehow, I need to link the cell in column D to the exact cell in column B so that when I sort the list, the values (report names) in column D dont change.

Is there a way to do this?
 
There might be an easier way to write that.
For sure.
Code:
If Not Intersect(Target, Range("ReportsLOV")) Is Nothing Then
    'do what needs to be done
End If
BUT
Anything added may be dictated by anything else that's already in the Worksheet_Change macro.
Can you post the already existing macro ?
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Private Sub Worksheet_Change(ByVal Target As Range)    Dim strAddress As String
    Dim val
    Dim dtmTime As Date
    Dim Rw As Long
    
    
    If Intersect(Target, Range("CCDSheet")) Is Nothing Then Exit Sub
    
    dtmTime = Now()
    val = Target.Value
    strAddress = Target.Address
    
    Rw = Sheets("Information").Range("B" & Rows.Count).End(xlUp).Row + 1
    With Sheets("Information")
        .Cells(Rw, 2) = "Competitor Comparison Data"
        .Cells(Rw, 3) = strAddress
        .Cells(Rw, 4) = val
        .Cells(Rw, 5) = dtmTime
        .Cells(Rw, 6) = Application.UserName
    End With
    
    '***Every below is what I'm trying to work out in this thread. Incomplete
    With ActiveSheet.ListObjects("ReportsLOV")
If Target.Address = ActiveSheet.Cells(.ListRows.Count + 6, .ListColumns.Count + 1).Select Then
 
     Range("D58").Value = Range(Cells(.ListRows.Count + 5, .ListColumns.Count + 1)).Value
 
End If
End With
End Sub
 
Upvote 0
Assuming the existing code is fine, try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim strAddress As String
    Dim val
    Dim dtmTime As Date
    Dim rw As Long, x As Long

'*********** existing code *********************************************
'
If Intersect(Target, Range("CCDSheet")) Is Nothing Then Exit Sub
    dtmTime = Now()
    val = Target.Value
    strAddress = Target.Address
    rw = Sheets("Information").Range("B" & Rows.Count).End(xlUp).Row + 1
        With Sheets("Information")
            .Cells(rw, 2) = "Competitor Comparison Data"
            .Cells(rw, 3) = strAddress
            .Cells(rw, 4) = val
            .Cells(rw, 5) = dtmTime
            .Cells(rw, 6) = Application.UserName
        End With

'********** code to add report name ************************************
'
'check if the cell is in the B6 and down range
If Not Intersect(Target, Range("ReportsLOV")) Is Nothing Then
'restrict to a single cell only
    If Target.CountLarge = 1 Then
        'check for not blank
        If Target.Value <> "" Then
            'check if already exists
            If Application.CountIf(Range("D:D"), Target.Value) = 0 Then
                'if not existing, put in report name
                For x = 6 To 10000 Step 51
                    If Cells(x, "D").Value = "" Then
                        Cells(x, "D").Value = Target.Value
                        Exit For
                    End If
                Next x
            End If
        End If
    End If
End If

End Sub
 
Upvote 0
I'm a bit late on the scene, but could you have done it without vba something like this?

- Have another column nearby your column B values (I've used column A but you may need to insert a new column somewhere) containing sequential numbers pre-populated down as far as you might want.
- Change your column D formulas as shown, Formula in D6 is just copied to D57, D108 etc
- When you want to sort the column B values, include the extra column in the sort.

If any of the column B values are changed, the new values should automatically show up in the corresponding place in column D.

Before the sort:

Excel Workbook
ABCD
61data 1data 1
72data 5
83data 8
94data 6
105data 7
116data 9
127data 3
138data 4
149data 2
1510
1611
17
57data 5
58
107
108data 8
109
157
158
159data 6
Maintain Reference




After the sort of A6:B16, based on column B:

Excel Workbook
ABCD
61data 1data 1
79data 2
87data 3
98data 4
102data 5
114data 6
125data 7
133data 8
146data 9
1510
1611
17
57data 5
58
107
108data 8
109
157
158
159data 6
Maintain Reference



Note that if lots of new rows are added at the top, pushing what is now row 6 down to row 51 or below the formulas would return incorrect results. If that is a possibility, it may be possible to deal with.
 
Upvote 0
Thank you Peter, this is a really great idea and I really like how it allows column B values to be changed and adjusted in column D. I will look into this method if I have any issues with the other solution provided by NoSparks.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

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