簡體   English   中英

在VBA用戶窗體中使用RefEdit_Change事件時出現故障

[英]Glitch when using RefEdit_Change Event in a VBA UserForm

應該發生以下情況:

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應該調整RefEdit1_Change的值,以使其在其旁邊或更佳的.offest(0,1)顯示對該列的引用。

但是,事實並非如此。值不會改變。 如果RefEdit1已經更改,則用戶單擊進入RefEdit2后,程序將中止,而不會出現錯誤消息。 如果您可以取消UserForm,我也會遇到Excel的嚴重崩潰。 我已經通過從頭開始重建UserForm並重命名了RefEdits暫時解決了該問題。 但是在某個時候它又重新出現了。 似乎這是Excel / VBA固有的問題。

有人知道如何解決此問題嗎?

歡迎使用丑陋的駭客和解決方法,總比沒有錯誤消息中止更好。

您需要將Range(RefEdit1.Value).offset(0, 1).Parent.Name inRange(RefEdit1.Value).offset(0, 1).Parent.Name in 'so中

="'" & Range(RefEdit1.Value).offset(0, 1).Parent.Name & "'!"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM