Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2449|回复: 9

[已解决]我想在这个EXCEL VBA中多加一个条件,可以做到吗?

[复制链接]
发表于 2014-1-3 00:44 | 显示全部楼层 |阅读模式
具体在EXCEL里面说明了,麻烦大神们了。
最佳答案
2014-1-3 14:11
改的,看成不:
Sub kkk()
    Dim d As Object, arr As Variant, i&, j&, a As Variant, theStr$
    Set d = CreateObject("Scripting.Dictionary")
    With Sheet1
        .Cells(1).CurrentRegion.Interior.ColorIndex = xlNone
        .Columns(5).Clear
        MsgBox "单击以继续……", vbInformation, "提示"
        arr = .Cells(1).CurrentRegion
        For i = 2 To UBound(arr)
            theStr = arr(i, 2)
            If theStr <> "" Then
                If InStr(arr(i, 2), "/") Then
                  a = Split(theStr & "/", "/")
                  a(UBound(a)) = theStr
                Else
                  a = Split(theStr, "/")
                End If
                For j = UBound(a) To 0 Step -1
                    a(j) = a(j) & arr(i, 4)
                    If d.Exists(a(j)) Then
                        .Cells(d(a(j)), 5) = "相同"
                        .Range(.Cells(d(a(j)), 4), .Cells(d(a(j)), 5)).Interior.ColorIndex = 6
                        .Cells(d(a(j)), 2).Interior.ColorIndex = 6
                        .Cells(i, 5) = "相同"
                        .Range(.Cells(i, 4), .Cells(i, 5)).Interior.ColorIndex = 6
                        .Cells(i, 2).Interior.ColorIndex = 6
                    Else
                       d(a(j)) = i
                    End If
                Next j
            End If
        Next i
    End With
    Set d = Nothing
End Sub
另:18行不是与5行合规则吗?为什么不相同呢?

筛选同批号.rar

10.09 KB, 下载次数: 25

发表于 2014-1-3 09:33 | 显示全部楼层
  1. Sub kkk()
  2.     Dim d As Object, arr As Variant, i&, j&, a As Variant, theStr$, theStr1$, theStr2$
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     With Sheet1
  5.         .Cells.Interior.ColorIndex = xlNone
  6.         .Columns(5).Clear
  7.         MsgBox "单击以继续……", vbInformation, "提示"
  8.         arr = .Cells(1).CurrentRegion
  9.         For i = 2 To UBound(arr)
  10.             theStr = arr(i, 2)
  11.             If theStr <> "" Then
  12.                 a = Split(theStr, "/")
  13.                 If UBound(a) = 0 Then
  14.                     d(a(0)) = i
  15.                 End If
  16.                 theStr = theStr & arr(i, 4)
  17.                 If Not d.Exists(theStr) Then
  18.                     d(theStr) = i
  19.                     theStr = ""
  20.                     For j = UBound(a) To 0 Step -1
  21.                         If theStr = "" Then
  22.                             theStr = a(j)
  23.                         Else
  24.                             theStr = theStr & "/" & a(j)
  25.                             theStr = theStr & arr(i, 4)
  26.                             If Not d.Exists(theStr) Then
  27.                                 d(theStr) = i
  28.                                 
  29.                             Else
  30.                                 .Cells(d(theStr), 5) = "相同"
  31.                                 .Range(.Cells(d(theStr), 4), .Cells(d(theStr), 5)).Interior.ColorIndex = 6
  32.                                 .Cells(d(theStr), 2).Interior.ColorIndex = 6
  33.                                 .Cells(i, 5) = "相同"
  34.                                 .Range(.Cells(i, 4), .Cells(i, 5)).Interior.ColorIndex = 6
  35.                                 .Cells(i, 2).Interior.ColorIndex = 6
  36.                             End If
  37.                         End If
  38.                     Next j
  39.                 Else
  40.                     .Cells(d(theStr), 5) = "相同"
  41.                     .Range(.Cells(d(theStr), 4), .Cells(d(theStr), 5)).Interior.ColorIndex = 6
  42.                     .Cells(d(theStr), 2).Interior.ColorIndex = 6
  43.                     .Cells(i, 5) = "相同"
  44.                     .Range(.Cells(i, 4), .Cells(i, 5)).Interior.ColorIndex = 6
  45.                     .Cells(i, 2).Interior.ColorIndex = 6
  46.                 End If
  47.             End If
  48.         Next i
  49.     End With
  50.     Set d = Nothing
  51. End Sub
