0

我在论坛中看到了类似的(几乎相同的问题)。这是选择的答案。

代码:

Sub fixThis()
    Dim i As Long, j As Long, col1 As Long, col2 As Long, lastrow1 As Long, lastrow2 As Long
    Dim sheetOne As String
    Dim sheetTwo As String

    col1 = 5
    col2 = 1
    sheetOne = "Names"
    sheetTwo = "Job"
    lastrow1 = Sheets(sheetOne).Cells(Sheets(sheetOne).Rows.Count, col1).End(xlUp).Row
    lastrow2 = Sheets(sheetTwo).Cells(Sheets(sheetTwo).Rows.Count, col2).End(xlUp).Row

    For i = 2 To lastrow1
        For j = 2 To lastrow2
            If Sheets(sheetOne).Cells(i, col1).Value = Sheets(sheetTwo).Cells(j, col2).Value Then
                Sheets(sheetOne).Cells(i, 6).Value = Sheets(sheetTwo).Cells(j, 2).Value
            End If
        Next j
    Next i
End Sub

但我不知道如何把它变成我的案例的答案。

我有 2 张名称添加和删除的工作表。我想比较两张纸的第一列。如果两个工作表中的值都匹配,我想将状态列的值从工作表“添加”复制到工作表“删除”以获取匹配的值。

请帮我解决一下这个。

Ps:我是编码的初学者。

4

2 回答 2

0

不需要VBA。这可以使用 Vlookup 来实现。请参考下图。

在此处输入图像描述

于 2013-04-19T04:09:16.137 回答
0
Sub fixThis()
    Dim i As Long, j As Long, colStatus As Long, lastrowAdd As Long, lastrowRemove As Long

    colStatus = 2 'your status column number
    lastrowAdd = Sheets("Add").Cells(Sheets("Add").Rows.Count, 1).End(xlUp).Row
    lastrowRemove = Sheets("Remove").Cells(Sheets("Remove").Rows.Count, 1).End(xlUp).Row

    For i = 1 To lastrowAdd 
        For j = 1 To lastrowRemove 
            If Sheets("Add").Cells(i, 1).Value = Sheets("Remove").Cells(j, 1).Value Then
                Sheets("Remove").Cells(j, colStatus).Value = Sheets("Add").Cells(i, colStatus).Value
            End If
        Next j
    Next i
End Sub
于 2013-04-19T04:16:18.717 回答