VBA Excel自定义函数的使用 简单的语法

一个简单的教程,实现VBA自定义函数。

新建模块

复制后面的代码放进来

函数的入口参数不定义,则认为是一块区域;

反之,如FindChar1 As String,则认为是输入的单值。

循环和分支如下例子,VB比较接近自然语言,英语有功底的话,写起来还是比较舒服的。

vbnet 复制代码
Function xiu_xi(FindArea)
   Dim flag As String
   flag = " "
   Dim N2 As Integer
   N2 = 0
   For i = 2 To FindArea.Columns.Count
       If Trim(FindArea.Cells(1, i)) = "" And Trim(FindArea.Cells(1, i - 1)) = "" Then
          N2 = N2 + 1
          If N2 >= 6 Then
             flag = "X"
             Exit For
          End If
       Else
          N2 = 0
       End If
   Next i
   xiu_xi = flag
End Function

以下实现的是一个双条件的查找,用VLOOKUP则需要先连接起来

vbnet 复制代码
Function TQ_MultiVLookup(FindChar1 As String, FindChar2 As String, FindArea)
   Dim n As Integer
   n = FindArea.Columns.Count
   For i = 1 To FindArea.Rows.Count
       If FindChar1 = FindArea.Cells(i, 1) And FindChar2 = FindArea.Cells(i, 2) Then
          TQ_MultiVLookup = FindArea.Cells(i, n)
          Exit For
       End If
   Next i
End Function

回到Excel的页面,使用函数时,会有刚才自己加的函数。

相关推荐
CircleMouse6 小时前
如何设置wps单元格下拉选项设置
excel·wps
zhangjin122211 小时前
kettle插件-excel插件,kettle读取excel动态表头,kettle根据列名读取excel
excel·kettle·kettle excel插件·kettle 动态excel
远洪1 天前
excel 找出两列不同的数据
excel
pcplayer1 天前
非常好用的 Excel 读写控件
excel·delphi·office
Navicat中国1 天前
使用 Navicat 导入向导导入 Excel 数据时,系统提示导入成功,表中也能看到数据,但行数统计显示为 0,这是什么原因?
数据库·excel·导入
穿着内裤的外星人1 天前
触控精灵远程读写Excel步骤配置
excel
是孑然呀2 天前
【小记】excel vlookup一对多(第二篇)
excel
开开心心就好2 天前
专为视障人士设计的免费辅助工具
windows·计算机视觉·计算机外设·excel·散列表·推荐算法·csdn开发云
transformer_WSZ2 天前
excel两列数据绘制折线图
excel·折线图
蒋胜山2 天前
Excel 练习题(5)
经验分享·excel