您的位置:程序门 -> vb -> vba



运行错误“13”,类型不匹配??


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


运行错误“13”,类型不匹配??
发表于:2008-01-12 14:49:44 楼主
大家好:
      如果想要从一列有空格的名单中(假设b列)筛选出重复的名单,并将其放入另外一列中(假设m列),我编写了一段程序,可老出现运行错误“13”,类型不匹配,恳请各位帮忙解决下!不甚感激!
sub   jszq()
dim   myrow1   as   integer
dim   x   as   integer,   y   as   integer,   n   as   integer
dim   aa,   bb,   cc
n   =   1
cells(1,   13)   =   0
range("b1").select
myrow1   =   [b65536].end(xlup).row
for   y   =   2   to   myrow1
        bb   =   0
        cc   =   0
        aa   =   0
        aa   =   cells(y,   2)
        bb   =   application.worksheetfunction.search(aa,   range("b2:b"   &   y   -   1))   '看现在的单元格是否在前几行出现过
        cc   =   application.worksheetfunction.search(aa,   range("m1:m"   &   n))         '看现在的单元格是否已被列入m列
        if   aa   =   ""   or   cc   >   0   then     '若出现无名单的单元格或者已被列入名单的名单,跳过
        goto   100
        elseif   bb   >   0   then
        cells(n,   13)   =   cells(y,   2)     '否则若在前几行出现过写入m列
        n   =   n   +   1
        else
        end   if
       
100:   next   y
        end   sub
发表于:2008-01-12 15:52:481楼 得分:0
myrow1   =[b65536].end(xlup).row   解释一下这一行是什么意思?
在vb里,可以直接这样使用吗?
问题很有可能出现在以下两行上:
bb=application.worksheetfunction.search(aa,range("b2:b"   &   y   -   1))   '看现在的单元格是否在前几行出现过  
cc=application.worksheetfunction.search(aa,range("m1:m"   &   n))           '看现在的单元格是否已被列入m列  
发表于:2008-01-13 10:41:202楼 得分:0
太费劲,用这方法试试
sub   jszq()
        dim   myrow1   as   integer,   ddd
        myrow1   =   [b65536].end(xlup).row
        with   range("m1:m"   &   myrow1)
                .formular1c1   =   "=if(countif(r1c2:rc[-11],rc[-11])=2,rc[-11],"""")"
                ddd   =   .value:     .value   =   ddd
                .sort   key1:=range("m1")
        end   with
end   sub
发表于:2008-01-13 10:45:203楼 得分:0
myrow1也不要了
sub   jszq()
        dim   ddd()
        with   range("m1:m"   &   [b65536].end(xlup).row)
                .formular1c1   =   "=if(countif(r1c2:rc[-11],rc[-11])=2,rc[-11],"""")"
                ddd   =   .value:     .value   =   ddd
                .sort   key1:=range("m1")
        end   with
end   sub
发表于:2008-01-13 13:12:084楼 得分:0
结果不对啊,我运行过了。还有能解释下.formular1c1       =       "=if(countif(r1c2:rc[-11],rc[-11])=2,rc[-11],"""")"   吗?我是刚在学习vb,还请指教
发表于:2008-01-13 13:16:215楼 得分:0
myrow1       =[b65536].end(xlup).row     这一行是计算带有空格的指定列的行数(包括空格)
发表于:2008-01-13 13:17:346楼 得分:0
我还想问一下如果按照我的方法,怎么改才算对呢?
发表于:2008-01-13 14:08:267楼 得分:0
单步调试,查找是在什么地方出错.
发表于:2008-01-13 14:18:358楼 得分:0
.formular1c1   =   "=if(countif(r1c2:rc[-11],rc[-11])=2,rc[-11],"""")"  

就是给单元格输入公式,即
m1单元格=if(countif($b$1:b1,b1)=2,b1,"")
m2单元格=if(countif($b$1:b2,b2)=2,b2,"")
m3单元格=if(countif($b$1:b3,b3)=2,b3,"")
.
.
.
具体思路:
假定当前为第8行
1.在m8中输入公式,统计b8的值在   b1:b8   中的个数,如果是2   ,说明有两个重复,取出b8,放在m8;如果为0   或   1,说明没有重复,m8="";如果> 2,说明有两个以上的重复值,也就说明前面已经去过,可不用再取,m8="".这样在m列中就得到只有重复值得b列的值,对m列排序,重复的值就到了上边,完成了你的要求。

如:
b列             m列公式                                                                             m列得到的结果                     m列排序后结果
  b1:a       =if(countif($b$1:b1,b1)=2,b1,"")                                                         a
  b2:b       =if(countif($b$1:b2,b2)=2,b2,"")                                                         b
  b3:         =if(countif($b$1:b3,b3)=2,b3,"")                                                         c
  b4:d       =if(countif($b$1:b4,b4)=2,b4,"")
  b5:c       =if(countif($b$1:b5,b5)=2,b5,"")
  b6:b       =if(countif($b$1:b6,b6)=2,b6,"")                 b
  b7:         =if(countif($b$1:b7,b7)=2,b7,"")
  b8:e       =if(countif($b$1:b8,b8)=2,b8,"")
  b9:a       =if(countif($b$1:b9,b9)=2,b9,"")                 a
b10:a       =if(countif($b$1:b10,b10)=2,b10,"")
b11:c       =if(countif($b$1:b11,b11)=2,b11,"")           c

我测试过,没问题


sub   jszq()  
        dim   ddd()  
        with   range("m1:m"   &   [b65536].end(xlup).row)  
                '输入公式
                .formular1c1   =   "=if(countif(r1c2:rc[-11],rc[-11])=2,rc[-11],"""")"  
                '将公式的计算结果转为值,相当于"复制-选择性粘贴-数值"  
                ddd   =   .value:     .value   =   ddd  
                '对m列排序
                .sort       key1:=range("m1")  
        end       with  
end   sub  
发表于:2008-01-13 14:46:409楼 得分:0
按你的方法改的,数据量多的时候效率不如我上面的高
sub   jszq()
dim   y   as   integer,   n   as   integer
dim   bb   as   long  
n   =   1
for   y   =   2   to   [b65536].end(xlup).row
        bb   =   application.worksheetfunction.countif(range("b1:b"   &   y),   cells(y,   2))   '看现在的单元格是否在前几行出现过
        if   bb   =   2   then
              cells(n,   13)   =   cells(y,   2)   '否则若在前几行出现过一次写入m列,多于一次都不写
                n   =   n   +   1
        end   if
next   y
end   sub
发表于:2008-01-13 17:06:0710楼 得分:0
sub   jszq()
for       y       =       2       to       [b65536].end(xlup).row  
        if   application.worksheetfunction.countif(range("b1:b"       &       y),       cells(y,       2))   > 1   then     '看现在的单元格是否在前几行出现过  
                cells([m65536].end(xlup).row+1,13)   =   cells(y,       2)
        end       if  
next       y  
end   sub
发表于:2008-01-13 17:27:3011楼 得分:0
gdtopone:
别乱改,if   条件不能> 1,如有3个,   3> 1,再写入m列,m列又重复了
[m65536].end(xlup).row+1   如果开始时m1没内容,那么地一个重复值就会写入第二行,你好好分析分析再改。
发表于:2008-01-13 17:29:3112楼 得分:0
gdtopone:  
别乱改,if   条件不能>   1,如有3个,3>   1,再写入m列,m列又重复了  
[m65536].end(xlup).row+1       如果开始时m1没内容,那么第一个重复值就会写入第二行,你好好分析分析再改。
发表于:2008-01-14 14:10:1313楼 得分:0
如果要在之前的基础上,将重复的名单用红色的标出来,我又写了一段程序,可结果只是空格下的第二行填充了红色,怎么找错误也找不出来,各位在帮下忙吧
sub   第二步()
dim   my1   as   integer,   my2   as   integer
dim   m%,   n%
dim   aa
range("b1").select
my1   =   [b65536].end(xlup).row
range("k1").select
my2   =   selection.currentregion.rows.count
for   n   =   1   to   my1
        if   cells(n,   2)   =   ""   or   cells(n,   2)   =   "姓名"   then
        goto   100
          aa   =   application.worksheetfunction.countif(range("k1:k"   &   my2),   cells(n,   2))‘在已列出的重复名单中看有没有现在要查找的名字
          elseif   aa   =   1   then        
        cells(n,   2).interior.colorindex   =   3     '若有则填充红色
        end   if
        aa   =   0
100:   next   n
end   sub

另外一种方法和筛选重复名单方法类似,但是第一次出现的重复名单却不能填充
sub   第一步2()
dim   y           as   integer,   n               as   integer
dim   bb           as   long
n   =   1
for   y   =   2   to   [b65536].end(xlup).row
                bb   =   application.worksheetfunction.countif(range("b1:b"   &   y   -   1),   cells(y,   2))           '看现在的单元格是否在前几行出现过
                if   bb   > =   1   then
                          cells(y,   2).interior.colorindex   =   3            
                                n   =   n   +   1
                end   if
next   y
end   sub
发表于:2008-01-14 16:08:1314楼 得分:0
你的程序我看不懂了:
for   n   =   1   to   my1  
        if   cells(n,   2)     =   ""   or   cells(n,   2)   =   "姓名"   then  
                goto   100  
                aa   =   application.worksheetfunction.countif(range("k1:k"   &   my2),   cells(n,   2))‘在已列出的重复名单中看有没有现在要查找的名字  
        elseif   aa   =   1   then      
                cells(n,   2).interior.colorindex   =   3       '若有则填充红色  
        end   if  
        aa   =   0  
100:   next   n  
end   sub  
红色的goto   100若放在这里,那么countif就永远不会执行,aa什么时候能=1呢?


做如下改动:
sub   第一步2()
dim   y   as   integer,   n   as   integer
dim   bb   as   long
for   y   =   2   to   [b65536].end(xlup).row
        bb   =   application.worksheetfunction.countif(range("b1:b"   &   y   -   1),   cells(y,   2))
        if   bb   > =   1   then
                cells(y,   2).interior.colorindex   =   3
                n   =   application.worksheetfunction.match(cells(y,   2),   range("b1:b"   &   y   -   1),   0)
                cells(n,   2).interior.colorindex   =   3
        end   if
next   y
end   sub
如果重复次数不是很多,可用此方法,次数太多则程序有些累赘,因为每次都给第一次的重复值设置红色。
祝你早日完成。
发表于:2008-01-17 10:12:5115楼 得分:0
怎样了?
发表于:2008-01-17 15:02:4916楼 得分:0
改用find函数吧


快速检索

最新资讯
热门点击