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

相关推荐
Non-existent9872 小时前
TableGIS的WPS扩展项插件-通信网优规划相关地理空间数据处理
数据分析·excel·wps
开开心心就好1 天前
一键扫描电脑重复文件的实用工具
linux·运维·服务器·随机森林·智能手机·excel·启发式算法
ModestCoder_1 天前
Git 忽略所有 `.xlsx`,但保留指定 Excel 文件的方法
git·excel
z19408920661 天前
在线题库整理:把重复劳动从 Excel 手工里解放出来
excel
开开心心_Every1 天前
免费简洁的安卓黄历日历,软件推荐
运维·服务器·随机森林·pdf·电脑·excel·最小二乘法
VBAMatrix2 天前
deepseek-v4正式接入Excel,一键生成财务分析报告
word·excel·审计·财务分析·deepseek·会计师事务所·tb工具箱
专注VB编程开发20年3 天前
傻瓜式Office 功能区插件 / Ribbon开发模板
ribbon·excel·vba·插件·扩展宏
专注VB编程开发20年3 天前
Excel 2024 / 365 已经把 “单元格填充效果→图片” 这个按钮删掉了
excel
开开心心就好4 天前
仅168KB的桌面图标自动隐藏工具
windows·计算机视觉·计算机外设·excel·启发式算法·宽度优先·csdn开发云
蒋胜山4 天前
Excel 练习题(7)
经验分享·excel