【问题标题】:Excel Sorting a Dynamic List or use VBA then sortExcel 对动态列表进行排序或使用 VBA 然后排序
【发布时间】:2015-02-12 20:33:10
【问题描述】:

我正在使用表 2 从表 1 中提取数据。

A9 里面有这个公式:

=(INDEX(sheet1!$G$9:$G$7000,MATCH(0,INDEX(COUNTIF($A$8:A8,sheet1!$G$9:$G$7000),0,0),0))

(它查看 G 列并取出重复项和空白)

B9 有这个公式:

=IF(MAX(IF($A9=sheet1!G:G,sheet1!E:E))=MIN(IF($A9=sheet1!G:G,sheet1!E:E)),"Only 1 Entry",MAX(IF($A9=sheet1!G:G,sheet1!E:E))-MIN(IF($A9=sheet1!G:G,sheet1!E:E)))

(这个在 sheet2 上的 A 列中查找,然后在 Sheet1 上查找日期、最小值和最大值以确定某个项目的年龄)

C9 有这个公式:

=SUMIF(sheet1!$G$9:$G$7000,A9,sheet1!$B$9:$B$7000)

(这看起来像工作表 2 中的 A 列,并引用工作表 1 来加起来)

问题是,如果我对 sheet2 上的 C 列进行排序,则没有任何变化。我认为是因为当它试图过滤它时,动态公式将它重新排序回它在工作表 1 上的样子。基本上无论你如何尝试过滤它,列表都保持不变,因为它基于 sheet1。我什至尝试对工作表 1 上的列进行排序,以查看工作表 2 是否会更改,但由于工作表 2 的 C 列中的数据实际上并不存在于工作表 1 上,因此也不起作用。

如何使用现有的动态公式过滤 C 列甚至 B 列和其他列?

我在网上搜索了一个解决方案,但找不到任何可行的方法。如果我不能使用这个动态列表,我想也许我可以用 VBA 在 A 表 2 中创建列表并使列表静态。

我也搜索了一个 VBA 来删除重复和空白,但由于某种原因,我想出了一个空白。我找到了一些,但不是两者兼而有之。

Sub MakeUnique()

    Dim vaData As Variant
    Dim colUnique As Collection
    Dim aOutput() As Variant
    Dim i As Long

    'Put the data in an array
    vaData = Sheet1.Range("A5:A7000").Value

    'Create a new collection
    Set colUnique = New Collection

    'Loop through the data
    For i = LBound(vaData, 1) To UBound(vaData, 1)
        'Collections can't have duplicate keys, so try to
        'add each item to the collection ignoring errors.
        'Only unique items will be added
        On Error Resume Next
            colUnique.Add vaData(i, 1), CStr(vaData(i, 1))
        On Error GoTo 0
    Next i

    'size an array to write out to the sheet
    ReDim aOutput(1 To colUnique.Count, 1 To 1)

    'Loop through the collection and fill the output array
    For i = 1 To colUnique.Count
        aOutput(i, 1) = colUnique.Item(i)
    Next i

    'Write the unique values to column B
    Sheet2.Range("A9").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput

End Sub

此 VBA 创建一个没有重复但留下空白的列表...

那么,我怎样才能让工作表 2 上的 B 列和 C 列可以排序,而 A 列是从工作表 1 上的数据派生出来的,没有重复和空白?有没有办法对动态公式进行排序和使用,还是应该用 VBA 来完成?

【问题讨论】:

    标签: list sorting excel excel-formula vba


    【解决方案1】:

    您发布的此版本代码将在唯一列表中包含空白:

    Sub MakeUnique()
    
        Dim vaData As Variant
        Dim colUnique As Collection
        Dim aOutput() As Variant
        Dim i As Long
    
        'Put the data in an array
        vaData = Sheet1.Range("A5:A7000").Value
    
        'Create a new collection
        Set colUnique = New Collection
    
        'Loop through the data
        For i = LBound(vaData, 1) To UBound(vaData, 1)
            'Collections can't have duplicate keys, so try to
            'add each item to the collection ignoring errors.
            'Only unique items will be added
            If vaData(i, 1) <> "" Then
                On Error Resume Next
                    colUnique.Add vaData(i, 1), CStr(vaData(i, 1))
                On Error GoTo 0
            End If
        Next i
    
        'size an array to write out to the sheet
        ReDim aOutput(1 To colUnique.Count, 1 To 1)
    
        'Loop through the collection and fill the output array
        For i = 1 To colUnique.Count
            aOutput(i, 1) = colUnique.Item(i)
        Next i
    
        'Write the unique values to column B
        Sheet2.Range("A9").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput
    
    End Sub
    

    【讨论】:

    • 这部分效果很好,谢谢。现在我只需要弄清楚排序部分:) 似乎我在 C 列中的方程式在我排序时会在顶部留下一堆“空白”......
    • 你是按宏排序的吗??
    • 为了清理 C 列中的方程,我添加了这个: =IF(A9="","",SUMIF(Sheet1!$G$9:$G$7000,A9,Sheet1!$B$9: $B$7000)) 在第 2 页上,它从 C9 变为 C500。如果我手动从 A 到 Z 排序它可以正常工作,但是如果我将 Z 到 A 排序,结果会在底部留下空白在顶部。我正在尝试创建一个仅包含可见数字的动态范围,但尚未使其正常工作。然后我也想将其转换为 VBA。
    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2019-12-10
    • 2012-06-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多