我有excel文件,填充了两列。首先,包括数字,第二个字母。我希望用条件填写第三列: 如果相同的号码有" A"在第二列的任何单元格中,在第三列中填写该数字的每个单元格的字母A. ELSEIF" B"那么B在第三栏...... 优先级A> B> C> D
答案 0 :(得分:0)
如果您可以使用公式代替VBA,则可以执行以下操作:
=IF(COUNTIFS(A:A,A2,B:B,"A")>0,"A",IF(COUNTIFS(A:A,A2,B:B,"B")>0,"B",IF(COUNTIFS(A:A,A2,B:B,"C")>0,"C","D")))
在这个公式中COUNTIF
函数正在组合2个标准并计算这些标准是否符合,然后IF
函数正在向单元格输入相关的字母。
答案 1 :(得分:0)
使用此
Sub test()
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Dim Cl As Range, i&
i = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For Each Cl In ActiveSheet.Range("A1:A" & i)
If Not Dic.exists(Cl.Value & Cl.Offset(, 1).Value) Then
Dic.Add (Cl.Value & Cl.Offset(, 1).Value), Cl.Row
End If
Next
For Each Cl In ActiveSheet.Range("A1:A" & i)
If Dic.exists(Cl.Value & "A") Then
Cl.Offset(, 2).Value = "A"
ElseIf Dic.exists(Cl.Value & "B") Then
Cl.Offset(, 2).Value = "B"
ElseIf Dic.exists(Cl.Value & "C") Then
Cl.Offset(, 2).Value = "C"
ElseIf Dic.exists(Cl.Value & "D") Then
Cl.Offset(, 2).Value = "D"
End If
Next
End Sub
输出结果是
使用此
Sub test()
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Dim Cl As Range, i&, key As Variant
i = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For Each Cl In ActiveSheet.Range("A1:A" & i)
If Not Dic.exists(Cl.Value & Cl.Offset(, 1).Value) Then
Dic.Add (Cl.Value & Cl.Offset(, 1).Value), Cl.Row
End If
Next
For Each Cl In ActiveSheet.Range("A1:A" & i)
For Each key In Dic
If UCase(key) Like Cl.Value & "*A*" Then
Cl.Offset(, 2).Value = Mid(key, 2, 100)
Exit For
End If
Next
If Cl.Offset(, 2).Value = Empty Then
For Each key In Dic
If UCase(key) Like Cl.Value & "*B*" Then
Cl.Offset(, 2).Value = Mid(key, 2, 100)
Exit For
End If
Next
End If
If Cl.Offset(, 2).Value = Empty Then
For Each key In Dic
If UCase(key) Like Cl.Value & "*C*" Then
Cl.Offset(, 2).Value = Mid(key, 2, 100)
Exit For
End If
Next
End If
If Cl.Offset(, 2).Value = Empty Then
For Each key In Dic
If UCase(key) Like Cl.Value & "*D*" Then
Cl.Offset(, 2).Value = Mid(key, 2, 100)
Exit For
End If
Next
End If
Next
End Sub
输出结果