我如何在其他类模块中创建类模块并为vba excel中的类传递值

  • 本文关键字:模块 excel vba 创建 其他 vba
  • 更新时间 :
  • 英文 :


我有一个麻烦,所以我需要创建一个类模块。例如条件房包括一些属性,如宽度,长度,高度,体积,窗户类型,墙壁类型,屋顶类型,但窗户,墙壁,屋顶都是类模块与其他属性,如窗户:玻璃单层或双层,框架木或铝等,…那么,我如何创建类模块条件房间由其他类模块组成如上所述谢谢你的帮助,

您需要创建单独的类(文件)

把下面的每一个放在单独的*中。cls文件:

' Windows.cls
Public Enum GlassType
SingleLayer
DoubleLayer
End Enum
Private m_Glass As GlassType
Public Property Get Glass As GlassType
Glass = m_Glass
End Property
Public Property Let Glass (ByVal value As GlassType)
m_Glass = value
End Property
' ---
' RoomDimension.cls
Private m_Height As Double
Private m_Length As Double
Private m_Width As Double
Public Property Get Height As Double
Height = m_Height
End Property
Public Property Let Height (ByVal value As Double)
m_Height = Abs(value)
End Property
Public Property Get Length As Double
Length = m_Length
End Property
Public Property Let Length (ByVal value As Double)
m_Length = Abs(value)
End Property
Public Property Get Width As Double
Width = m_Width
End Property
Public Property Let Width (ByVal value As Double)
m_Width = Abs(value)
End Property
Public Property Get Volume As Double
Volume = Length * Width * Height
End Property
' ---
' ConditionRoom.cls
Private m_Window As Windows
Private m_Dimension As RoomDimension
Public Property Get Window As Windows
Window = m_Window
End Property
Public Property Let Window (ByVal value As Windows)
m_Window = value
End Property
Public Property Get Dimension As RoomDimension
Dimension = m_Dimension
End Property
Public Property Let m_Dimension (ByVal value As RoomDimension)
m_Dimension = value
End Property
Class_Initialize()
Set Window = New Windows
Set Dimension = New RoomDimension
End Class
' ---
' Code sample on how to use it
Dim room As ConditionRoom
Set room = New ConditionRoom
' Set the window type
room.Window.Glass = GlassType.SingleLayer
' Set room dimensions
With room.Dimension
.Height = 250
.Length = 500
.Width = 500
Debug.Print "Room volume: "; .Volume
End With

在excel中,您不能在类中创建类,父/子或父/子类在VBA中不受支持。但是您可以使用接口来实现类似的功能。

最新更新