How do I sort a table in Excel if it has cell references in it?

后端 未结 22 1857
逝去的感伤
逝去的感伤 2020-12-05 02:42

I have a table of data in excel in sheet 1 which references various different cells in many other sheets. When I try to sort or filter the sheet, the references change when

相关标签:
22条回答
  • 2020-12-05 03:17

    We are also struggling with the same issue.

    As a workaround, we use a macro to covert table to list, sort the list and then covert the list back to table.

    here please find a sample macro

        Sub Sort_Table()
    
        'change table name to "table1"
        ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$c$4"), , xlYes).Name = _
        "Table1"
        ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
    
    
        Dim oSh As Worksheet
        Set oSh = ActiveSheet
      'remove table or list style
       oSh.ListObjects("Table1").Unlist
    
     'Sort List
      Range("B2").Select
      ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
      ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A4"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:C4")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
     'Change list back to table again
      ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$c$4"), , xlYes).Name = _
        "Table1"
      ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
      End Sub
    
    0 讨论(0)
  • 2020-12-05 03:20

    You have a couple options:

    (1) There's no way around cell references getting messed up when sorting when not using static references. The most basic way to deal with this is to simply copy and paste as values before sorting, which could be automated via a simple VBA macro.

    (2) You could also try utilizing named ranges if you're using a number of common ranges across your formulas. You could define 'Sheet2!B23:28' as 'Range1' and reference 'Range1' within your formulas. In that case, sorting obviously wouldn't affect the range being specified since it's defined elsewhere.

    HTH

    0 讨论(0)
  • 2020-12-05 03:21

    Best way is to keep your reference data on one side of the sheet and all the formulas on the other side of the sheet. then leave a blank column between them (hide it if you want) an then you'll be sorting only the reference data keeping the Formula references pointing always at the same place. Downside is Excel will recalc everytime you sort.

    0 讨论(0)
  • 2020-12-05 03:22

    Put a dollar sign in front of the row and/or column of the cell you want to remain constant.

    Fixed it for me!

    0 讨论(0)
提交回复
热议问题