2014-01-10 8 views
0

我在保持ListView控件的SelectedItem中的前一個值時遇到了一些麻煩。 我使用EXCEL VBA UserForm來顯示錶格內容並允許對列進行編輯。 但是,我無法保持之前的值。更新後,該值將爲空白。如果新值是一個空字符串,VBA ListView保持以前的值

任何想法? 謝謝!

Dim valBeforeEdit As String 

Private Sub listviewOBJ_AfterLabelEdit(Cancel As Integer, NewString As String) 
    If NewString <> "" Then 
    'Update database with new value 
     MsgBox "updated to " & NewString 
    Else 
     listviewOBJ.SelectedItem = valBeforeEdit 
    'Put the value back to whatever it was before user erased it to blank "" 
    End If 
End Sub 

Private Sub listviewOBJ_BeforeLabelEdit(Cancel As Integer) 
    valBeforeEdit = listviewOBJ.SelectedItem 
End Sub 

回答

1

而不是嘗試將其設置爲舊值,只需取消編輯。

Private Sub listviewOBJ_AfterLabelEdit(Cancel As Integer, NewString As String) 
    If NewString <> "" Then 
    'Update database with new value 
     MsgBox "updated to " & NewString 
    Else 
     Cancel = true '<-- This cancels the edit, keeping whatever the value was before it was set to "" by the user. 
    'Put the value back to whatever it was before user erased it to blank "" 
    End If 
End Sub 
+0

太棒了,謝謝! – JTFRage

相關問題