Class 」标签的归档

VBHelper

前面介紹了一個實用的xla,今天來介紹一下它的用法,順便講解一下class在vba中的應用.

在加載該插件之後,可以在VBE的菜單欄中的工具菜單下,找到’DK Tools’.

1. 添加Module

image VBHelper

  • C: 插入Class Module
  • M: 插入普通Module
  • U: 插入Form Module

继续阅读

article clipper remember VBHelper
 

Declare Constants in a class module

VBA中的class是不能定義常量constants的, 對於習慣OOP的人來說,這當然會帶來不便. 但是,我們還是可以”曲綫”實現這一目的.

方法就是利用Enum.但可惜的是,該曲綫方法只支持Long類型的參數,因為Enum只支持Long類型.
更多關於Enum的信息,可參閱此文.

Option Explicit
' The following code saved in a normal module
Sub main()

Dim clsFruit As New CFruit
clsFruit.printMe

End Sub
Option Explicit

Public Enum enumFruits
    banana = 5
    apple = 2.5
    orange
End Enum


Function printMe()

Debug.Print enumFruits.banana   '5
Debug.Print enumFruits.apple   'result will still be 2, not 2.5
'3 Those constants without a value would be one more than the previous variable
Debug.Print enumFruits.orange  
End Function
技術上VB根本不承認Public Enum是類的一部份

There’s no need even to instantiate the class, since technically Visual Basic doesn’t consider Public Enum as members of a class, even though they’re written to the type library.

article clipper remember Declare Constants in a class module
 

Class Example

Daily Dose of Excel 一连4集,通过银行的一个ACH transactions (Automated Clearing House, think direct deposit) 的例子,介绍了VBA中对Class的运用,可谓经典!
其中用到的一个讲Public Property转化为GET/SET语句的xla 可以到此Link获取.

article clipper remember Class Example