复制代码
回复

使用道具 举报

发表于 2014-1-3 09:34 | 显示全部楼层
就在   a = Split(theStr, "/") 下面加了三行而已。
      If UBound(a) = 0 Then
             d(a(0)) = i
      End If
回复

使用道具 举报

 楼主| 发表于 2014-1-3 13:38 | 显示全部楼层
hwc2ycy 发表于 2014-1-3 09:34
就在   a = Split(theStr, "/") 下面加了三行而已。
      If UBound(a) = 0 Then
             d(a(0))  ...

不行啊,还是和之前的一样
回复

使用道具 举报

发表于 2014-1-3 13:44 | 显示全部楼层
颜色没变么?
QQ截图20140103134720.jpg


QQ截图20140103134656.jpg
回复

使用道具 举报

发表于 2014-1-3 13:45 | 显示全部楼层
对于你现有的附件,测试效果是对的。
回复

使用道具 举报

发表于 2014-1-3 14:11 | 显示全部楼层    本楼为最佳答案   
改的,看成不:
Sub kkk()
    Dim d As Object, arr As Variant, i&, j&, a As Variant, theStr$
    Set d = CreateObject("Scripting.Dictionary")
    With Sheet1
        .Cells(1).CurrentRegion.Interior.ColorIndex = xlNone
        .Columns(5).Clear
        MsgBox "单击以继续……", vbInformation, "提示"
        arr = .Cells(1).CurrentRegion
        For i = 2 To UBound(arr)
            theStr = arr(i, 2)
            If theStr <> "" Then
                If InStr(arr(i, 2), "/") Then
                  a = Split(theStr & "/", "/")
                  a(UBound(a)) = theStr
                Else
                  a = Split(theStr, "/")
                End If
                For j = UBound(a) To 0 Step -1
                    a(j) = a(j) & arr(i, 4)
                    If d.Exists(a(j)) Then
                        .Cells(d(a(j)), 5) = "相同"
                        .Range(.Cells(d(a(j)), 4), .Cells(d(a(j)), 5)).Interior.ColorIndex = 6
                        .Cells(d(a(j)), 2).Interior.ColorIndex = 6
                        .Cells(i, 5) = "相同"
                        .Range(.Cells(i, 4), .Cells(i, 5)).Interior.ColorIndex = 6
                        .Cells(i, 2).Interior.ColorIndex = 6
                    Else
                       d(a(j)) = i
                    End If
                Next j
            End If
        Next i
    End With
    Set d = Nothing
End Sub
另:18行不是与5行合规则吗?为什么不相同呢?
回复

使用道具 举报

 楼主| 发表于 2014-1-3 15:11 | 显示全部楼层
hwc2ycy 发表于 2014-1-3 13:44
颜色没变么?

不是啊,11行和18行都有YC152,但后面都没有显示“相同”并变黄色底啊。
回复

使用道具 举报

 楼主| 发表于 2014-1-3 15:26 | 显示全部楼层
青城山苦丁茶 发表于 2014-1-3 14:11
改的,看成不:
Sub kkk()
    Dim d As Object, arr As Variant, i&, j&, a As Variant, theStr$

你的好像可以啊
回复

使用道具 举报

发表于 2014-1-3 16:12 | 显示全部楼层
JOB007 发表于 2014-1-3 15:11
不是啊,11行和18行都有YC152,但后面都没有显示“相同”并变黄色底啊。

11行和18行的没仔细看

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-6 12:29 , Processed in 0.286126 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表