如何通过仅使用一个单元格的数据来获取列中的特定数据

我有很多销售订单,我需要填写来自不同的Excel工作簿和工作表的大量数据。 所以我需要一个可以为我做的macros。 我有4个不同的Excel工作簿。 1数据必须插入,3我必须从中获取数据。 所有Excel工作簿都列出了销售订单,

因此,macros必须扫描每个工作簿中的每个销售订单,然后从工作簿中获取特定的数据。

在这里输入图像说明

这是我粘贴数据的工作簿的一个例子。

在这里输入图像说明

这里有一个工作簿的例子,我必须复制数据。

所以它必须复制:

在这里输入图像说明

然后将其粘贴到工作簿中,我必须粘贴数据。

如果任何人都可以给我一个开始的地方或一些代码,我会非常高兴!

下面的编辑解决scheme。 不是最漂亮的代码,可能有更好的方法来做到这一点,但它应该做你想要的东西在迂回的方式。

将此macros复制到您的主簿中的一个模块中,并将其复制 XLSM文件的某个位置。

将所有您想要其他文件夹中复制的3张表格(或任意数量)放在某个位置,然后插入该macros指定的文件位置。

这应该循环遍历指定位置中的每个文件,抓取除标题行以外的所有已使用的单元格,并将其粘贴到主书册Sheet2中的下一个可用行中。

然后,macros将针对销售订单号对复制的数据执行一次查找,并粘贴特殊值以将其重新转换为值。 最后,它将清除Sheet2,准备下次运行它。

显然,如果您的工作表被命名为别的东西,您可以修改,或通过编号引用它们,但至less应该给您一个起点。

Sub CopyTheData() Dim Folder As String Dim File As Variant Dim wbk As Workbook Dim This As Worksheet, That As Worksheet Folder = "[FOLDER LOCATION HERE]" File = Dir(Folder & "*.*") Set This = ThisWorkbook.Sheets(1) Set That = ThisWorkbook.Sheets(2) Application.ScreenUpdating = False While (File <> "") Set wbk = Workbooks.Open(Folder & File) With wbk Range("A1").Select Selection.CurrentRegion.Select Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select Selection.Copy Destination:=That.Range("B65536").End(xlUp)(2).Offset(0, -1) End With wbk.Close File = Dir Wend This.Activate This.Range("B2", Range("A2").End(xlDown).Offset(0, 1)).Formula = "=VLOOKUP(A2, Sheet2!$A:$H,2,FALSE)" This.Range("C2", Range("A2").End(xlDown).Offset(0, 2)).Formula = "=VLOOKUP(A2,Sheet2!$A:$H,4,FALSE)" This.Range("D2", Range("A2").End(xlDown).Offset(0, 3)).Formula = "=VLOOKUP(A2,Sheet2!$A:$H,6,FALSE)" This.Range("E2", Range("A2").End(xlDown).Offset(0, 4)).Formula = "=VLOOKUP(A2,Sheet2!$A:$H,8,FALSE)" With This.Range("B2", Range("A2").End(xlDown).Offset(0, 4)) .Copy .PasteSpecial Paste:=xlPasteValues End With Columns("D:E").NumberFormat = "m/d/yyyy" That.Cells.ClearContents Application.ScreenUpdating = True End Sub 

哈利的代码看起来不错,如果你只想要使用某些表格,尽pipe你可以在你想要添加到主表格的每张表格中粘贴以下代码。

 Dim owb As Workbook Dim Master As Worksheet Dim Slave As Worksheet 'the following declares both master and slave as worksheets fpath = "location of master workbook" Set owb = Application.Workbooks.Open(fpath) 'opens the file path Set Master = ThisWorkbook.Worksheets("name of sheet in workbook your pasting from") 'declares this workbook and sheet as "master" Set Slave = owb.Worksheets("name of sheet in master you are pasting to") 'declares the workbook and sheet you're copying to as "slave" For j = 1 To 10000 '(the master sheet) 'goes through each row from 1 to 10000 For i = 1 To 10000 '(the slave sheet) 'again does the same and the slave sheet If Trim(Master.Cells(j, 4).Value2) = vbNullString Then Exit For 'if the ID is blank it will exit and move on to the next row If Master.Cells(j, 1).Value = Slave.Cells(i, 1).Value Then 'the 1 represents column A, if cell in column A matches the cell in column D in the masterwork book then it will.. Slave.Cells(i, 2).Value = Master.Cells(j, 2).Value 'the cell here represent column B as it's a 2, you can change and add as many as you like to bring through the column End If Next Next MsgBox ("Successful")