查找内容并填写到指定列,VBA代码如何实现
如何通过VBA代码实现:工作表sheet1B列的内容是根据工作表sheet1A列的内容进行查找,如果sheet1B列的内容,已经有与sheet1A列相同的内容存在,那么s...
如何通过VBA代码实现:工作表sheet1 B列的内容是根据工作表sheet1 A 列的内容进行查找,如果sheet1 B列的内容,已经有与sheet1 A 列相同的内容存在,那么sheet1 B列不增加,如果没就在下一行追加名称进去。代码怎么写!!!
展开
4个回答
展开全部
代码及注释如下:
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
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
展开全部
'*** 在工作页面(例如: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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询