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

后端 未结 22 2040
逝去的感伤
逝去的感伤 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
    

提交回复
热议问题