Re Formula linking sheets

gelder

New Member
Joined
Jan 20, 2005
Messages
8
This may be an easily solved question, but here goes.

I have my main list in sheet 1, with a sub-list on sheet2. I would like to have sheet 2 change automatically as I rearrange sheet 1.

Ex. Original is Sheet 1, Cell A5. Sheet 2, A1 is linked to this.
When I alphabetize Sheet 1, A5 moves to A7.
Sheet 2 A1 is still linked to Sheet 1 A5. I need it to be now linked to Sheet 1 A7.

This is the formula I have used: =Sheet1!A5. I would like it to automaticall change to: =Sheet1!A7 (as necessary). But this does not change as I change sheet 1. Please help! :rolleyes:
 
I did think about trying to get everything done in memory, but the problem I think, is that if one does the changes in an array variable then applies them back to the workbook, the linked cells will still point to the same addresses. I couldn't think of a way to address the linked cells problem without manipulating the rows one by one, which is very slow for large ranges.

I do think the other workarounds already posted look good, and if people really need to "track" linked cells around a workbook maybe they should just use those ideas. Still, it'd be nice to find a solution that works directly on the data you're working with without needing extra formulas or labels.

I wonder if there is a way to identify dependent cells using formulas or VBA...that might open up another approach.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Keith,

After testing your code: congratulations: good start, since it works!
As an exercise: how can we reduce runtime?
I was thinking of a solution, which would first calculate all movements and then execute them, but this post isn't going so far... To give you some ideas: here are some enhancements. I think it's working OK.
The main purpose is to avoid loops.

At the end you add names and then you delete them: this is timeconsuming as is the one-by-one cut-and-insert, the major runtoime problem. I feel at least all those names can be avoided. I feel with the method like you'll find in the code "OriginalRows = rng", storing values in a "computed array" without using Excel itself, we can get rid of the names and paste "something" in an extra column at once...
As you said, we are no "profs", but if you like this, we could continue this way ... :)

kind regards,
Erik

this was deleted
For r = 2 To rCount
Cells(r, cCount + 1).Value = r
Next r

the same result without loop
Dim rng As Range
Set rng = Range(Cells(2, cCount + 1), Cells(rCount, cCount + 1))
With rng
.Formula = "=Row()"
.Value = .Value
End With


another deleted loop
For r = 2 To rCount
OriginalRows(r) = Cells(r, cCount + 1).Value
Next r

replaced by a oneliner
OriginalRows = rng 'using "computed array"

therefore = Dim OriginalRows As Variant
and : wbSort.Names.Add Name:="temprow" & r, _
RefersTo:=Range(Cells(rngSort.Row + OriginalRows(r - 1, 1) - 1, rngSort.Column), _
Cells(rngSort.Row + OriginalRows(r - 1, 1) - 1, rngSort.Column + cCount - 1))
don't ask me exactly why but it works


Code:
Option Base 1

Sub SortEntireCells()
'This macro takes a selected range and "sorts" it by
'cutting and inserting rows, so that links to the actual
'cells will "follow" the cells after they are sorted.
'
'In order to use this macro, select a data range and run this.
'The selected data range should contain headers/field names.
'The sort "key" will be the header in the column of the active cell.
'The range to be sorted can be one or several columns.
    
    Application.ScreenUpdating = False
    
    Dim rngSort As Range
    Dim r As Integer
    Dim rCount As Integer, cCount As Integer
    Dim keyOffset As Integer
    Dim OriginalRows As Variant
    Dim wbSort As Workbook
    
    Set wbSort = ActiveWorkbook
    Set rngSort = Selection
    keyOffset = ActiveCell.Column - Selection.Column
    
    rCount = rngSort.Rows.Count
    cCount = rngSort.Columns.Count
    
    'We this array to store row numbers
    'ReDim OriginalRows(rCount) As Variant
        
    'Copy the range to be sorted into another workbook
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    
    'Storing the original position of each row in the range
    With Cells(1, cCount + 1)
        .Value = "Original Row"
        .Font.Bold = True
        .Font.Italic = True
    End With
    
    Dim rng As Range
    Set rng = Range(Cells(2, cCount + 1), Cells(rCount, cCount + 1))
    With rng
    .Formula = "=Row()"
    .Value = .Value
    End With
    
    
    'Select entire copied range and the added row numbers, then sort
    ActiveSheet.UsedRange.Select
    Selection.Sort Key1:=Cells(1, 1).Offset(0, keyOffset), Header:=xlYes
    
    OriginalRows = rng 'using "computed array"


    'Close the temporary workbook
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
 
    'Back to the original workbook
    wbSort.Activate
    
    'Creating a temporary name for each row in original range,
    'the "number" of each name corresponds to the position in the
    'sorted range
    
    For r = 2 To rCount
        'see the little changes OriginalRows(r - 1, 1)
        wbSort.Names.Add Name:="temprow" & r, _
            RefersTo:=Range(Cells(rngSort.Row + OriginalRows(r - 1, 1) - 1, rngSort.Column), _
            Cells(rngSort.Row + OriginalRows(r - 1, 1) - 1, rngSort.Column + cCount - 1))
        
    Next r
            
    'Cut and insert rows to their new order.
    For r = 2 To rCount
        If Range("temprow" & r).Row <> rngSort.Cells(r, 1).Row Then 'Skip row if it's already in the right place
            Range("temprow" & r).Cut
            rngSort.Cells(r, 1).Insert Shift:=xlDown
        End If
    Next r
    
    'Delete the temporary row names
    For Each nm In wbSort.Names
        If Left(nm.Name, 7) = "temprow" Then nm.Delete
    Next nm
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Formula linking sheets

