【发布时间】:2020-03-18 02:03:16
【问题描述】:
在工作簿 1 中,我有一个跟踪肉类产品库存的电子表格。 第 1 行用于列名称:A 列中的“包裹追踪编号”以及其他列中与包裹相关的其他数据(例如“出口日期”、“重量”和“内容”等)。
第一列描述了包裹的“内容”,这些包裹都包含“肉类”。
此电子表格中的信息行是从工作簿 2 中复制的,其中包含第一列中包含“肉类”、“奶酪”、“牛奶”和“鸡蛋”的包裹。
两个工作簿在第 1 行中具有相同的列名。
在工作簿 1 中,我更新了某些行的数据,并且我希望通过复制工作簿 1 的行并将它们粘贴到工作簿 2 中列中“包裹跟踪号”的行中的方式在工作簿 2 中应用更改A 匹配。
到目前为止,我有从工作簿 2 复制所有“肉类”包裹行并将它们粘贴到工作簿 1 中的代码,但现在我需要帮助来应对这种新情况。
通过打开工作簿 2 并按下命令按钮来执行程序,该按钮打开工作簿 1 并开始将行复制到 Meat 工作表。
这里是:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False ' Screen Update application turned off in order to make program run faster
Dim y As Workbook '
Dim sh As Worksheet '
Set y = Workbooks.Open("\\SCF1\USERS-D\Robert\My Documents\Excel VBA code\Meat.xlsx") '
a = ThisWorkbook.Worksheets("Products").Cells(Rows.Count, 1).End(xlUp).Row
Set sh = Workbooks("Meat.xlsx").Worksheets("Meat")
With ThisWorkbook.Worksheets("Products")
For i = 2 To a ' value ''i'' is the column number
If ThisWorkbook.Worksheets("Products").Cells(i, 9).Value Like "*Meat*" And IsError(Application.Match(.Cells(i, "A").Value, sh.Columns("A"), 0)) Then ' this sets the condition for which the data can only be copied if the row has '' Meat '' included in the 9th column (substance) and if the row is not already copied in the Meat worksheet.
ThisWorkbook.Worksheets("Products ").Rows(i).Copy
Workbooks("Meat.xlsx").Worksheets("Meat").Activate
b = Workbooks("Meat.xlsx").Worksheets("Meat ").Cells(Rows.Count, 1).End(xlUp).Row
Workbooks("Meat.xlsx").Worksheets("Meat").Cells(b + 1, 1).Select
ActiveSheet.Paste
ThisWorkbook.Worksheets("Products").Activate
End If
Next
On Error Resume Next '1004 error kept appearing so this function allows us to continue to next step without error appearing
ThisWorkbook.Worksheets("Products").Cells(1, 1).Select
End With
MsgBox "All rows from Products worksheet have been copied."
Application.ScreenUpdating = True
End Sub
非常感谢任何帮助。谢谢。
【问题讨论】:
-
旁注:this question 可能会有所帮助。
-
考虑使用数据库(如兄弟应用程序、MS Access)来更好地管理结构化数据,而无需复制/粘贴和循环过程。
-
可以识别更新的行还是要复制所有行?