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的页面,使用函数时,会有刚才自己加的函数。

相关推荐
2601_967980118 小时前
Python自动化办公:模块、库与样式调整教程
python·excel·自动化办公·openpyxl·样式调整
用户03321266636711 小时前
使用 Python 轻松加密或解密 Excel 文档
python·excel
总裁余(余登武)2 天前
【python项目】:1操作EXCEl自动化开发
python·自动化·excel
ailitsoft4 天前
库存管理系统如何记录跨门店调拨后的收货差异?
excel·零售·软件需求
百事牛科技5 天前
Excel的三种密码:防看、防存、防乱改,各管各的
windows·excel
用户298698530145 天前
React 前端将 Excel 工作表导出为图片的技术实践
javascript·react.js·excel
用户0332126663676 天前
使用 Python 在 Excel 文档中添加水印
python·excel
VBA63378 天前
VBA数据库解决方案第二十六讲 工作表中不存在的数据在数据表中批量删除
vba
科技新资讯8 天前
AI如何批量处理Excel文件? 从数据合并、清洗到自动生成报表,看看 AI Agent 怎么完成任务
大数据·人工智能·excel
用户0332126663678 天前
使用 Python 在 Excel 中添加与删除筛选器
python·excel