15 SECONDS for 5000 ROWS
sorting cells by cutting and pasting
(executed on a new file)
computer is 3 years old
or is this just a dream ?

using the computed array this code places the items at the right of the originals from the "lowest to highest"
no inserts will slow down the code
(no named ranges to add)

then everything is cut back in place
so there is a restriction of 128 columns

use this code to fill a range
Sub fill_with_random_values()
Columns(1) = ""
Range("A1") = "HEADER"
rCount = 5000
With Range(Cells(2, 1), Cells(rCount, 1))
.FormulaR1C1 = "=RAND()"
.Copy
.PasteSpecial Paste:=xlPasteValues
End With
Range(Cells(1, 1), Cells(rCount, 1)).Select
Application.CutCopyMode = False
End Sub

without changing selection, run the code below

kind regards,
Erik

Rich (BB code):
'an idea of "scifibum" at http://www.mrexcel.com/board2/viewtopic.php?t=126257
'developed by Erik Van Geit
Option Explicit
Option Base 1

Sub SortEntireCells()
'This macro takes a selected range and "sorts" it by
'cutting and inserting rows, so that links to the actual
'cells will "follow" the cells after they are sorted.
'
'In order to use this macro, select a data range and run this.
'The selected data range should contain headers/field names.
'The sort "key" will be the header in the column of the active cell.
'The range to be sorted can be one or several columns.

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Dim rngSort As Range
    Dim i As Long
    Dim rr As Long
    Dim ori As Long
    Dim rcount As Long
    Dim cCount As Integer
    Dim keyOffset As Integer
    Dim rc As Integer
    Dim rcc As Integer
    Dim OriginalRows As Variant
    Dim wbSort As Workbook
    
    Set wbSort = ActiveWorkbook
    Set rngSort = Selection
    keyOffset = ActiveCell.Column - Selection.Column
    
    rcount = rngSort.Rows.Count
    cCount = rngSort.Columns.Count
    
    'array to store row numbers
    ReDim OriginalRows(rcount) As Variant
     
    'Copy the range to be sorted into another workbook
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    
    'Storing the original position of each row in the range
    With Cells(1, cCount + 1)
        .Value = "Original Row"
        .Font.Bold = True
        .Font.Italic = True
    End With
    
    Dim rng As Range
    Set rng = Range(Cells(2, cCount + 1), Cells(rcount, cCount + 1))
    With rng
    .Formula = "=Row()"
    .Value = .Value
    End With
    
    
    'Select entire copied range and the added row numbers, then sort
    ActiveSheet.UsedRange.Select
    Selection.Sort Key1:=Cells(1, 1).Offset(0, keyOffset), Header:=xlYes
    
    OriginalRows = rng 'using "computed array"

    'Close the temporary workbook
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
 
    'Back to the original workbook
    wbSort.Activate
    
    rr = rngSort.Row
    rc = rngSort.Column
    rcc = rngSort.Column + cCount - 1
