在此代碼會發生什麼:如果發現匹配
- 從 「!小樣B4」
- 搜索通過的 「帳戶」
- 列A以搜索值,複製從「模擬」到「賬戶」(行)的數據,與提供的圖片相匹配。
- 如果沒有在「A」找到的匹配,通過「E」
- 再次搜索如果「E」找到的匹配,從「帳戶」(行),以「小樣」
複製數據
代碼:
Sub MockUpTranfer()
Dim lastRow As Long, lRow As Long
Dim source As String, target As String, tempVal As String
Dim match As Boolean
match = False
source = "Mock up"
target = "Accounts"
'Get last Row of target Sheet and temp value to search.
lastRow = Sheets(target).Range("A" & Rows.count).End(xlUp).row
tempVal = Sheets(source).Range("B4")
'Check the search value against Column A on "Accounts"
For lRow = 1 To lastRow
'Copy from MockUp to Accounts
If Sheets(target).Cells(lRow, "A") = tempVal Then
Sheets(target).Cells(lRow, "B") = Sheets(source).Range("B10")
Sheets(target).Cells(lRow, "C") = Sheets(source).Range("B8")
Sheets(target).Cells(lRow, "D") = Sheets(source).Range("B9")
Sheets(target).Cells(lRow, "E") = Sheets(source).Range("B19")
Sheets(target).Cells(lRow, "F") = Sheets(source).Range("B18")
Sheets(target).Cells(lRow, "G") = Sheets(source).Range("B17")
Sheets(target).Cells(lRow, "H") = Sheets(source).Range("B16")
Sheets(target).Cells(lRow, "I") = Sheets(source).Range("B22")
Sheets(target).Cells(lRow, "J") = Sheets(source).Range("B23")
Sheets(target).Cells(lRow, "K") = Sheets(source).Range("B24")
Sheets(target).Cells(lRow, "L") = Sheets(source).Range("B25")
match = True
End If
Next lRow
'No match found in "A", now searching "E"
If match = False Then
For lRow = 1 To lastRow
'Copy from Accounts to MockUp
If Sheets(target).Cells(lRow, "E") = tempVal Then
Sheets(source).Range("B10") = Sheets(target).Cells(lRow, "B")
Sheets(source).Range("B8") = Sheets(target).Cells(lRow, "C")
Sheets(source).Range("B9") = Sheets(target).Cells(lRow, "D")
Sheets(source).Range("B19") = Sheets(target).Cells(lRow, "E")
Sheets(source).Range("B18") = Sheets(target).Cells(lRow, "F")
Sheets(source).Range("B17") = Sheets(target).Cells(lRow, "G")
Sheets(source).Range("B16") = Sheets(target).Cells(lRow, "H")
Sheets(source).Range("B22") = Sheets(target).Cells(lRow, "I")
Sheets(source).Range("B23") = Sheets(target).Cells(lRow, "J")
Sheets(source).Range("B24") = Sheets(target).Cells(lRow, "K")
Sheets(source).Range("B25") = Sheets(target).Cells(lRow, "L")
End If
Next lRow
End If
End Sub
注:如果不止一個匹配存在,最後一個將覆蓋第一。這將遍歷整個帳戶表。
發佈迄今爲止的代碼並準確指出您遇到問題的位置會很有用。 –
我同意蒂姆。但是你可能想要檢查[* Range Object ** Find ** Method *](http://msdn.microsoft.com/en-us/library/office/ff839746(v = office.15).aspx)來完成這個。或者你也可以嘗試古典循環。 – L42