【问题标题】:Creating one row of information in excel using a unique value使用唯一值在excel中创建一行信息
【发布时间】:2012-05-30 16:38:48
【问题描述】:

这是我的第一篇文章。我目前正在处理一个项目,该项目需要我使用几个不同的工作表才能创建一个邮件主工作表,以便进行邮件合并。工作表包含有关不同购买的信息,每个购买者都有自己的 ID 号。下面是我的电子表格现在的样子(但我确实有更多列):

ID  Salutation     Address  ID      Name        Donation      ID  Name          Tickets
9   Mr. John Doe    123     12  Ms. Jane Smith   100.00       12  Ms.Jane Smith   300.00 
12  Ms. Jane Smith  456     22  Mr. Mike Man     500.00       84  Ms. Jo Smith    300.00 

我想做的是以某种方式对我的数据进行排序,以使具有相同唯一标识符 (ID) 的所有字符都排在同一行上。例如 ID 12 Jane Smith - 她的所有信息都将显示在与她的 ID 号匹配的她的姓名下,ID 22 将与 22 等匹配......

当我将所有电子表格合并在一起时,我按 ID 号对它们进行了排序,但我的问题是,并非所有捐款的人都买了票,或者有些人只买了票而我们什么都没买,所以排序不起作用.

希望这是有道理的。

提前致谢。

【问题讨论】:

    标签: excel vba uniqueidentifier mergesort


    【解决方案1】:

    您可以在 Excel VBA 中执行此操作。如果您之前没有在 Excel 中使用过 VBA,请参阅下面的链接以了解如何访问它。

    http://msdn.microsoft.com/en-us/library/ee814737.aspx

    确保在尝试任何操作之前备份您的电子表格,以防出现问题!

    我写了一些东西,可以将辅助工作表中的数据复制到主工作表中。只需打开 VBA 编辑器,然后粘贴代码即可。

    接下来,编辑ConsolidateWorksheets() 函数,使其具有适合您工作表的名称。如果您有其他工作表,请声明它们并添加另一行,为添加的工作表调用 ProcessWorksheet 子例程。

    此代码会在找到匹配的 ID 时将您的门票和捐赠工作表中的数据复制到您的主工作表中。如果没有匹配的 id,则不会为该行复制任何内容。

    Option Explicit
    
    Sub ConsolidateWorksheets()
    
        'declare the worksheets you are using
        Dim mainWks As Worksheet
        Dim ticketsWks As Worksheet
        Dim donationsWks As Worksheet
    
        'set the worksheet names
        Set mainWks = ThisWorkbook.Worksheets("Sheet1")
        Set ticketsWks = ThisWorkbook.Worksheets("Sheet2")
        Set donationsWks = ThisWorkbook.Worksheets("Sheet3")
    
        Call ProcessWorksheet(mainWks, ticketsWks)
        Call ProcessWorksheet(mainWks, donationsWks)
    
    End Sub
    
    ' copies data from the otherWks to the mainWks
    Sub ProcessWorksheet(mainWks As Worksheet, otherWks As Worksheet)
    
        Dim i As Integer
        Dim rowId As Integer
    
        Dim otherRowIndex As Integer
        Dim otherLastColIndex As Integer
    
        Dim lastRowIndex As Integer
    
        Dim pasteColStart As Integer
        Dim pasteColEnd As Integer
    
        ' figure out the last row in the main sheet
        lastRowIndex = mainWks.UsedRange.Rows.count
        otherLastColIndex = otherWks.UsedRange.Columns.count
    
        ' figure out where to copy and paste from
        ' this assumes that the id row is always the first row in every sheet
        pasteColStart = mainWks.UsedRange.Columns.count + 1
        pasteColEnd = pasteColStart + (otherLastColIndex - 2)
    
        ' copy column headers
        otherWks.Activate
        otherWks.Range(Cells(1, 2), Cells(1, otherLastColIndex)).Copy
        mainWks.Activate
        mainWks.Range(Cells(1, pasteColStart), Cells(1, pasteColEnd)).PasteSpecial
    
        ' loop through all the rows of the main sheet
        For i = 2 To lastRowIndex
            ' get row id from first cell in current row
            rowId = Cells(i, 1).Value
            'lookup row id in other worksheets
            otherRowIndex = FindIdRowInWks(otherWks, rowId)
    
            If otherRowIndex <> 0 Then
                otherWks.Activate
                otherWks.Range(Cells(otherRowIndex, 2), Cells(otherRowIndex, otherLastColIndex)).Copy
                mainWks.Activate
                mainWks.Range(Cells(i, pasteColStart), Cells(i, pasteColEnd)).PasteSpecial
            End If
        Next i
    
    End Sub
    
    ' loops through the given worksheet, looks for a given id in the first column
    ' and returns the row index where the id was found. returns 0 if nothing found.
    Public Function FindIdRowInWks(wks As Worksheet, idToFind As Integer) As Integer
        Dim lastRow As Integer
        lastRow = wks.Range("A" & Rows.count).End(xlUp).Row
    
        Dim rowNumber As Integer
        rowNumber = 0
    
        Dim i As Integer
        For i = 2 To lastRow
            If (Cells(i, 1).Value = idToFind) Then
                rowNumber = i
            End If
        Next i
        FindIdRowInWks = rowNumber
    End Function
    

    希望有帮助!

    【讨论】:

      【解决方案2】:

      您可以在不使用 VBA 的情况下分 3 步完成此操作:

      1) 创建新工作表

      2) 在新工作表上获取 ID 和称呼的综合列表

      对于 XL2007,请参阅删除重复项,对于 XL2003,请参阅高级过滤器。

      3) 像这样使用 vlookups 和 iferror 创建一个设置,根据原始工作表中的相关部分获取您想要的值。查看公式栏中的图像和 vlookup 公式

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-08
        • 2019-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-30
        相关资源
        最近更新 更多