Excel 中的排序和过滤

发布于 2025-01-13 06:00:21 字数 781 浏览 3 评论 0原文

A列 B列 C列 D
John22David87
Marcy42Kumar23
Kumar35Marcy42
David21John33

在 Excel 根据 A 列对 C 列进行排序时,C 列和 D 列的数据将一起移动。

例如:第一行将是

Column AColumn BColumn CColumn D
John22John33

我尝试过 Excel 函数,例如:

PROCVVLOOKUP 以及排序和过滤按钮优秀但没有运气。

Column AColumn BColumn CColumn D
John22David87
Marcy42Kumar23
Kumar35Marcy42
David21John33

In excel ordering Column C according Column A, the data of column C e D are to move together.

Ex: First row will be

Column AColumn BColumn CColumn D
John22John33

I've tried excel functions like:

PROCV, VLOOKUP and the sort and filter button in excel with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2025-01-20 06:00:21

注意:

  1. 所有行必须由 a 列中的名称唯一标识
  2. 您需要引用 Microsoft Scripting Runtime

... 无论如何

Sub sheesh()

    Set dict = CreateObject("Scripting.Dictionary")
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(1)
    Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
    Dim i As Integer
    For i = 1 To lastRow
        dict.Add CStr(ws.Cells(i, 3).Value), CInt(ws.Cells(i, 4).Value)
    Next i
    
    For Each k In dict.Keys
        Debug.Print k
    Next
    
    For i = 1 To lastRow
        If dict.Exists(ws.Cells(i, 1).Value) Then
            ws.Cells(i, 3).Value = ws.Cells(i, 1).Value
            ws.Cells(i, 4).Value = dict(ws.Cells(i, 1).Value)
        End If
    Next i
    
End Sub

Note:

  1. All rows must be uniquely identified by the name in column a
  2. You need to reference Microsoft Scripting Runtime

... anyway

Sub sheesh()

    Set dict = CreateObject("Scripting.Dictionary")
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(1)
    Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
    Dim i As Integer
    For i = 1 To lastRow
        dict.Add CStr(ws.Cells(i, 3).Value), CInt(ws.Cells(i, 4).Value)
    Next i
    
    For Each k In dict.Keys
        Debug.Print k
    Next
    
    For i = 1 To lastRow
        If dict.Exists(ws.Cells(i, 1).Value) Then
            ws.Cells(i, 3).Value = ws.Cells(i, 1).Value
            ws.Cells(i, 4).Value = dict(ws.Cells(i, 1).Value)
        End If
    Next i
    
End Sub
辞慾 2025-01-20 06:00:21

对齐数据

在此输入图像描述

Option Explicit

Sub AlignData()
    
    Const fRow As Long = 1
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    Dim Key As Variant
    Dim r As Long
    
    ' Left two columns to left dictionary.
    
    Dim llRow As Long: llRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim lCount As Long: lCount = llRow - fRow + 1
    Dim lrg As Range: Set lrg = ws.Cells(fRow, "A").Resize(lCount, 2)
    Dim lData As Variant: lData = lrg.Value
    Dim lDict As Object: Set lDict = CreateObject("Scripting.Dictionary")
    lDict.CompareMode = vbTextCompare
    
    For r = 1 To lCount
        Key = lData(r, 1)
        If Not IsError(Key) Then ' exclude error values
            If Len(Key) > 0 Then ' exclude blanks
                lDict(lData(r, 1)) = lDict(lData(r, 1)) + lData(r, 2) ' Sum
            End If
        End If
    Next r
    Erase lData
    lCount = lDict.Count
    
    ' Right two columns to right dictionary.
    
    Dim rlRow As Long: rlRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    Dim rCount As Long: rCount = rlRow - fRow + 1
    Dim rrg As Range: Set rrg = ws.Cells(fRow, "C").Resize(rCount, 2)
    Dim rData As Variant: rData = rrg.Value
    Dim rDict As Object: Set rDict = CreateObject("Scripting.Dictionary")
    rDict.CompareMode = vbTextCompare
    
    For r = 1 To rCount
        Key = rData(r, 1)
        If Not IsError(Key) Then ' exclude error values
            If Len(Key) > 0 Then ' exclude blanks
                rDict(rData(r, 1)) = rDict(rData(r, 1)) + rData(r, 2) ' Sum
            End If
        End If
    Next r
    Erase rData
    rCount = rDict.Count
    
    ' Write to destination arrays.
    
    ReDim lData(1 To lCount, 1 To 2) ' exact size
    ReDim rData(1 To lCount + rCount, 1 To 2) ' oversized
    r = 0
    
    For Each Key In lDict.Keys
        r = r + 1
        lData(r, 1) = Key: lData(r, 2) = lDict(Key)
        If rDict.Exists(Key) Then
            rData(r, 1) = Key: rData(r, 2) = rDict(Key)
            rDict.Remove Key
        End If
    Next Key
        
    If rDict.Count > 0 Then
        For Each Key In rDict.Keys
            r = r + 1
            rData(r, 1) = Key: rData(r, 2) = rDict(Key)
        Next Key
    End If
    
    ' Overwrite ranges.
    
    With lrg
        .Resize(lDict.Count).Value = lData
        .Resize(ws.Rows.Count - .Row - lCount + 1).Offset(lCount).ClearContents
    End With
    With rrg
        .Resize(r).Value = rData
        .Resize(ws.Rows.Count - .Row - r + 1).Offset(r).ClearContents
    End With
    
    MsgBox "Data aligned.", vbInformation
    
