我对此非常陌生,并试图建立一个比较两列A& A的自动电子表格。来自Sheet2和S& C的C.来自Sheet1的C.如果信息相同,请将Sheet2的D列中的相应数据粘贴到Sheet1的Z列。我一直在搜索并找到了很多例子,但没有一个能够满足我的要求。
一个问题是两列中的数据本质上非常相似。防爆。 Sheet2.ColumnA将有15个不同的数字重复,Sheet2.ColumnC将有一些可能重复的数字系列。基本上是:
If Sheet2.ColumnA1 & Sheet2.ColumnC1 = Sheet1.ColumnS1 & Sheet1.ColumnC1
Then Sheet1.ColumnZ1 = Sheet2.ColumnD1
我绝对可以合并两个数据集并留下:如果Sheet2。(A1& C1)= Sheet1。(S1& C1)然后Sheet1.Z1 = Sheet2.D1,但是,我坚持找到任何组合匹配,然后复制信息。例如:
A C S C D
2 34 3 55 Waivered
3 55 4 31 covered
5 12 5 55 Skittles
5 55
从以下列表中,我希望“Waivered”和“Skittles”分别复制到工作表1.column(Z),第2行和第4行。
答案 0 :(得分:0)
如果Sheet1看起来与此相似:
和Sheet2像这样:
此代码(在新的通用模块中)
Option Explicit
Public Sub Copy3rdCol()
Const S1COL1 = 19 'Sheet1, col S
Const S1COL2 = 3 'Sheet1, col C
Const S1COL3 = 26 'Sheet1, col Z
Const S2COL1 = 1 'Sheet2, col A
Const S2COL2 = 3 'Sheet2, col C
Const S2COL3 = 4 'Sheet2, col D
Dim ws1 As Worksheet, ws2 As Worksheet, lr1 As Long, lr2 As Long
Set ws1 = Sheet1
Set ws2 = Sheet2
lr1 = ws1.Cells(Rows.Count, S1COL2).End(xlUp).Row 'last row on Sheet1, col C
lr2 = ws2.Cells(Rows.Count, S2COL2).End(xlUp).Row 'last row on Sheet2, col C
If lr1 > 2 And lr2 > 2 Then 'Row 1 is Header row on both sheets
Dim s1 As Variant, s2 As Variant, i As Long, j As Long
s1 = ws1.Range(ws1.Cells(1, 1), ws1.Cells(lr1, S1COL3))
s2 = ws2.Range(ws2.Cells(1, 1), ws2.Cells(lr2, S2COL3))
For i = 2 To lr1
For j = 2 To lr2
If s1(i, S1COL1) & s1(i, S1COL2) = s2(j, S2COL1) & s2(j, S2COL2) Then
s1(i, S1COL3) = s2(j, S2COL3)
End If
Next j
Next i
ws1.Range(ws1.Cells(1, 1), ws1.Cells(lr1, S1COL3)) = s1
End If
End Sub
将导致此(Sheet1)