简体   繁体   English

VBA进行故障排除,根据两个条件将一个工作簿中的一个单元格复制到另一个工作簿中的另一个单元格

[英]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.

相关问题 Excel VBA:将单元格值从一个工作簿复制到另一个? - excel vba: copy cell value from one workbook to another? 使用vba将单元格从一个工作簿复制到excel中的另一个工作簿 - Copying the cell from One workbook to another workbook in excel using vba VBA:查找文件并将单元格值从一个工作簿中的列复制到另一个根据条件? - VBA: find file and copy cell values from column in one workbook to another based on condition? 如何从另一个工作簿复制一系列单元格并将其粘贴到另一个工作簿? - How to copy a range of cell from another workbook and paste it to another one? 如何根据多个条件将单元格从一个工作簿复制到另一个工作簿 - How to copy cells from one workbook to another based on multiple criteria VBA根据某些用户输入的标准将数据从一个工作簿复制并粘贴到另一个工作簿? - VBA copy and paste data based on certain user inputed criteria from one workbook to another? 如何将单元格内容从一个工作簿复制到另一个工作簿? - How to copy cell content from one workbook to another? 如何在Excel VBA中将所有单元格颜色从一个工作簿复制到另一工作簿? - How to copy all cell colors from one workbook to another in Excel VBA? VBA:根据当前工作簿的单元格值从另一个工作簿复制数据 - VBA: Copy data from another workbook, based on cell value of current workbook 根据单元格数据将数据从一个工作簿复制到另一个工作簿 - Coping Data from One Workbook To Another Based On Cell Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM