【发布时间】:2020-08-04 09:39:31
【问题描述】:
我无法弄清楚如何将 vlookup 代码输入到我现有的代码(如下)中,以查看另一个工作簿并更新我的主工作簿。 Vlookup 公式如下所示:
=VLOOKUP(B2,'[Loading Summary.xlsx]SHIPPING'!$A:$K,11,FALSE .
我还提供了几个屏幕截图。两个工作簿主数据和原始数据都有多个相应的选项卡。棘手的部分是;我不能在每次原始数据更新时都进行复制和粘贴。我的团队在主日程表上做笔记,不希望他们的笔记或字段改变,他们只想改变小时列。
Master Schedule - Workbook
Raw Data - Workbook
Error after trying Williams code
Sub Import_Awea_LP2516()
'Prevents Clipboard Pop-up from appearing.
Application.DisplayAlerts = False
'Prevents screen flicker and makes the macro run faster.
Application.ScreenUpdating = False
'Ensures the Loading Summary is open
Workbooks.Open Filename:=Environ("USERPROFILE") & "\Dropbox (Napoleon Machine)\Operations Management\#MASTER SCHEDULE\Shop Schedule V4\Loading Summary.xlsx"
'Copies new line items from loading summary and pastes them at the bottom of the master schedule (per machine)
Dim ws1 As Worksheet: Set ws1 = Workbooks("Shop Schedule - Master V4.xlsm").Worksheets("Awea-LP2516")
Dim ws2 As Worksheet: Set ws2 = Workbooks("Loading Summary.xlsx").Worksheets("LP2516")
Dim criteria As String
Dim found As Range
Dim i As Long
For i = 2 To 500
criteria = ws1.Cells(i, 2).Value
On Error Resume Next
Set found = ws2.Range("A:A").Find(What:=criteria, LookAt:=xlWhole)
On Error GoTo 0
If found Is Nothing Then
ws1.Cells(i, 2).EntireRow.Interior.ColorIndex = 22
End If
Next i
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Set wsCopy = Workbooks("Loading Summary.xlsx").Worksheets("LP2516")
Set wsDest = Workbooks("Shop Schedule - Master V4.xlsm").Worksheets("Awea-LP2516")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(2).Row
wsCopy.Range("A2:O" & lCopyLastRow).Copy _
wsDest.Range("B" & lDestLastRow)
Windows("Shop Schedule - Master V4.xlsm").Activate
Dim rCell As Range
Dim rRange As Range
Dim lCount As Long
Set rRange = Range("B2", Range("B" & Rows.Count).End(xlUp))
lCount = rRange.Rows.Count
For lCount = lCount To 1 Step -1
With rRange.Cells(lCount, 1)
If WorksheetFunction.CountIf(rRange, .Value) > 1 Then
.EntireRow.Delete
End If
End With
Next lCount
Windows("Shop Schedule - Master V4.xlsm").Activate
Rows(2).EntireRow.Delete
Range("A2").Select
Call Master_Sheet_Cleanup
Workbooks("Loading Summary.xlsx").Close SaveChanges:=False
'Turns display alerts back on
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
仅供参考,您不需要将
Find()包装在On Error Resume Next中 - 如果没有匹配项,它不会引发错误。 -
谢谢蒂姆,还有你是如何让代码正确格式化的。我在这里问了几个问题,当我粘贴代码时,它从不使用 VBA 颜色格式。
-
不要使用“sn-p”按钮 - 以三个反引号开始您的代码块,后跟
lang-vb,并以 3 个反引号结束代码 -
Dim res: res = Application.Vlookup(workSheetHere.Range("B2").Value, Workbooks("Loading Summary.xlsx").Worksheets("Shipping").Range("A:K"),11, False)然后在继续之前使用If IsError(res)检查是否找到匹配项。 -
蒂姆,感谢您对反引号和发帖的提醒。我阅读了您建议的 App.Vlookup。警告,你将不得不为我打破 Crayola 蜡笔,哈哈。我正在处理我没有创建的 VBA 代码,当涉及到 VBA 代码时,我几乎是无脑的。我应该将您提供的代码放在程序中吗?我想这是因为它对我正在跳跃的床单是特别的。但是,我不知道在哪里放置代码。