应该发生以下情况:
1。显示具有2个RefEdit控件的UserForm
2。第一个RefEdit用于选择范围
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后,程序将中止,而不会出现错误消息。如果您可以取消UserForm,我也会遇到Excel的严重崩溃。我已经通过从头开始重建UserForm并重命名了RefEdits暂时解决了该问题。但是在某个时候它又重新出现了。似乎这是Excel / VBA固有的问题。
有人知道如何解决此问题吗?
欢迎使用难看的黑客手段和变通办法,比这更好的选择是,中止而不会出现错误消息。
答案 0 :(得分:1)
您需要随附Range(RefEdit1.Value).offset(0, 1).Parent.Name in
'
="'" & Range(RefEdit1.Value).offset(0, 1).Parent.Name & "'!"