使用唯一值在Excel中创build一行信息

这是我的第一篇文章。 我目前正在开发一个工作项目,要求我使用几个不同的工作表来创build一个邮件主工作表,以便进行邮件合并。 工作表包含有关不同购买的信息,每个购买者都有自己的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 

我想要做的是以某种方式对我的数据进行sorting,以便具有相同唯一标识符(ID)的每个标签在同一行上排列。 例如ID 12 Jane Smith – 所有的信息都会以她的ID号码显示出来,ID 22将会与22等相匹配。

当我将所有的电子表格合并在一起时,我都按身份证号码sorting,但问题是,并不是所有捐款都买了票,有的人刚刚买票,也没有我们,所以sorting不起作用。

希望这是有道理的。

提前致谢。

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

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

确保您在尝试任何这种情况之前备份电子表格,以免出现问题!

我写了一些东西,将副本工作表中的数据复制到主工作表中。 只要打开VBA编辑器,并粘贴代码。

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

当代码find一个匹配的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 

希望有所帮助!

您可以在没有VBA的情况下以三步完成此操作:

1)创build新的工作表

2)在新表上得到ID和称呼的综合名单

对于XL2007,请参阅删除重复,对于XL2003请参阅高级filter。

3)创build一个安装程序像使用vlookups和iferror一样根据原始工作表中的相关部分获取所需的值。 在配方栏中查看图像和vlookup公式

在这里输入图像说明