Excel VBA:我试图比较具有某些条件的书籍与数据之间的数据,并仅传输不匹配的数据第二本书

book1sheet1数据如下;

Rows: from 11 to 17 Column B: 101, 102, 103, 104, 105, 106, 107 Column C: test1, test2, test3, test4, test5, test6, test7 Column D: 12/1/15, 12/2/15, 12/3/15, 12/4/15, 12/5/15, 12/6/15, 12/7/15 Column E: 12/7/15, 12/7/15, 12/8/15, 12/10/15, 12/2/15, 11/30/15, 12/15/15 J11: $45.00 J16: $90.00 K12: $50.00, K13: $100.00, K14: $45.0C L15: $50.00 M14: $45.00, M17: $250.00 

book2sheet1数据应该如下;

 Row: 4 to 7 Column A: 12/2/15, 12/3/15, 12/4/15, 12/7/15 Column B: 12/7/15, 12/8/15, 12/10/15, 12/15/15 Column C: test2, test3, test4, test7 Column H: 102, 103, 104, 107 Column I: $50.00, $100.00, $90.00, $250.00 

我在这里尝试的是,我想看看book1 / sheet1中的“K11”或“M11”是否为> 0,如果是,则将book1 / sheet1中的“C11”和“E11”的值与列C和book2 / sheet1中的B列。 如果这两个值在book2 / sheet1的C列和B列中相同,则检查book1 / sheet1中的E12并继续。 如果在book2 / sheet1的C列和B列中的值不匹配,那么我想从book1 / sheet1复制到book2 / sheet1的D11到A4,E11到B4,C11到C4,K11和M11到I4和B11到H4,继续在book2 / sheet1中的下一个空行。

所以代码应该只从book1 / sheet1中得到第12行到第14行和第17行的数据,并把它放在第4行到第7行的book2 / sheet1中。我运行下面的代码,但是它没有复制任何内容。

 Sub test5() Dim lrow1 As Long Dim lrow2 As Long Dim erow As Long Dim name1 As String Dim name2 As String Dim mydate1 As Date Dim mydate2 As Date Dim check As Boolean Workbooks.Open Filename:="C:\Users\tp142d\Documents\Book2.xlsx" lrow1 = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row lrow1 = Workbooks("Book2").Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row For i = 11 To lrow1 name1 = Sheets("Sheet1").Cells(i, "C").Value mydate1 = Sheets("Sheet1").Cells(i, "E").Value check = False For j = 3 To lrow2 name2 = Workbooks("Book2").Sheets("Sheet1").Cells(j, "C").Value mydate2 = Workbooks("Book2").Sheets("Sheet1").Cells(j, "B").Value If Sheets("Sheet1").Cells(i, "K") > 0 And Sheets("Sheet1").Cells(i, "M") > 0 And name1 <> name2 And mydate1 <> mydate2 Then check = True End If Next j If Not check Then Sheets("Sheet1").Cells(i, "D").Copy erow = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, 1).End(xlUp).Offset(1, 0).Row Workbooks("Book1").Sheets("sheet1").Cells(erow, "A").PasteSpecial Sheets("Sheet1").Cells(i, "E").Copy Workbooks("Book2").Sheets("Sheet1").Cells(erow, "B").PasteSpecial Sheets("Sheet1").Cells(i, "C").Copy Workbooks("Book2").Sheets("Sheet1").Cells(erow, "C").PasteSpecial Sheets("Sheet1").Cells(i, "B").Copy Workbooks("Book2").Sheets("Sheet1").Cells(erow, "H").PasteSpecial Sheets("Sheet1").Cells(i, "K").Copy Workbooks("Book2").Sheets("Sheet1").Cells(erow, "I").PasteSpecial ActiveWorkbook.Save End If Next i End Sub 

首先,你真的需要看一下@Ambie在他的回答中所陈述的内容,因为这会有助于看到你已经向后复制和粘贴。

