ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 6942|回复: 15

[分享] 简单分享建立类的多级属性

[复制链接]

TA的精华主题

TA的得分主题

发表于 2017-3-9 17:06 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:类和类模块
记得前两个月有位兄弟谈到需要建立类的多级属性问题。当时文字简单说了一下处理方法,那位兄弟实现了并解决了。今天因为处理程序时,又使用上这个,于是写了个范例以供大家参详,详见附件。粗糙代码,高手们就不用贱笑了。

类的多级属性范例(By.Micro).rar

23.62 KB, 下载次数: 656

评分

5

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-3-9 17:12 | 显示全部楼层
建立父类1、定义准备调用的子类
2、利用Class_Initialize对相关参数进行初始化
3、通过Property Get 子类调用时的名字() As 子类名称建立父类的子类
4、通过Property Let 设定类内使用的值(传递数值)设定类所需的相关值

  1. Private clYear As New Info_Year '定义年类变量
  2. Private clMonth As New Info_Month '定义月类变量
  3. Private clDay As New Info_Day '定义日类变量

  4. Private nYear As Integer, nMonth As Integer

  5. Private Sub Class_Initialize()
  6.     nYear = Year(Date)
  7.     nMonth = Month(Date)
  8.     iDay.vYear = nYear '设置初始的日期类的年的初始值
  9.     iDay.vMonth = nMonth '设置初始的日期类的月的初始值
  10. End Sub

  11. Property Get iYear() As Info_Year '返回年的类的属性
  12.     Set iYear = clYear
  13. End Property

  14. Property Get iMonth() As Info_Month '返回月的类的属性
  15.     Set iMonth = clMonth
  16. End Property

  17. Property Get iDay() As Info_Day '返回月的类的属性
  18.     Set iDay = clDay
  19. End Property

  20. Property Let vYear(ByVal nEnter As Integer) '设定本类的年份值
  21.     nYear = nEnter
  22.     iDay.vYear = nYear
  23. End Property

  24. Property Let vMonth(ByVal nEnter As Integer) '设定本类的月份值
  25.     nMonth = nEnter
  26.     iDay.vMonth = nMonth
  27. End Property

  28. Sub Reset() '重置
  29.     clYear.Reset
  30.     clMonth.Reset
  31.     clDay.Reset
  32. End Sub
复制代码



TA的精华主题

TA的得分主题

 楼主| 发表于 2017-3-9 17:26 | 显示全部楼层
附件为建立的三个年、月、日子类
建立子类——年
1、通过Private Sub Class_Initialize()进行类初始化,设定相关参数
2、通过Property Get Values() As Variant引用该子类的Values时,返回该子类所包含的数值的数组
3、通过Property Get Address() As String引用该子类的Address时,返回该子类所包含的数值的所在单元格地址的字符串

年子类代码
  1. Private rYear As Range
  2. Private vYear As Variant

  3. Private Sub Class_Initialize()
  4.     Dim sYear As String, nRow As Double
  5.    
  6.     With Sheets("基本资料")
  7.         nRow = .Cells(.Rows.Count, .[A1].Column).End(xlUp).Row
  8.         If nRow < 33 Then
  9.             .[A33] = Year(Date)
  10.             nRow = 33
  11.         End If
  12.         Set rYear = .[A33].Resize(nRow - 32)
  13.     End With
  14.     vYear = rYear.Value
  15.     If Not IsArray(vYear) Then
  16.         sYear = vYear
  17.         ReDim vYear(1 To 1, 1 To 1)
  18.         vYear(1, 1) = sYear
  19.     End If
  20. End Sub

  21. Property Get Values() As Variant '返回年份的数组
  22.     Values = vYear
  23. End Property

  24. Property Get Address() As String '年份,单元格地址的字符串
  25.     Address = "基本资料!" & rYear.Address
  26. End Property

  27. Sub Reset() '重置
  28.     Class_Initialize
  29. End Sub
复制代码


Info_SubClass.rar

1.86 KB, 下载次数: 113

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-3-9 17:31 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
建立子类——日
1、通过Private Sub Class_Initialize()进行类初始化,设定相关参数
2、通过Property Get Values() As Variant引用该子类的Values时,返回该子类所包含的数值的数组
3、通过Property Get Address() As String引用该子类的Address时,返回该子类所包含的数值的所在单元格地址的字符串
4、通过Property Get MaxDay() As Integer 引用该子类的MaxDay时,返回月份最后一天的日数
5、因为每月的选值区域随年月在变化,故通过Property Let vYear(ByVal nEnter As Integer) 来设置日期的年份,Property Let vMonth(ByVal nEnter As Integer) 来设置日期的月份
日子类代码
  1. Private rDay As Range
  2. Private vDay As Variant
  3. Private nYear As Integer, nMonth As Integer
  4. Private nMaxDay As Integer

  5. Private Sub Class_Initialize()
  6.     nYear = Year(Date)
  7.     nMonth = Month(Date)
  8.     初始化
  9. End Sub

  10. Property Get MaxDay() As Integer '返回月份最后一天的日数
  11.     MaxDay = Day(DateSerial(nYear, nMonth + 1, 1) - 1)
  12. End Property

  13. Property Get Values() As Variant '返回日期的数组
  14.     Values = vDay
  15. End Property

  16. Property Get Address() As String '返回日期的单元格地址的字符串
  17.     Address = "基本资料!$A$2:$A$" & (nMaxDay + 1)
  18. End Property

  19. Property Let vYear(ByVal nEnter As Integer) '设置日期的年份
  20.     nYear = nEnter
  21.     初始化
  22. End Property

  23. Property Let vMonth(ByVal nEnter As Integer) '设置日期的月份
  24.     nMonth = nEnter
  25.     初始化
  26. End Property

  27. Sub Reset() '重置
  28.     Class_Initialize
  29. End Sub

  30. Private Sub 初始化()
  31.     nMaxDay = Day(DateSerial(nYear, nMonth + 1, 1) - 1)
  32.     Set rDay = Sheets("基本资料").[A2].Resize(nMaxDay)
  33.     vDay = rDay.Value
  34. End Sub
