匹配值并复制到同一行 Excel VBA
Match Value and copy onto same line Excel VBA
希望你一切都好
我有一些 VBA 代码,我在使用时遇到了一些问题,想知道是否有人可以伸出援手,好吗?
问题;
如果sheet1上有多行需要复制,我只能复制一行。我不知道如何让它搜索、匹配然后复制多行。
编辑
我希望实现的是复制列中的值; M、N 和 O (支付日期、支付金额、注释) 到 sheet 2 上 table 的各自行中,第 I、J 和 L (收到金额、收到日期和备注)
我的 VBA 技能有点有限啊,所以我从来没有在这方面走得太远。
已更新 sheet 1 和 sheet 2 的屏幕截图
编辑
Sub missingData()
Dim s1 As Worksheet
Dim s2 As Worksheet
Set s1 = ActiveWorkbook.Worksheets("Sheet1")
Set s2 = ActiveWorkbook.Worksheets("Sheet2")
lrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Dim i As Integer
i = 1 //start index
Do While (i < lrow)
For j = 1 To 7
If s1.Cells(i, j) <> "" And s2.Cells(i, j) = "" Then
s2.Cells(i, j) = s1.Cells(i, j)
End If
Next j
i = i + 1
Loop
End Sub
我认为它会解决问题,但如果你的文件有大数据,它可能需要一些时间
将匹配行复制到 Excel Table (ListObject)
请注意,table 的 D2
中的一个简单公式(复制到其余单元格)也可以执行相同的操作:
=IFERROR(INDEX(Sheet1!D:D,MATCH([@Invoice NR],Sheet1!$A:$A,0)),"")
Option Explicit
Sub UpdateTable()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Source
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "A").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data in column range
Dim srg As Range: Set srg = sws.Range("A2:A" & slRow) ' to lookup
Dim scrg As Range: Set scrg = srg.EntireRow.Columns("D:G") ' to copy
Dim cCount As Long: cCount = scrg.Columns.Count ' how many columns in 'D:G'?
' Destination
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet2")
Dim dtbl As ListObject: Set dtbl = dws.ListObjects("Table1")
Dim srIndex As Variant
Dim dCell As Range
' Copy.
For Each dCell In dtbl.ListColumns(1).DataBodyRange
srIndex = Application.Match(dCell.Value, srg, 0) ' find a match
If IsNumeric(srIndex) Then ' if match was found then copy if not blank
If Application.CountBlank(scrg.Rows(srIndex)) < cCount Then
dCell.Offset(, 3).Resize(, cCount).Value _
= scrg.Rows(srIndex).Value
End If
End If
Next dCell
' Inform.
MsgBox "Table updated."
End Sub
希望你一切都好
我有一些 VBA 代码,我在使用时遇到了一些问题,想知道是否有人可以伸出援手,好吗?
问题; 如果sheet1上有多行需要复制,我只能复制一行。我不知道如何让它搜索、匹配然后复制多行。
编辑 我希望实现的是复制列中的值; M、N 和 O (支付日期、支付金额、注释) 到 sheet 2 上 table 的各自行中,第 I、J 和 L (收到金额、收到日期和备注)
我的 VBA 技能有点有限啊,所以我从来没有在这方面走得太远。
已更新 sheet 1 和 sheet 2 的屏幕截图
编辑
Sub missingData()
Dim s1 As Worksheet
Dim s2 As Worksheet
Set s1 = ActiveWorkbook.Worksheets("Sheet1")
Set s2 = ActiveWorkbook.Worksheets("Sheet2")
lrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Dim i As Integer
i = 1 //start index
Do While (i < lrow)
For j = 1 To 7
If s1.Cells(i, j) <> "" And s2.Cells(i, j) = "" Then
s2.Cells(i, j) = s1.Cells(i, j)
End If
Next j
i = i + 1
Loop
End Sub
我认为它会解决问题,但如果你的文件有大数据,它可能需要一些时间
将匹配行复制到 Excel Table (ListObject)
请注意,table 的
D2
中的一个简单公式(复制到其余单元格)也可以执行相同的操作:=IFERROR(INDEX(Sheet1!D:D,MATCH([@Invoice NR],Sheet1!$A:$A,0)),"")
Option Explicit
Sub UpdateTable()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Source
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "A").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data in column range
Dim srg As Range: Set srg = sws.Range("A2:A" & slRow) ' to lookup
Dim scrg As Range: Set scrg = srg.EntireRow.Columns("D:G") ' to copy
Dim cCount As Long: cCount = scrg.Columns.Count ' how many columns in 'D:G'?
' Destination
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet2")
Dim dtbl As ListObject: Set dtbl = dws.ListObjects("Table1")
Dim srIndex As Variant
Dim dCell As Range
' Copy.
For Each dCell In dtbl.ListColumns(1).DataBodyRange
srIndex = Application.Match(dCell.Value, srg, 0) ' find a match
If IsNumeric(srIndex) Then ' if match was found then copy if not blank
If Application.CountBlank(scrg.Rows(srIndex)) < cCount Then
dCell.Offset(, 3).Resize(, cCount).Value _
= scrg.Rows(srIndex).Value
End If
End If
Next dCell
' Inform.
MsgBox "Table updated."
End Sub