如何从多个Excel文件中获取两列的唯一组合?

huangapple go评论96阅读模式
英文:

How to get unique combinations of two columns from multiple excel files?

问题

我有一个文件夹中有多个Excel表格。它们具有“Department”和“Grade”列。我想要从所有文件中获取这两列的唯一组合,并将其保存到不同的Excel工作簿中。
例如,如果一个文件中有这个:

如何从多个Excel文件中获取两列的唯一组合?

我应该得到:

如何从多个Excel文件中获取两列的唯一组合?

类似地,它应该只从其余文件中获取这两列的唯一组合。因此,如果其余文件中包含“IT”和“P01”,那么它应该被忽略,因为已经存在。

英文:

I have multiple excel sheets in a folder. They have columns "Department" and "Grade". I want to get the unique combinations of these two columns from all the files to a different excel workbook.
For eg. if I have this in one file,

如何从多个Excel文件中获取两列的唯一组合?

I should get:

如何从多个Excel文件中获取两列的唯一组合?

And similarly, it should take only unique combinations of these two columns only from the remaining files. So if any of the remaining files contains "IT" and "P01", then it should be ignored as it is already there.

答案1

得分: 1

这将复制每个Excel文件的值到活动工作簿中。

假设单元格的值位于每个文件的列A和B,从第2行开始,请相应地修改路径、源工作表名称和目标工作表名称。

  1. Sub MergeDataFromFiles()
  2. Dim filePath As String
  3. Dim fileName As String
  4. Dim wbSource As Workbook
  5. Dim wsSource As Worksheet
  6. Dim lastRow As Long
  7. Dim sourceRow As Range
  8. Dim searchValue As String
  9. Dim foundRange As Range
  10. Application.ScreenUpdating = False
  11. Application.DisplayAlerts = False
  12. ' 设置包含文件的文件夹路径
  13. filePath = "C:\Users\myuser\Documents\sample excel files\"
  14. ' 获取文件夹中的第一个文件
  15. fileName = Dir(filePath & "*.xlsx")
  16. ' 循环遍历文件夹中的每个文件
  17. Do While fileName <> ""
  18. ' 打开文件
  19. Set wbSource = Workbooks.Open(filePath & fileName)
  20. Set wsSource = wbSource.Sheets(1) ' 假设数据在第一个工作表中
  21. ' 获取活动工作簿中的最后一行
  22. lastRow = ActiveWorkbook.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
  23. ' 循环遍历源工作表中的每一行
  24. For Each sourceRow In wsSource.Range("A2:B" & wsSource.Cells(Rows.Count, "A").End(xlUp).Row)
  25. ' 在源工作簿中连接列AB的值
  26. searchValue = sourceRow.Cells(1).Value & sourceRow.Cells(2).Value
  27. ' 在活动工作簿中查找连接后的值
  28. Set foundRange = ActiveWorkbook.Sheets(1).Range("A2:B" & lastRow).Find(searchValue, LookIn:=xlValues, LookAt:=xlWhole)
  29. ' 如果未找到,则将值复制到活动工作簿中的下一个空行
  30. If foundRange Is Nothing Then
  31. lastRow = lastRow + 1
  32. ActiveWorkbook.Sheets(1).Cells(lastRow, "A").Value = sourceRow.Cells(1).Value
  33. ActiveWorkbook.Sheets(1).Cells(lastRow, "B").Value = sourceRow.Cells(2).Value
  34. End If
  35. Next sourceRow
  36. ' 关闭源工作簿
  37. wbSource.Close SaveChanges:=False
  38. ' 获取文件夹中的下一个文件
  39. fileName = Dir
  40. Loop
  41. Application.ScreenUpdating = True
  42. Application.DisplayAlerts = True
  43. MsgBox "复制完成。"
  44. End Sub
英文:

this will copy the values from each excel file to the activeworkbook

assuming the cell values are in column A and B of each files starting at row 2, modify path,source sheet name and destination sheet name accordingly

  1. Sub MergeDataFromFiles()
  2. Dim filePath As String
  3. Dim fileName As String
  4. Dim wbSource As Workbook
  5. Dim wsSource As Worksheet
  6. Dim lastRow As Long
  7. Dim sourceRow As Range
  8. Dim searchValue As String
  9. Dim foundRange As Range
  10. Application.ScreenUpdating = False
  11. Application.DisplayAlerts = False
  12. &#39; Set the path to the folder containing the files
  13. filePath = &quot;C:\Users\myuser\Documents\sample excel files\&quot;
  14. &#39; Get the first file in the folder
  15. fileName = Dir(filePath &amp; &quot;*.xlsx&quot;)
  16. &#39; Loop through each file in the folder
  17. Do While fileName &lt;&gt; &quot;&quot;
  18. &#39; Open the file
  19. Set wbSource = Workbooks.Open(filePath &amp; fileName)
  20. Set wsSource = wbSource.Sheets(1) &#39; Assuming the data is in the first sheet
  21. &#39; Get the last row in the active workbook
  22. lastRow = ActiveWorkbook.Sheets(1).Cells(Rows.Count, &quot;A&quot;).End(xlUp).Row
  23. &#39; Loop through each row in the source worksheet
  24. For Each sourceRow In wsSource.Range(&quot;A2:B&quot; &amp; wsSource.Cells(Rows.Count, &quot;A&quot;).End(xlUp).Row)
  25. &#39; Concatenate values from columns A and B in the source workbook
  26. searchValue = sourceRow.Cells(1).Value &amp; sourceRow.Cells(2).Value
  27. &#39; Find the concatenated value in the active workbook
  28. Set foundRange = ActiveWorkbook.Sheets(1).Range(&quot;A2:B&quot; &amp; lastRow).Find(searchValue, LookIn:=xlValues, LookAt:=xlWhole)
  29. &#39; If not found, copy the values to the next empty row in the active workbook
  30. If foundRange Is Nothing Then
  31. lastRow = lastRow + 1
  32. ActiveWorkbook.Sheets(1).Cells(lastRow, &quot;A&quot;).Value = sourceRow.Cells(1).Value
  33. ActiveWorkbook.Sheets(1).Cells(lastRow, &quot;B&quot;).Value = sourceRow.Cells(2).Value
  34. End If
  35. Next sourceRow
  36. &#39; Close the source workbook
  37. wbSource.Close SaveChanges:=False
  38. &#39; Get the next file in the folder
  39. fileName = Dir
  40. Loop
  41. Application.ScreenUpdating = True
  42. Application.DisplayAlerts = True
  43. Msgbox &quot;Copy complete.&quot;
  44. End Sub

huangapple
  • 本文由 发表于 2023年5月28日 20:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定