这段代码应该做你想做的事情:

  Sub test5() Dim lrow1 As Long Dim lrow2 As Long Dim erow As Long Dim name1 As Variant Dim name2 As Variant Dim mydate1 As Variant Dim mydate2 As Variant Dim check As Boolean Dim ows As Worksheet Dim tws As Worksheet Dim owb As Workbook Dim twb As Workbook Dim check2 As Boolean Set owb = ActiveWorkbook Set twb = ActiveWorkbook 'Workbooks.Open(Filename:="C:\Users\tp142d\Documents\Book2.xlsx") Set ows = owb.Sheets("Sheet21") Set tws = twb.Sheets("Sheet28") lrow1 = ows.Range("B" & Rows.Count).End(xlUp).Row lrow2 = tws.Range("A" & Rows.Count).End(xlUp).Row For i = 11 To lrow1 name1 = ows.Cells(i, "C").Value mydate1 = ows.Cells(i, "E").Value check = False check2 = False If ows.Cells(i, "K") > 0 Or ows.Cells(i, "M") > 0 Then check2 = True For j = 3 To lrow2 name2 = tws.Cells(j, "C").Value mydate2 = tws.Cells(j, "B").Value If name1 = name2 And mydate1 = mydate2 Then check = True Exit For End If Next j End If If Not check And check2 Then erow = tws.Cells(tws.Rows.Count, 1).End(xlUp).offset(1, 0).Row tws.Cells(erow, "A").Value = ows.Cells(i, "D").Value tws.Cells(erow, "B").Value = ows.Cells(i, "E").Value tws.Cells(erow, "C").Value = ows.Cells(i, "C").Value tws.Cells(erow, "H").Value = ows.Cells(i, "B").Value tws.Cells(erow, "I").Value = ows.Cells(i, "K").Value + ows.Cells(i, "M").Value ' twb.Save End If 

接下来我

结束小组

线索在您的标题: 比较两个工作簿之间的数据

您的代码是在同一个工作簿上比较不同的单元格。 原因是你没有明确地引用Book1对象,并且没有显式的WorkBook对象,VBA将采用ActiveWorkBook 。 当你打开一个Workbooks.Open Filename:="C:\Users\tp142d\Documents\Book2.xlsx" (像这样,例如, Workbooks.Open Filename:="C:\Users\tp142d\Documents\Book2.xlsx" ),它就成为ActiveWorkbook

恐怕你要比较Book2中的数据和Book2中的数据。

为了防止这样的行

 name1 = Sheets("Sheet1").Cells(i, "C").Value 

需要包含WorkBook对象,如下所示:

 name1 = WorkBooks("Book1").Sheets("Sheet1").Cells(i, "C").Value 

更容易设置两个WorkBook对象variables – 更容易input,更容易理解,更容易阅读。 我也会对表单名称也做同样的事情。 使用您的命名协议,一些示例代码将如下所示:

 Dim wb1 As WorkBook Dim wb2 As WorkBook Dim ws1 As WorkSheet Dim ws2 As WorkSheet 'Define your workbooks Set wb1 = WorkBooks("Book1") Set wb2 = WorkBooks.Open Filename:="C:\Users\tp142d\Documents\Book2.xlsx" 'Define your worksheets Set ws1 = wb1.WorkSheets("Sheet1") Set ws2 = wb2.WorkSheets("Sheet1") 'Sample useage name1 = ws1.Cells(i, "C").Value name2 = ws2.Cells(j, "C").Value 

我个人的规则总是包含WorkBookWorkSheet对象,即使我只打开一个Workbook 。 所有你需要的是一个用户激活一个不同的工作簿或不同的工作表,你已经失去了你的代码的控制 – 像Cells.Clear可以肯定地把你从用户的圣诞卡列表。 如果您要通过两个工作簿或工作表比较数据,那么这是必须的。

顺便说一句,我怀疑这行是一个错字:

 lrow1 = Workbooks("Book2").Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row 

不应该是:

 lrow2 = Workbooks("Book2").Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row