[英]VBA troubleshoot, copy one cell in one workbook to another cell in another workbook based on two criteria
I have two workbooks, one which is my main overview and another which is a trimmed down copy of my main workbook, which a colleague uses to write notes based on his contacts with the people listed in our shared lists. 我有两本工作簿,一本是我的主要概述,另一本是我的主要工作簿的精简副本,一个同事用来根据与共享列表中所列人员的联系来写笔记。
I want to copy my colleagues notes into an appropriate column, by checking to see if the name and ID of the people are the same. 我想通过检查同事的姓名和ID是否相同,将同事的笔记复制到适当的列中。 So, I want to make sure the notes for Mike Smith, #12 get copied to his row and not the row for Mike Smith #77. 因此,我想确保将Mike Smith#12的笔记复制到他的行中,而不是Mike Smith#77的行中。
My thought was to do two loops, one that goes through each row of the main sheet and then for that particular main sheet row, loops through all the note sheet rows, looking for matches of ID and names, and when it finds a match for both, copies the notes in that row in the note sheet to the main sheet in the appropriate column. 我的想法是做两个循环,一个循环经过主表的每一行,然后针对该特定的主表行,循环遍历所有笔记表行,查找ID和名称的匹配项,并找到与之匹配的项。两者都将注释表中该行中的注释复制到相应列的主表中。
Here's what I have: 这是我所拥有的:
Private Sub CommandButton1_Click()
Dim jbBook As Workbook
Dim x As Integer
Dim i As Integer
Set jbBook = Workbooks.Open("C:\...\noteWorkbook.xslx")
For i = 2 To 200
For x = 2 To 200
If Cells(i, 5) = jbBook.Worksheets(1).Cells(x, 5) Then If Cells(i, 16) = jbBook.Worksheets(1).Cells(x, 16) Then Cells(i, 52) = jbBook.Worksheets(1).Cells(i, 34)
Next x
Next i
jbBook.Close
End Sub
I believe the issue is with this: 我相信问题在于:
If Cells(i, 5) = jbBook.Worksheets(1).Cells(x, 5) Then
Try changing it to 尝试将其更改为
If ActiveWorkbook.Cells(i, 5) = jbBook.Worksheets(1).Cells(x, 5) Then
I believe that the unreferenced Cells()
is what's giving the error. 我相信未引用的Cells()
导致了错误。 You would, of course, have to change all of them. 当然,您将必须更改所有这些。
You may also consider breaking the If/Thens onto separate lines, just for readability. 您也可以考虑将If / Thens分成几行,以提高可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.