简体   繁体   English

Excel VBA-IF / AND问题-如果两列中的值在单独的工作表上匹配,则将一个工作表中的值复制到另一列中的值

[英]Excel VBA - IF/AND Issue - If values from two columns match on separate sheets, copy a value from one sheet to another in another column

I want sheets "Status" and "Ref" compared. 我想将“状态”表和“参考”表进行比较。 If the values match in both columns Status!F and Ref!B AND Status!Q and Ref!C for a particular individual's data in a row, then copy the value from Ref!F to Status!H 如果某行中某个人的数据的“状态” F和“参考” B 和“状态” Q和“参考” C列中的值均匹配,则将值从“参考” F复制到“状态”H。

This is my first ever attempt at writing code so it's probably full of errors, but the debugger points out the first If statement in particular. 这是我第一次尝试编写代码,因此可能充满了错误,但是调试器特别指出了第一个If语句。 I've tried it with and without parentheses. 我试过了,没有括号。

Sub help()

    Dim i As Long
    Dim j As Long
    Dim index As Integer


    Sheet1LastRow = Worksheets("Status").Range("F" & Rows.Count).End(xlUp).Row
    Sheet2LastRow = Worksheets("Ref").Range("B" & Rows.Count).End(xlUp).Row


    For index = 1 To Sheet1LastRow
        If ((Worksheets("Status").Cells(i, 6).Value = Worksheets("Ref").Cells(j, 2).Value) And (Worksheets("Status").Cells(i, 17).Value = Worksheets("Ref").Cells(j, 3).Value)) Then Worksheets("Status").Cells(i, 8).Value = Worksheets("Ref").Cells(j, 6)
    Next
    index = index + 1
    j = j + 1
    i = i + 1
    Do Until index = Sheet1LastRow
    Loop
End Sub

edit - a note--these sheets are not in the same order. 编辑-注释-这些工作表的顺序不同。 so row 1500 in Status could match something on row 3 on ref, for example 因此,例如,状态中的第1500行可以与参考第3行上的内容匹配

Try this 尝试这个

For i = 1 To Sheet1LastRow
    For j = 1 To Sheet2LastRow
        If ((Worksheets("Status").Cells(i, 6).Value = Worksheets("Ref").Cells(j, 2).Value) and (Worksheets("Status").Cells(i, 17).Value = Worksheets("Ref").Cells(j, 3).Value)) Then
            Worksheets("Status").Cells(i, 8).Value = Worksheets("Ref").Cells(j, 6).Value
            Exit for
        End if
    Next j
Next i

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个Excel工作表中的两列与另一工作表中的两列进行比较,如果匹配,则从另一列中复制数据 - Comparing two columns in one Excel sheet, to two columns in another sheet, and if they match, copy data from another column 如果其他两列的值匹配,则将值从一张工作表中的单元格复制到另一张工作表中最后使用的列中的单元格 - Copy value from a cell in one sheet to a cell in the last used Column in another sheet, if values in two other Columns' values match Excel VBA 根据第二列单元格值将列从一张表复制到另一张表 - Excel VBA to Copy Column from one sheet to another based on a second columns cell value 匹配不同工作表中的 2 列,并将第三列从一张工作表复制到另一张工作表中以匹配单元格 - Match 2 columns in different sheets and copy 3rd column from one sheet into another for matched cells 如果两列在单独的图纸中匹配,则将Sheet1中的插入单独的列值插入Sheet2中 - If Two Columns Match In Separate Sheets, The Insert Separate Column Value From Sheet1 Into Sheet2 将两列从一个Excel工作表复制到另一个 - Copy two columns from one Excel sheet to another 列内容匹配时,将值从一张纸复制到另一张纸 - Copy values from one sheet to another when column contents match Excel VBA将多列复制到另一张表到一列 - excel vba copy multiple columns to another sheet to one column Excel VBA-将两列从一个工作簿复制并粘贴到另一个工作簿 - Excel VBA - Copy and Paste two columns from one workbook into another 如何从一个工作表中复制列数据,然后将其复制到VBA Excel中的另一工作表 - How to copy column data from one sheet and then copy that to another sheet in vba excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM