首页 > 解决方案 > 如何将用户定义类型传递给 VBA 函数?

问题描述

这似乎应该很容易实现,但我目前正在努力,任何帮助将不胜感激。

我有一个用户定义类型,它定义了 4 个与日期信息相关的字段。我想将链接到这种类型的变量传递给一个函数,每个月都进行索引。下面是类型,子和功能。

我知道该函数不知道 TestMDate 类型,我尝试在函数中声明类型,但似乎没有任何效果。

提前谢谢了

格鲁瓦

''''''''''''''''''''''''''''''''
'UDT
''''''''''''''''''''''''''''''''
Private Type TestMDate
        MonthS As String
        MonthD As String
        StartD As String
        EndD As String
End Type
    
''''''''''''''''''''''''''''''''
'Sub
''''''''''''''''''''''''''''''''
Sub UpdateChart()

Dim Tstruct(12) As TestMDate
Dim a As Integer, i As Integer


    
''''''''''''''''''''''''''''''''
'
''''''''''''''''''''''''''''''''
Tstruct(1).MonthS = "Jan"
Tstruct(1).MonthD = "01"
Tstruct(1).StartD = "01"
Tstruct(1).EndD = "31"
Tstruct(2).MonthS = "Feb"
Tstruct(2).MonthD = "02"
Tstruct(2).StartD = "01"
Tstruct(2).EndD = "28"
Tstruct(3).MonthS = "Mar"
Tstruct(3).MonthD = "03"
Tstruct(3).StartD = "01"
Tstruct(3).EndD = "31"
Tstruct(4).MonthS = "Apr"
Tstruct(4).MonthD = "04"
Tstruct(4).StartD = "01"
Tstruct(4).EndD = "30"
Tstruct(5).MonthS = "May"
Tstruct(5).MonthD = "05"
Tstruct(5).StartD = "01"
Tstruct(5).EndD = "31"
Tstruct(6).MonthS = "Jun"
Tstruct(6).MonthD = "06"
Tstruct(6).StartD = "01"
Tstruct(6).EndD = "30"
Tstruct(7).MonthS = "Jul"
Tstruct(7).MonthD = "07"
Tstruct(7).StartD = "01"
Tstruct(7).EndD = "31"
Tstruct(8).MonthS = "Aug"
Tstruct(8).MonthD = "08"
Tstruct(8).StartD = "01"
Tstruct(8).EndD = "31"
Tstruct(9).MonthS = "Sep"
Tstruct(9).MonthD = "09"
Tstruct(9).StartD = "01"
Tstruct(9).EndD = "30"
Tstruct(10).MonthS = "Oct"
Tstruct(10).MonthD = "10"
Tstruct(10).StartD = "01"
Tstruct(10).EndD = "31"
Tstruct(11).MonthS = "Nov"
Tstruct(11).MonthD = "11"
Tstruct(11).StartD = "01"
Tstruct(11).EndD = "30"
Tstruct(12).MonthS = "Dec"
Tstruct(12).MonthD = "12"
Tstruct(12).StartD = "01"
Tstruct(12).EndD = "31"

   
For i = 1 To 12
    a = proc_res_field(Tstruct(i))
Next i
End Sub

''''''''''''''''''''''''''''''''
'Function
''''''''''''''''''''''''''''''''
Function proc_res_field(Tstruct As TestMDate)
    Debug.Print Tstruct.MonthS
    Debug.Print Tstruct.MonthD
    Debug.Print Tstruct.StartD
    Debug.Print Tstruct.EndD
    a = 1
End Function

标签: excelvbafunctiontypes

解决方案


proc_res_field不知道任何事情ia

Function proc_res_field(Tstruct As TestMDate)
    Debug.Print Tstruct.MonthS
    Debug.Print Tstruct.MonthD
    Debug.Print Tstruct.StartD
    Debug.Print Tstruct.EndD
    proc_res_field = 1 '?
End Function

推荐阅读