我在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
答案 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