查找内容并填写到指定列,VBA代码如何实现

如何通过VBA代码实现:工作表sheet1 B列的内容是根据工作表sheet1 A 列的内容进行查找,如果sheet1 B列的内容,已经有与sheet1 A 列相同的内容存在,那么sheet1 B列不增加,如果没就在下一行追加名称进去。代码怎么写!!!

代码及注释如下:

Sub main()
Set dic = CreateObject("scripting.dictionary") '定义字典
x = Range("B65536").End(3).Row '将B列最大非空行号赋值给x
For i = 2 To x
dic(Cells(i, "B").Value) = "" '将B列全部放入字典中
Next i
For i = 2 To Range("A65536").End(3).Row '从A2到最后一个非空单元格循环
If dic.eixsts(Cells(i, "A").Value) = False Then '如果字典中不存在A列的值
x = x + 1 'x增加1
Cells(x, "B") = Cells(i, "A") '将A列的值赋予B列最后的单元格中
End If
Next i
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-01
'*** 在工作页面(例如:sheet1)内加个“命令按钮”其代码如下:
Private Sub CommandButton1_Click()
Macro1
End Sub
'****** 模块代码如下:
Sub Macro1()
For I = 1 To 65535
If Len(ActiveSheet.Cells(I, 2).Value) = 0 Then
Exit For
End If
Next I
I = I - 1
For j = 1 To 65535
If Len(ActiveSheet.Cells(j, 1).Value) = 0 Then
Exit For
End If
Next j
W = 0
For K = 1 To j - 1
N = 0
For H = 1 To I + W
If ActiveSheet.Cells(K, 1).Value = ActiveSheet.Cells(H, 2).Value Then
Exit For
Else
N = N + 1
End If
Next H
If N = I + W Then
W = W + 1
ActiveSheet.Cells(H, 2).Value = ActiveSheet.Cells(K, 1).Value
End If
Next K
End Sub
第2个回答  2012-03-31
'AB列最后一行行号
LA = Range("a65536").End(xlUp).Row
LB = Range("b65536").End(xlUp).Row
For I = 2 To LA
'获取A列当前单元格值
C = Cells(I, 1)
'用COUNTIF统计B列中C出现的次数,若为0则添加
If Application.WorksheetFunction.CountIf(Range(Cells(1, 2), Cells(LB, 2)), C) = 0 Then
LB = LB + 1
Cells(LB, 2) = C
End If
Next
第3个回答  2015-09-29
需求不明确
相似回答