王佩丰老师vba课程笔记——第十七讲 触"类"旁通 类模块
发布网友
发布时间:2024-09-17 08:44
我来回答
共1个回答
热心网友
时间:2024-09-29 22:29
第十七讲 触“类”旁通2:类模块
一、共有与私有
1、Private Sub text()
MsgBox "aa"
End Sub
在同一模块中,可以通过call text 调用,但要是在其他模块就会报错
默认是public,都可以调用;private 只有本模块能调用。
2、Sub text()
i = 1
End Sub
Sub text1()
MsgBox i
End Sub
没有值弹出
3、Dim i As Integer
Sub text()
i = 1
End Sub
Sub text1()
MsgBox i
End Sub
弹出i
当变量定义在所有过程的外面,所有的过程都可以定义改变量。但在别的模块不能调用,除非是 public I as integer (谨慎使用)。可以在模块中定义分函数:
Function qbl()
qbl = i
End Function => msgbox qbl()
二、类模块
1、新建一月表:
Sub test()
Dim sht, sht1 As Worksheet
For Each sht In Sheets
If sht.Name = "一月" Then
k = k + 1
End If
Next
If k = 0 Then
Set sht1 = Sheets.Add
sht1.Name = "一月"
End If
End Sub
è
Sub test()
Call sadd("二月")
End Sub
Sub sadd(str As String)
Dim sht, sht1 As Worksheet
For Each sht In Sheets
If sht.Name = str Then
k = k + 1
End If
Next
If k = 0 Then
Set sht1 = Sheets.Add
sht1.Name = str
End If
End Sub
2、删除二月表
Sub test1()
Call sdelete("二月")
End Sub
Sub sdelete(str As String)
Dim sht As Worksheet
Application.DisplayAlerts = False
For Each sht In Sheets
If sht.Name = str Then
sht.Delete
End If
Next
Application.DisplayAlerts = True
End Sub
3、类模块
类模块的方法:
插入类模块,打开属性窗口,将类的名字改为supersheet
将上面两个带参数的过程复制到类模块中
类模块的属性:
Property Get scount()
scount = Sheet.Count
End Property
在模块中调用类:
Sub test()
Dim aaa As New supersheet
aaa.sadd ("二月")
MsgBox aaa.scount
End Sub
4、练习
要是单元格为空,则删除总行
Sub rdelete(rng As Range)
If rng = "" Then
rng.EntireRow.Delete
End If
End Sub
Sub test1()
Dim bb As New superrange
For i = 25 To 1 Step -1
bb.rdelete Range("d" & i)
Next
End Sub
!将类导出,以后可以导入到excel中直接处理数据
三、do while 循环
Sub test()
Do While InputBox("请输入密码") <> "123"
Loop
End Sub
当输入的密码不等于123时执行循环,等于123时退出循环
热心网友
时间:2024-09-29 22:23
第十七讲 触“类”旁通2:类模块
一、共有与私有
1、Private Sub text()
MsgBox "aa"
End Sub
在同一模块中,可以通过call text 调用,但要是在其他模块就会报错
默认是public,都可以调用;private 只有本模块能调用。
2、Sub text()
i = 1
End Sub
Sub text1()
MsgBox i
End Sub
没有值弹出
3、Dim i As Integer
Sub text()
i = 1
End Sub
Sub text1()
MsgBox i
End Sub
弹出i
当变量定义在所有过程的外面,所有的过程都可以定义改变量。但在别的模块不能调用,除非是 public I as integer (谨慎使用)。可以在模块中定义分函数:
Function qbl()
qbl = i
End Function => msgbox qbl()
二、类模块
1、新建一月表:
Sub test()
Dim sht, sht1 As Worksheet
For Each sht In Sheets
If sht.Name = "一月" Then
k = k + 1
End If
Next
If k = 0 Then
Set sht1 = Sheets.Add
sht1.Name = "一月"
End If
End Sub
è
Sub test()
Call sadd("二月")
End Sub
Sub sadd(str As String)
Dim sht, sht1 As Worksheet
For Each sht In Sheets
If sht.Name = str Then
k = k + 1
End If
Next
If k = 0 Then
Set sht1 = Sheets.Add
sht1.Name = str
End If
End Sub
2、删除二月表
Sub test1()
Call sdelete("二月")
End Sub
Sub sdelete(str As String)
Dim sht As Worksheet
Application.DisplayAlerts = False
For Each sht In Sheets
If sht.Name = str Then
sht.Delete
End If
Next
Application.DisplayAlerts = True
End Sub
3、类模块
类模块的方法:
插入类模块,打开属性窗口,将类的名字改为supersheet
将上面两个带参数的过程复制到类模块中
类模块的属性:
Property Get scount()
scount = Sheet.Count
End Property
在模块中调用类:
Sub test()
Dim aaa As New supersheet
aaa.sadd ("二月")
MsgBox aaa.scount
End Sub
4、练习
要是单元格为空,则删除总行
Sub rdelete(rng As Range)
If rng = "" Then
rng.EntireRow.Delete
End If
End Sub
Sub test1()
Dim bb As New superrange
For i = 25 To 1 Step -1
bb.rdelete Range("d" & i)
Next
End Sub
!将类导出,以后可以导入到excel中直接处理数据
三、do while 循环
Sub test()
Do While InputBox("请输入密码") <> "123"
Loop
End Sub
当输入的密码不等于123时执行循环,等于123时退出循环