【发布时间】:2021-10-10 10:08:24
【问题描述】:
我正在尝试将数据从源表中提取到主表中。
- 如果主工作表中有任何现有记录,请使用源工作表中的最新数据更新主工作表中的记录。
- 否则将源工作表中的数据添加到主工作表中。
我为一位客户拼凑了代码(单张)。
如何修改它以允许更新多个工作表?
我知道我需要循环工作表,但我遇到了错误。
Sub Update()
Dim wsSrc As Worksheet, wsDest As Worksheet, i As Integer, j As Integer, k As Integer, srcLastRow As Long, destLastRow As Long, srcFndVal As String, destFndCell As Range, srcValRow As Long, destValRow As Long, destFndVal As String, srcFndCell As Range
Application.ScreenUpdating = False
Set wsSrc = Worksheets("Cust A")
Set wsDest = Worksheets("Master")
srcLastRow = wsSrc.Cells(Rows.Count, "BA").End(xlUp).Row
destLastRow = wsDest.Cells(Rows.Count, "A").End(xlUp).Row
j = wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
With wsDest
For i = 4 To srcLastRow
srcFndVal = wsSrc.Cells(i, "AA")
Set destFndCell = .Range("A:A").Find(srcFndVal, LookIn:=xlValues)
If destFndCell Is Nothing And wsSrc.Cells(i, "AA").Value <> "" Then
.Range("A" & j & ":F" & j).Value = wsSrc.Range("AA" & i & ":AF" & i).Value
.Range("J" & j & ":K" & j).Value = wsSrc.Range("AG" & i & ":AH" & i).Value
.Range("G" & j & ":H" & j).Value = wsSrc.Range("AE" & i & ":AF" & i).Value
j = j + 1
Else
srcValRow = wsSrc.Range("AA:AA").Find(what:=srcFndVal, after:=wsSrc.Range("AA4"), LookIn:=xlValues).Row
destValRow = wsDest.Range("A:A").Find(what:=srcFndVal, after:=wsDest.Range("A4"), LookIn:=xlValues).Row
.Range("B" & destValRow & ":F" & destValRow).Value = wsSrc.Range("AB" & srcValRow & ":AF" & srcValRow).Value
.Range("J" & destValRow & ":K" & destValRow).Value = wsSrc.Range("AG" & srcValRow & ":AH" & srcValRow).Value
End If
Next
For k = 4 To destLastRow
destFndVal = wsDest.Cells(k, "A")
Set srcFndCell = wsSrc.Range("AA:AA").Find(destFndVal, LookIn:=xlValues)
If srcFndCell Is Nothing And wsDest.Cells(k, "A").Value <> "" Then
.Range("B" & k & ":F" & k).Value = vbNullString
End If
Next
End With
Application.ScreenUpdating = True
End Sub
I modified the code to loop through the worksheets in an array however there is an issue with getting the last row of the wsSrc.
>Run-time error 424 Object required.
Below line is highlighted
srcLastRow = wsSrc.Cells(Rows.Count, "AA").End(xlUp).Row
```vba
Sub Update()
Dim wsSrc As Variant, srcList As Variant, wsDest As Worksheet, i As Integer, j As Integer, k As Integer, srcLastRow As Long, destLastRow As Long, srcFndVal As String, destFndCell As Range, srcValRow As Long, destValRow As Long, destFndVal As String, srcFndCell As Range
Application.ScreenUpdating = False
srcList = Array("Cust A", "Cust B", "Cust C", "Cust D", "Cust E", "Cust F", "Cust G")
Set wsDest = Worksheets("Master")
srcLastRow = wsSrc.Cells(Rows.Count, "AA").End(xlUp).Row
destLastRow = wsDest.Cells(Rows.Count, "A").End(xlUp).Row
j = wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
For Each wsSrc In srcList
With wsDest
For i = 4 To srcLastRow
srcFndVal = wsSrc.Cells(i, "AA")
Set destFndCell = .Range("A:A").Find(srcFndVal, LookIn:=xlValues)
If destFndCell Is Nothing And wsSrc.Cells(i, "AA").Value <> "" Then
.Range("A" & j & ":F" & j).Value = wsSrc.Range("AA" & i & ":AF" & i).Value
.Range("J" & j & ":K" & j).Value = wsSrc.Range("AG" & i & ":AH" & i).Value
.Range("G" & j & ":H" & j).Value = wsSrc.Range("AE" & i & ":AF" & i).Value
j = j + 1
Else
srcValRow = wsSrc.Range("AA:AA").Find(what:=srcFndVal, after:=wsSrc.Range("AA4"), LookIn:=xlValues).Row
destValRow = wsDest.Range("A:A").Find(what:=srcFndVal, after:=wsDest.Range("A4"), LookIn:=xlValues).Row
.Range("B" & destValRow & ":F" & destValRow).Value = wsSrc.Range("AB" & srcValRow & ":AF" & srcValRow).Value
.Range("J" & destValRow & ":K" & destValRow).Value = wsSrc.Range("AG" & srcValRow & ":AH" & srcValRow).Value
End If
Next
For k = 4 To destLastRow
destFndVal = wsDest.Cells(k, "A")
Set srcFndCell = wsSrc.Range("AA:AA").Find(destFndVal, LookIn:=xlValues)
If srcFndCell Is Nothing And wsDest.Cells(k, "A").Value <> "" Then
.Range("B" & k & ":F" & k).Value = vbNullString
End If
Next
End With
Next wsSrc
Application.ScreenUpdating = True
End Sub
【问题讨论】: