excel - 类属性错误“需要对象”,但工作正常
问题描述
vba的奇怪问题。我正在尝试创建具有表装饰属性的模块。在表单中,用户选择表格标题背景的颜色。颜色已更改,但有错误。我怎样才能解决这个问题?想法?
装饰模块:
Private sHeaderTextBackground As Double
Public sHeaderRange As Range
Public Sub initVars()
Set sHeaderRange = Range("A1:F1")
End Sub
Public Property Let HeaderTextBackground(ByVal color As Double)
Let sHeaderTextBackground = color
End Property
Public Property Get HeaderTextBackground() As Double
HeaderTextBackground = sHeaderTextBackground
End Property
代码形式:
Private Sub changeStyleApplyButton_Click()
'Call DecorationModule.initVars
If Application.Dialogs(xlDialogEditColor).Show(40) = True Then
MsgBox (VarType(ThisWorkbook.Colors(40))) '<--- 5 = vbDouble
DecorationModule.HeaderTextBackground = ThisWorkbook.Colors(40) <---Error here
DecorationModule.sHeaderRange.Interior.color = DecorationModule.HeaderTextBackground
End If
解决方案
对于以后遇到此错误的人:确保您已Module
正确命名,并且没有任何拼写错误。这应该涵盖大部分错误。
问题是ClassModule
使用 a 而不是 a Module
。代码应粘贴到Module
(使用正确的名称)中,然后代码才能工作。确保sHeaderRange
在执行代码之前初始化。如果您想确保它仅在您可以将initVars()
sub 更改为:
Public Sub initVars()
If sHeaderRange Is Nothing Then
Set sHeaderRange = Range("A1:F1")
End If
End Sub
推荐阅读
- python - 使用数据框中的值作为键来替换第二个数据框中的值
- r - 带有线条rstudio的闪亮的情节渲染问题
- typescript - Angular 7 - PrimeNg ConfirmationDialog 问题
- python - 类实例关系可以是双边的吗?
- c# - 通过 ISecurityTokenValidator.ValidateToken 获得的 ClaimsPrincipal 正在更改 SCP 声明的名称
- c# - 在 C# 中绑定 2 个字段
- javascript - 如何让异步函数使用 Array.Filter 传入元素作为其参数
- javascript - 如何更新 React 中的元素列表
- jenkins - 解除工作人员 RAM 内存限制
- python - Python Selenium 刮取 USPS