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

相关推荐
XYX的Blog12 小时前
Pandas基础07(Csv/Excel/Mysql数据的存储与读取)
mysql·excel·pandas
东京老树根16 小时前
Excel 技巧23 - 在Excel中用切片器做出查询效果(★★★)
笔记·学习·excel
东京老树根2 天前
Excel 技巧22 - Ctrl+D 向下复制(★★),复制同间距图形
笔记·学习·excel
如意机反光镜裸2 天前
如何批量导入竖版Excel表到数据库
数据库·excel
NiNg_1_2342 天前
FastExcel使用详解
开发语言·excel·fastexcel
小奥超人4 天前
EXCEL教程:如何打开Excel隐藏部分?
windows·经验分享·microsoft·excel·办公技巧
Eiceblue4 天前
C# 添加、替换、提取、或删除Excel中的图片
开发语言·c#·excel·visual studio
ynrainy4 天前
Excel分区间统计分析(等步长、不等步长、多维度)
excel
扎量丙不要犟5 天前
excel如何查找一个表的数据在另外一个表是否存在
rust·excel
xun-ming5 天前
Excel中LOOKUP函数的使用
excel·xlookup·vlookup·lookup·hlookup