简体   繁体   English

根据影响整个表的一列对数据进行排序 VBA

[英]Sort Data based on one column affecting the entire table VBA

I am trying to sort a table where I am sorting based on the data entries in Column E. However, when I am using my code, (below) my rows for which the column become disorganized and mismatched.我正在尝试根据列 E 中的数据条目对我正在排序的表进行排序。但是,当我使用我的代码时,(下面)我的列变得杂乱无章和不匹配的行。 Each row needs to contain the same data entries as before they were sorted, only moving to be sorted based on the values in column E. I am trying to sort this table, with my expected outcome shown below:每行都需要包含与排序前相同的数据条目,只是根据 E 列中的值进行排序。我正在尝试对这个表进行排序,我的预期结果如下所示:

Code:代码:

    With Worksheets("Fruits")
    Dim FruitsLastRow As Long
    Dim FruitsKeyRange As Range
        
        FruitsLastRow = .Range("E" & .Rows.Count).End(xlUp).Row
        Set DashboardRange = Range("E17:G" & FruitsLastRow)

        
        DashboardRange.Sort key1:=Range("E17:E" & FruitsLastRow), _
        order1:=xlAscending, Header:=xlNo _           

    End With

This is what I have这就是我所拥有的在此处输入图像描述

This is what I need这就是我需要的在此处输入图像描述

You need to specify just the top cell instead of all cells for key1 parameter ( Range.Sort example ).您只需为key1参数( Range.Sort 示例)指定顶部单元格而不是所有单元格。

DashboardRange.Sort key1:=Range("E17"), order1:=xlAscending, Header:=xlNo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM