excel中用vba自定义函数问题
我相将附件中第一列姓名和第三列比较,如果在第三列中有和第一列相同姓名的在第二列上注“有”。我想用函数来解决,这样可以用鼠标选定数据源灵活些。我的代码是 Function 比较(dy As Range, dr As Range, w As Integer) w = 0 Dim a1 As Integer, a2 As Integer, b1 As Integer, b2 As Integer a1 = dy.Cells(1).Row a2 = dy.Cells(dy.Cells.Count).Row b1 = dr.Cells(1).Row b2 = dr.Cells(dr.Cells.Count).Row Dim i As Integer, j As Integer For i = a1 To a2 For j = b1 To b2 If Cells(i, 1) = Cells(j, 3) Then Cells(i, 2) = "有" Next j Next i 比较 = b2 End Function 执行时显示#value,我分析是循环执行不了,请教哪位高手帮我完善一下,或修改一下不足,多谢,急
诚如楼上所述,这个功能用Excel内置的函数解决起来更方便。不过我想楼主可能是想借此学习VBA,给出几点拙见: 1、养成良好的编程习惯 这个可能体现在很多方面,最基础的要数命名了,变量的命名、过程和函数的命名等。等程序一复杂,如果胡乱命名会带来很多麻烦,尤其是做维护的时候; 2、关注程序的执行效率 循环是编程时常用的一个语句,不过某些指令不用循环也能实现的话,应该不用循环,毕竟循环的执行效率差多了。
3、写自定义函数的注意事项 自定义函数是在系统内置函数解决不了问题时的替代品,它的执行效率是不如内置函数的。当自定义函数定义得越复杂时,需要的计算过程也越多,计算时响应的速度也会慢下来。所以有必要定义一下自定义函数的易失性,如定义成非易失性函数的代码是—— Application。
Volatile (False) 下面给出比循环更快的Find方法: Option Explicit Function CheckExist(rngSource As Range, rngCompare As Range) As String Dim c As Range Application。
Volatile (False) '定义函数为非易失性函数 Set c = rngSource。Find(rngCompare, LookIn:=xlValues) If Not c Is Nothing Then CheckExist = "有" End Function 补充: 在你的代码基础上改,你比较一下跟你的有何不同就明白为什么你的得不出正确结果了。
在B2里输入公式:=比较(A2,$C$3:$C$21),其余下拉。 Option Explicit Function 比较(dy As Range, dr As Range) As String On Error GoTo 0 Dim a1 As Integer, a2 As Integer, b1 As Integer, b2 As Integer a1 = dy。
Cells(1)。Row a2 = dy。Cells(dy。Cells。Count)。Row b1 = dr。Cells(1)。Row b2 = dr。Cells(dr。Cells。Count)。
Row Dim i As Integer, j As Integer For i = a1 To a2 For j = b1 To b2 If Cells(i, 1) = Cells(j, 3) Then 比较 = 比较 & "有" Next j Next i End Function。
Function bijiao(a As Range, b As Range) mark = 0 For Each r In b If a.Value = r.Value Then mark = 1 End If Next bijiao = IIf(mark = 1, "有", "") End Function 其实这个问题,用VLOOKUP()函数应该可以解决,不一定要用到VBA,详情请见附件~~~
答:操作说明: 1、选中A1。 2、在“插入/名称/定义/在当前工作簿的名称/输入任一名字如‘求值’/引用位置/输入‘=EVALUATE(Sheet1!A65536...详情>>
问:怎样用Camtasia Studio v2.0.3来制作媒体
答:这个软件有使用向导的,你新建一个文件,然后照着他的说明一步一步的走下去,就知道怎么用了,这个软件太小了,网上跟本找不到教程的,如果你确实还是不会的话,找个懂电脑...详情>>