如果新值为空字符串,VBA ListView将保留以前的值

时间:2014-01-10 18:48:51

标签: excel vba listview

我在ListView控件的SelectedItem中保留先前的值时遇到了一些麻烦。 我正在使用EXCEL VBA UserForm来显示表格内容并允许对列进行编辑。 但是,我无法维持以前的值。更新后,该值将为空白。

有什么想法吗? 谢谢!

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 个答案:

答案 0 :(得分: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