[英]Excel VBA Macro to copy data between sheets when value appears in column then loops to next value
I have some experience of writing macros but this area is new to me.我有一些编写宏的经验,但这个领域对我来说是新的。 I have been working on a new project and have most parts of it working but I can't seem to get this part to work.
我一直在研究一个新项目,并且它的大部分内容都在工作,但我似乎无法让这部分工作。 I have looked at lookup and match range but none seem to work properly.
我已经查看了查找和匹配范围,但似乎都无法正常工作。
What I am trying to do is this.我正在尝试做的是这个。
Many thanks & I hope this makes sense非常感谢 & 我希望这是有道理的
This was my attempt:这是我的尝试:
Sub Match()
On Error Resume Next
Dim ID_upn As String
Dim ID_Row As Long
Dim ID_Clm As Long
Table1 = Sheets("Projectx").Range("A2:A200") ' ID Column from Project sheet table
Table2 = Sheets("Master").Range("A2:P200") ' Master Table
ID_Row = Sheets("Projectx").Range("A2").row ' C2 is the first cell to start checking
ID_Clm = Sheets("Master").Range("A2").Column
For Each cl In Table1
Sheets("Projectx").Cells(ID_Row, ID_Clm) = Application.WorksheetFunction.if(cl, Table2, 3, False)
ID_Row = ID_Row + 1
Next cl
MsgBox "Done"
End SubSub Match()
Here is how I would do it:这是我将如何做到的:
Sub getFromMaster()
Dim row as Range, wb as Workbook, masterSheet as Worksheet, idSheet as Worksheet
Dim idVal as String, masterRow as Range, masterVal as String
Set wb = ActiveWorkbook
Set masterSheet = wb.sheets("Master")
Set idSheet = wb.sheets("Projectx")
For each row in idSheet.usedrange.rows
idVal = row.cells(1).value
For each masterRow in masterSheet.usedrange.rows
masterVal = masterRow.cells(1).value
If masterVal = idVal then
masterRow.copy row
Exit For
End if
next 'masterRow
next 'row
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.