Application.DisplayStatusBar = True

    For i = LBound(OriginalRows, 1) To UBound(OriginalRows, 1)
        ori = OriginalRows(i, 1)
        Range(Cells(ori, rc), Cells(ori, rcc)).Cut Range(Cells(i + rr, rcc + 1), Cells(i + rr, rcc * 2))
        Application.StatusBar = "sorting: " & Round(i / rcount * 100, 0) & "%"
    Next
    Range(Cells(rr + 1, rcc + 1), Cells(rr + rcount - 1, rcc * 2)).Cut Range(Cells(rr + 1, rc), Cells(rr + rcount - 1, rr + cCount - 1))
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False
End Sub
 
Upvote 0
OK, this is progress I think. Much faster than the one I made, nice work!

However,

THis is not working for me when I try some other data (other than your random list which works fine). I have to spend some more time looking at the changes you made to understand the problem.

Try it on a list such as the following if you want to see if it also goes wrong for you:
sort2.xls
BCDEFGHIJ
2CategoryValue1Value2
3A217181
4B116186
5C399405
6D3887
7E125401
8F193415
9G47157
10H390172
11I487467
12J353209
13K260305
14L441405
15M312142
16N485400
17O29031
18P12426
19Q180431
20R344297
21S257434
22T25268
23U7114
24V158286
25W373113
26X287301
27Y19170
28Z118270
Sheet1
 
Upvote 0
Everything OK for me,

What's the problem fo you?
When cell "Category" is active starting the macro, everything stays the same, since A ==> Z is already in the right order. When "Value1" or "Value2" is active it sorts on 2nd or 3d column, moving the cells 3 by 3 to another location, which seems logic to me when you sort.
Did you want as result every column sorted by it's own, not keeping the "neighbours" together?

kind regards,
Erik
 
Upvote 0
Thank you guys for all your help! Unfortnunately, I have no idea what all the code means, or how to translate it! Any way you could "dumb" that down for a lowly excel beginner? :oops:
 
Upvote 0
Hi, Gelder,
glad to hear about you!
Since you weren't coming back we were just exploring the possibilities to enhance code.
BUT perhaps you won't need code at all!
So please give us an example of your situation.

Do you have a lot of cells linked to cells that will be sorted?
How are your pages set up?

to paste code in your workbook and to run it, etcetera
we can't offer here courses on how to do that

just a brief example to start

(Open a new workbook in Microsoft Excel, and )
start the Visual Basic Editor (via Menu Tools, Macro or press ALT+F11).
On the Insert menu in the VBE, click Module. (if necessary)
In the module (the white area at the right), paste the following code:
Code:
Sub a()
Range("A1").Select
ActiveCell = "this is the first cell of the sheet"
MsgBox Range("A1"), 64, "Contents of cell " & ActiveCell.Address
End Sub
How to learn with example-codes...
1. pass through it with the function key F8
2. click on an item and hit F1 to let popup the help
3. use the macro recorder to see how code is generated (this code will need some "cleaning" afterwards)

BUT as said, if you can do it without code ...
So give us more info ...


kind regards,
Erik
 
Upvote 0
Hey Erik, Keith,
I was reading the posts, trying to puzzle them out, but not replying. Thanks for all your help guys!

Here's the deal:

Sheet 1 is my main customer list (current clients + prospective customers), each cell in a row containing information pertinant to that business.
ex. Sheet 1, Row 1 = BusinessA, address, phone #, etc.
Sheet 1, Row 2 = BusinessB, address, phone #, etc.
Sheet 2 is only my current client list, therefore it contains only some of the businesses on sheet 1. I would like to be able to type in Sheet 1 only, and have it automatically update Sheet 2 (as necessary).

The problem is that I am constantly adding new businesses to Sheet 1. When I sort Sheet 1 alphabetically, the codes do not follow the appropriate cell.

Sheet 1 contains ~500 rows.

Is this enough info?

Thanks again!

-Genoa :rolleyes:
 
Upvote 0
Genoa,

On Sheet 2, do you have columns repeated for each field on Sheet1? I.e. sheet 2 contains not only name, but address, phone, etc. or does sheet 2 contain only the businesses' names?

[I ask because I took a completely different approach to this, but I'm not sure how well my idea would pan out if there were many columns on sheet 2 and would want to test that aspect before posting more.]
 
Upvote 0
Greg,

Yes, same # of columns in sheet 1 and sheet 2. Basically its a duplicate (column-wise) of sheet 1.

The easiest way to do this for me is to have only 1 sheet of info and simply re-sort it depending on what info I want to go by (ie, company name, or zip code, etc.) But I would rather have many sheets. Makes it easier to simply glance at it.

Thanks,

Genoa
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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