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

相关推荐
蒋胜山1 天前
Excel 练习题(7)
经验分享·excel
蒋胜山1 天前
Excel 练习题(6)
经验分享·excel
wcy_10111 天前
QCoder智能生成Excel数据清洗与可视化代码
python·excel
JoshRen2 天前
2026教程:上传Excel,用Gemini 3镜像站多模态一键生成问卷分析图表代码与结论(附国内免费方案)
excel
实战编程3 天前
Temu 插件导出 Excel 图片问题总结(SheetJS / ExcelJS)
excel
Data-Miner3 天前
用DeepSeek V4做表:数以轻舟Agent让做Excel表像聊天一样简单
microsoft·excel
Eiceblue4 天前
使用 C# 将 Excel 转换为 Markdown 表格(含批量转换示例)
开发语言·c#·excel
Java面试题总结4 天前
使用 Python 设置 Excel 数据验证
开发语言·python·excel
追逐梦想永不停4 天前
记录一个好用的excel判断数字格式的公式
前端·chrome·excel
Eiceblue4 天前
C# 如何实现 Word 转 Excel ?分享两种实用方法
c#·word·excel