Excel-宏与VBA-有参数子程序-函数过程

有参数的子程序

不能返回值

  1. 基本使用案例
Option Explicit
Sub SubTest(toNum As Integer, addStep As Integer)
  Dim num As Integer, total As Long
  For num To toNum Step addStep
    total = total + num
  MsgBox total
End Sub

Sub Test()
  SubTest 100, 2
End Sub

函数过程

可以返回值

  1. 基本使用案例
Option Explicit
Function CubeSum(x As Double, y As Double)
  CubeSum = x * x * x + y * y * y
End Function

Sub Test()
  MsgBox CubeSum
End Sub
  1. 设置返回值类型
Option Explicit
Function CubeSum(x As Double, y As Double) As Double
  CubeSum = x * x * x + y * y * y
End Function

Sub Test()
  MsgBox CubeSum
End Sub
posted @ 2021-05-13 09:34  乔悟空  阅读(316)  评论(0编辑  收藏  举报