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

相关推荐
喝汽水的猫^19 小时前
Java实现Excel 导出(多 Sheet、复杂格式)
java·excel
小费的部落21 小时前
Excel 在Sheet3中 匹配Sheet1的A列和Sheet2的A列并处理空内容
java·前端·excel
缺点内向1 天前
如何在 C# 中重命名 Excel 工作表并设置标签颜色
开发语言·c#·excel
wtsolutions1 天前
Sheet-to-Doc: Automate Document Generation with Excel Data and Word Templates
word·excel·wtsolutions
wtsolutions1 天前
Sheet-to-Doc:用Excel数据和Word模板自动生成文档
excel·wps·wtsolutions
程序猿ZhangSir1 天前
将数据库数据导出为Excel文件,并针对性的对部分数据做脱敏加密,常用方法解析
excel
哆啦A梦15882 天前
商城后台管理系统 03 Vue项目-实现表格导出EXCEL表格
前端·vue.js·excel
xingzhemengyou12 天前
python pandas操作excel
python·excel·pandas
小小心LOVE2 天前
Vue3 安装和使用 vue-office来实现 Word、Excel 和 PDF 文件的预览
vue.js·word·excel
cyhysr2 天前
oracle的model子句让sql像excel一样灵活2
sql·oracle·excel