复制代码



TA的精华主题

TA的得分主题

 楼主| 发表于 2017-3-9 17:38 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
建立父类
1、定义子类变量
2、通过Private Sub Class_Initialize()进行类初始化,设定相关参数
3、通过Property Get iYear() As Info_Year 引用父类的iYear时,返回年子类所有的属性
4、通过Property Get iMonth() As Info_Month引用父类的iMonth时,返回月子类所有的属性
5、通过Property Get iDay() As Info_Day引用父类的iDay时,返回日子类所有的属性
6、通过Property Let vYear(ByVal nEnter As Integer) Property Let vMonth(ByVal nEnter As Integer) 分别设定日子类所需的年份、月份值

父类类代码
  1. Private clYear As New Info_Year '定义年类变量
  2. Private clMonth As New Info_Month '定义月类变量
  3. Private clDay As New Info_Day '定义日类变量

  4. Private nYear As Integer, nMonth As Integer

  5. Private Sub Class_Initialize()
  6.     nYear = Year(Date)
  7.     nMonth = Month(Date)
  8.     iDay.vYear = nYear '设置初始的日期类的年的初始值
  9.     iDay.vMonth = nMonth '设置初始的日期类的月的初始值
  10. End Sub

  11. Property Get iYear() As Info_Year '返回年的类的属性
  12.     Set iYear = clYear
  13. End Property

  14. Property Get iMonth() As Info_Month '返回月的类的属性
  15.     Set iMonth = clMonth
  16. End Property

  17. Property Get iDay() As Info_Day '返回月的类的属性
  18.     Set iDay = clDay
  19. End Property

  20. Property Let vYear(ByVal nEnter As Integer) '设定本类的年份值
  21.     nYear = nEnter
  22.     iDay.vYear = nYear
  23. End Property

  24. Property Let vMonth(ByVal nEnter As Integer) '设定本类的月份值
  25.     nMonth = nEnter
  26.     iDay.vMonth = nMonth
  27. End Property

  28. Sub Reset() '重置
  29.     clYear.Reset
  30.     clMonth.Reset
  31.     clDay.Reset
  32. End Sub
复制代码



InfoClass.rar

666 Bytes, 下载次数: 116

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-3-9 17:43 | 显示全部楼层
引用类
1、过程使用时,先定义Info为类变量
2、通过Info.iMonth.Address获得对应类别的地址字符串
3、通过Info.vYear = nYear对类内参数进行设置
  1. Private Info As New InfoClass

  2. Sub SelectChange(ByVal Target As Range)
  3.     Dim sValidation As String, nYear As Integer, nMonth As Integer
  4.    
  5.     With Target
  6.         If .Column = 2 Then
  7.             If .Row = 1 Then '获得年的数据有效性的地址字符串
  8.                 sValidation = "=" & Info.iYear.Address
  9.             ElseIf .Row = 2 Then '获得月的数据有效性的地址字符串
  10.                 sValidation = "=" & Info.iMonth.Address
  11.             ElseIf .Row = 3 Then '获得日的数据有效性的地址字符串
  12.                 nYear = [B1].Value
  13.                 If nYear = 0 Then nYear = Year(Date)
  14.                 nMonth = [B2].Value
  15.                 If nMonth = 0 Then nMonth = Month(Date)
  16.                 Info.vYear = nYear
  17.                 Info.vMonth = nMonth
  18.                 sValidation = "=" & Info.iDay.Address
  19.             End If
  20.             With .Validation
  21.                 .Delete
  22.                 .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=sValidation
  23.             End With
  24.         End If
  25.     End With
  26. End Sub
复制代码


评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2018-3-21 17:23 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
好资料,十分感谢分享。

TA的精华主题

TA的得分主题

发表于 2018-12-6 08:10 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2019-11-20 10:50 | 显示全部楼层
先标记,回头过来学习类模块

TA的精华主题

TA的得分主题

发表于 2019-11-30 16:52 | 显示全部楼层
代码量怎么这么大?感觉好难啊!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 02:14 , Processed in 0.059742 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表