VBA编译错误,谁能帮我看看?

来源:百度知道 编辑:UC知道 时间:2024/05/07 17:06:52
模块1中的代码:
Option Explicit

Type MyStyle
incHeading3 As Balloon
incHeading4 As Balloon
End Type

Sub createTestReport()
UserForm1.Show
End Sub

Function test(hdStyle As MyStyle)
If hdStyle.incHeading3 = True Then MsgBox "Use wdStyleHeading3"
ElseIf hdStyle.incHeading4 = True Then MsgBox "Use wdStyleHeading4"
End Function

控件对象中的代码:
Private Sub CommandButton1_Click()
Dim hStyle As MyStyle
If CheckBox2.Value Then
hStyle.incHeading3 = True
ElseIf CheckBox3.Value Then
hStyle.incHeading4 = True
End If
test hStyle '调用自定义函数
'UserForm1.Hide
End Sub

使用了自定义结构与自定义函数,随便选择任一个CheckBox,都提示“对象变量或With块变量未设置”。hStyle.incHeading3 或 hStyle.incHeading4 的值都是nothing ,为什么会这样啊??请高人解答。
另外,调用函数不能用 test (hStyle) 这样,而要用 test hStyle,什么原因?

你的代码有问题
新建一个模块
Public Type MyStyle
incHeading3 As Boolean ’是BOOLEAN不是BALLOON
incHeading4 As Boolean
End Type

在窗体里加:
Option Explicit

Sub createTestReport()
UserForm1.Show
End Sub

Function test(hdStyle As MyStyle)
If hdStyle.incHeading3 = True Then
MsgBox "Use wdStyleHeading3"
ElseIf hdStyle.incHeading4 = True Then
MsgBox "Use wdStyleHeading4"
End If
End Function

Private Sub CommandButton1_Click()
Dim hStyle As MyStyle
If CheckBox2.Value Then
hStyle.incHeading3 = True
ElseIf CheckBox3.Value Then
hStyle.incHeading4 = True
End If
test hStyle '调用自定义函数
'UserForm1.Hide
End Sub