End Sub

Align Data

enter image description here

Option Explicit

Sub AlignData()
    
    Const fRow As Long = 1
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    Dim Key As Variant
    Dim r As Long
    
    ' Left two columns to left dictionary.
    
    Dim llRow As Long: llRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim lCount As Long: lCount = llRow - fRow + 1
    Dim lrg As Range: Set lrg = ws.Cells(fRow, "A").Resize(lCount, 2)
    Dim lData As Variant: lData = lrg.Value
    Dim lDict As Object: Set lDict = CreateObject("Scripting.Dictionary")
    lDict.CompareMode = vbTextCompare
    
    For r = 1 To lCount
        Key = lData(r, 1)
        If Not IsError(Key) Then ' exclude error values
            If Len(Key) > 0 Then ' exclude blanks
                lDict(lData(r, 1)) = lDict(lData(r, 1)) + lData(r, 2) ' Sum
            End If
        End If
    Next r
    Erase lData
    lCount = lDict.Count
    
    ' Right two columns to right dictionary.
    
    Dim rlRow As Long: rlRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    Dim rCount As Long: rCount = rlRow - fRow + 1
    Dim rrg As Range: Set rrg = ws.Cells(fRow, "C").Resize(rCount, 2)
    Dim rData As Variant: rData = rrg.Value
    Dim rDict As Object: Set rDict = CreateObject("Scripting.Dictionary")
    rDict.CompareMode = vbTextCompare
    
    For r = 1 To rCount
        Key = rData(r, 1)
        If Not IsError(Key) Then ' exclude error values
            If Len(Key) > 0 Then ' exclude blanks
                rDict(rData(r, 1)) = rDict(rData(r, 1)) + rData(r, 2) ' Sum
            End If
        End If
    Next r
    Erase rData
    rCount = rDict.Count
    
    ' Write to destination arrays.
    
    ReDim lData(1 To lCount, 1 To 2) ' exact size
    ReDim rData(1 To lCount + rCount, 1 To 2) ' oversized
    r = 0
    
    For Each Key In lDict.Keys
        r = r + 1
        lData(r, 1) = Key: lData(r, 2) = lDict(Key)
        If rDict.Exists(Key) Then
            rData(r, 1) = Key: rData(r, 2) = rDict(Key)
            rDict.Remove Key
        End If
    Next Key
        
    If rDict.Count > 0 Then
        For Each Key In rDict.Keys
            r = r + 1
            rData(r, 1) = Key: rData(r, 2) = rDict(Key)
        Next Key
    End If
    
    ' Overwrite ranges.
    
    With lrg
        .Resize(lDict.Count).Value = lData
        .Resize(ws.Rows.Count - .Row - lCount + 1).Offset(lCount).ClearContents
    End With
    With rrg
        .Resize(r).Value = rData
        .Resize(ws.Rows.Count - .Row - r + 1).Offset(r).ClearContents
    End With
    
    MsgBox "Data aligned.", vbInformation
    
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文