【发布时间】:2020-09-10 04:48:10
【问题描述】:
我正在尝试复制和粘贴,使用循环、匹配和偏移。
在更新表中,我有一个表,用户将在其中更新值
A B C D E
1 PrimaryKey Line New Value 1 New Value 2 UpdateValue
2 XY.1 1 ABCD 1234 1
3 XY.2 2 ZXCV 9876 1
4
我需要做以下事情。
- 搜索 Sheet2 (Table1[PrimaryKey]) 并根据单元格 A2 值进行匹配
- 在工作表 1 的 C2 中复制值(例如 ABCD)
- 在 Lx 中粘贴.Values(其中 x =在步骤 1 中找到的行 id)
- 在表 1 中复制 CD 中的值(例如 1234)
- Ox 中的粘贴.Values(其中 x =在步骤 1 中找到的行 ID)
- 在 Wx 中粘贴 today()
- 循环搜索列 E = 1 的所有单元格
这是我找到的代码。我无法弄清楚循环和匹配组件。
'Set sheets
Set sh1 = Sheets("Data Capture")
Set sh2 = Sheets("DataTable")
'Find string in column A of Sheet2
Set foundCell = sh2.Range("Table5[PrimaryKey]").Find(sh1.Range("Z2").Value, , xlValues, xlWhole)
If Not foundCell Is Nothing Then 'If match cell is found
sh1.Range("B2").Copy
foundCell.PasteSpecial xlPasteValues
foundCell.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
Else
End If
End Sub
【问题讨论】:
-
这其中的哪个具体部分给您带来了问题?
-
全部。我找到了一些代码,但是我无法理解它以适应我的需要。
-
'Set sheets Set sh1 = Sheets("Data Capture") Set sh2 = Sheets("DataTable") 'Find string in column A of Sheet2 Set foundCell = sh2.Range("Table5[PrimaryKey]").Find(sh1.Range("Z2").Value, , xlValues, xlWhole) If Not foundCell Is Nothing Then 'If match cell is found sh1.Range("B2").Copy foundCell.PasteSpecial xlPasteValues foundCell.PasteSpecial xlPasteFormats Application.CutCopyMode = False Else End If End Sub -
这是我找到的代码,但是我可以找出循环和匹配组件。
-
当您弄清楚循环和匹配组件时,您所做的就是根据需要进行编辑。
标签: excel vba loops match paste