admin管理员组

文章数量:1794759

VBA之使用自定义函数

VBA之使用自定义函数

如何在VBA中使用自定义的函数呢? 首先在模块里定义好函数,然后注意要往里面传参 然后在excel单元格里调用该函数

1.需求1,判断性别

代码如下 Function zmj(x) zmj = (x / 6.03) - x * 0.03

End Function

Function xb(x As String)

If x = "男" Then xb = "先生" ElseIf x = "女" Then xb = "女士" End If

End Function

Sub test()

Range(“l2”) = zmj(Range(“k2”))

End Sub

2.需求2,提取字符串 代码如下

Function zfcqg(str As String, str2 As String, i As Integer)

zfcqg = Split(str, str2)(i) 'split("ds-ds-ds-ds","-")(2) 'split有3个参数 第一个是要切割的字符串,第二个是想按什么切割,第三个是取第几位

End Function

本文标签: 自定义函数VBA