Excel VBA复制匹配条件单元格并根据不同工作表中的条件粘贴到特定单元格中

时间:2018-07-12 05:49:35

标签: excel vba

我正在尝试在单元格中搜索特定文本并将其复制到新创建的列表中。我需要比较这些值,并且从未使用过VBA。下面是搜索和复制值的条件的示例。

Oldparam sheet Newparam sheet

在Newparam工作表中,根据以下示例,需要用OldParam工作表中的L列中的匹配文本填充L列;

对于名称为20M408和20M408_1的情况,在NewParam中,如果F列= st1CLOSE,则仅在G列= OFFNRM时,从OldParam列L复制文本。 如果名称和带有OFFNRM的st1CLOSE之间没有匹配项,则NewParam列L中将填充N / A。 对于20M409重复以上操作,依此类推。

谢谢。

1 个答案:

答案 0 :(得分:0)

我尝试下面的代码,但是找到匹配项后,我无法获得匹配的行号(对于K =)。

Sub match()
 Dim s1 As Worksheet, s2 As Worksheet
 Dim I As Long, J As Long, K As Long, L As Long, M As Long, NewName As String, NewValue As String
  
 Set s1 = Sheets("Report")
 Set s2 = Sheets("Sheet1")
 K = 1
 M = 1
 I = s1.Cells(Rows.Count, "A").End(xlUp).Row
 L = s2.Cells(Rows.Count, "A").End(xlUp).Row

 For M = 2 To L
 
    If s2.Cells(M, "F").Value = "st1 CLOSE" Then
        NewName = s2.Cells(M, "B").Value
                  
        For K = 2 To I
        
            If s1.Cells(K, "K").Value = "OFFNRMPR" And s1.Cells(K, "A").Value = NewName Then
                s1.Cells(K, "L").Copy s2.Cells(M, "L").Value
                Exit For
            Else: s2.Cells(M, "L").Value = "N/A"
                Exit For
            End If
        Next
    End If
 Next
End Sub