在 VBA 用户窗体中使用 RefEdit_Change 事件时出现故障
Glitch when using RefEdit_Change Event in a VBA UserForm
应该会发生以下情况:
1.显示带有 2 个 RefEdit 控件的用户窗体
2。第一个RefEdit用于select一个范围
3。 RefEdit_Change 事件将第二个 RefEdit 控件调整为范围
的 .offset(0,1)
这里是我的代码:
模块 1:
Dim frmSelectXY As New frmSelectImportData
With frmSelectXY
.Show
.DoStuffWithTheSelectedRanges
End With
用户窗体:frmSelectImportData
Option Explicit
Private Type TView
IsCancelled As Boolean
xrng As Range
yrng As Range
End Type
Private this As TView
Public Property Get IsCancelled() As Boolean
IsCancelled = this.IsCancelled
End Property
Public Property Get yrng() As Range
Set yrng = this.yrng
End Property
Public Property Get xrng() As Range
Set xrng = this.xrng
End Property
'Here is where the fun happens
Private Sub RefEdit1_Change()
'RefEdit2.Value = RefEdit1.Value
If InStr(1, RefEdit1.Value, "[") <> 0 And InStr(1, RefEdit1.Value, "!") <> 0 Then
RefEdit2.Value = Range(RefEdit1.Value).offset(0, 1).Address(External:=True)
ElseIf InStr(1, RefEdit1.Value, "!") <> 0 Then
RefEdit2.Value = Range(RefEdit1.Value).offset(0, 1).Parent.Name & "!" & Range(RefEdit1.Value).offset(0, 1).Address(External:=False)
Else
RefEdit2.Value = Range(RefEdit1.Value).offset(0, 1).Address(External:=False)
End If
End Sub
Private Sub SaveBTN_Click()
Set this.xrng = Range(RefEdit1.Value)
Set this.yrng = Range(RefEdit2.Value)
If Not validate Then
MsgBox "x-values and y-values need to have the same size."
Else
Me.Hide
End If
End Sub
Function validate() As Boolean
validate = False
If this.xrng.count = this.yrng.count Then validate = True
End Function
RefEdit1_Change
应该调整 RefEdit2 的值,这样它就会显示对紧挨着它的列的引用,或者更好的是 .offest(0,1)
。
但事实并非如此.. 值不会改变。如果 RefEdit1 已被更改,只要用户单击进入 RefEdit2,程序就会中止,不会出现错误消息。如果您取消用户窗体,我也遇到过 excel 的严重崩溃。我通过从头开始重建用户窗体并重命名 RefEdits 暂时解决了这个问题。但在某个时候它又出现了。似乎这是一个 Excel/VBA 固有的问题。
有人知道如何解决这个问题吗?
欢迎丑陋的 hacks 和解决方法,任何事情都比,中止没有错误消息。
你需要附上 Range(RefEdit1.Value).offset(0, 1).Parent.Name in
' 所以
="'" & Range(RefEdit1.Value).offset(0, 1).Parent.Name & "'!"
应该会发生以下情况:
1.显示带有 2 个 RefEdit 控件的用户窗体
2。第一个RefEdit用于select一个范围
3。 RefEdit_Change 事件将第二个 RefEdit 控件调整为范围
的 .offset(0,1)这里是我的代码:
模块 1:
Dim frmSelectXY As New frmSelectImportData
With frmSelectXY
.Show
.DoStuffWithTheSelectedRanges
End With
用户窗体:frmSelectImportData
Option Explicit
Private Type TView
IsCancelled As Boolean
xrng As Range
yrng As Range
End Type
Private this As TView
Public Property Get IsCancelled() As Boolean
IsCancelled = this.IsCancelled
End Property
Public Property Get yrng() As Range
Set yrng = this.yrng
End Property
Public Property Get xrng() As Range
Set xrng = this.xrng
End Property
'Here is where the fun happens
Private Sub RefEdit1_Change()
'RefEdit2.Value = RefEdit1.Value
If InStr(1, RefEdit1.Value, "[") <> 0 And InStr(1, RefEdit1.Value, "!") <> 0 Then
RefEdit2.Value = Range(RefEdit1.Value).offset(0, 1).Address(External:=True)
ElseIf InStr(1, RefEdit1.Value, "!") <> 0 Then
RefEdit2.Value = Range(RefEdit1.Value).offset(0, 1).Parent.Name & "!" & Range(RefEdit1.Value).offset(0, 1).Address(External:=False)
Else
RefEdit2.Value = Range(RefEdit1.Value).offset(0, 1).Address(External:=False)
End If
End Sub
Private Sub SaveBTN_Click()
Set this.xrng = Range(RefEdit1.Value)
Set this.yrng = Range(RefEdit2.Value)
If Not validate Then
MsgBox "x-values and y-values need to have the same size."
Else
Me.Hide
End If
End Sub
Function validate() As Boolean
validate = False
If this.xrng.count = this.yrng.count Then validate = True
End Function
RefEdit1_Change
应该调整 RefEdit2 的值,这样它就会显示对紧挨着它的列的引用,或者更好的是 .offest(0,1)
。
但事实并非如此.. 值不会改变。如果 RefEdit1 已被更改,只要用户单击进入 RefEdit2,程序就会中止,不会出现错误消息。如果您取消用户窗体,我也遇到过 excel 的严重崩溃。我通过从头开始重建用户窗体并重命名 RefEdits 暂时解决了这个问题。但在某个时候它又出现了。似乎这是一个 Excel/VBA 固有的问题。
有人知道如何解决这个问题吗?
欢迎丑陋的 hacks 和解决方法,任何事情都比,中止没有错误消息。
你需要附上 Range(RefEdit1.Value).offset(0, 1).Parent.Name in
' 所以
="'" & Range(RefEdit1.Value).offset(0, 1).Parent.Name & "'!"