Excel 覆盖列表框的sourcerange范围内的值

Excel 覆盖列表框的sourcerange范围内的值,excel,vba,listbox,Excel,Vba,Listbox,我在userform上有一个listbox,它有一个sourcerange,我试图通过提供userform中的值来覆盖它,但一旦我覆盖了一个特定的单元格,就会触发事件ListBox1\u Click(),这是不希望的,因为它会在userform上重新填充数据 Private Sub ListBox1_Click() Application.EnableEvents = False Dim i As Long, fRow As Long For i = 0 To ListBox1.ListCo

我在userform上有一个listbox,它有一个sourcerange,我试图通过提供userform中的值来覆盖它,但一旦我覆盖了一个特定的单元格,就会触发事件
ListBox1\u Click()
,这是不希望的,因为它会在userform上重新填充数据

Private Sub ListBox1_Click()

Application.EnableEvents = False
Dim i As Long, fRow As Long

For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then
        If i > 0 Then
            HSht.Range("cRow").Value = i + 1
            fRow = HSht.Range("cRow").Value
            Call getData(fRow)
            HSht.Range("LRow").Value = getlastRow()
            Me.ItemLbl.Caption = "Item number :" & HSht.Range("cRow").Value - 1 & " of " & HSht.Range("LRow").Value - 1
        End If
        Exit For
    End If
Next i

Application.EnableEvents = True
End Sub
以下是更新按钮代码:

Private Sub cmdUpdate_Click()

Application.EnableEvents = False

'Update
Dim fRow As Long, i As Long
fRow = HSht.Range("cRow").Value
Call updateData(fRow)
HSht.Range("LRow").Value = getlastRow()
Me.ItemLbl.Caption = "Item number :" & HSht.Range("cRow").Value - 1 & " of " & HSht.Range("LRow").Value - 1

'MsgBox "Data updated successfully"
Application.EnableEvents = True
End Sub

例如,假设你有10个字段,在用户窗体上有10个文本框来查看/修改数据,但你也有多列列表框来查看和滚动表格格式的数据,当我向上或向下滚动时,我在用户窗体上的文本框中得到特定行数据,我还有一个按钮,上面写着“覆盖”如果我想通过userform修改工作表上的数据。但一旦它修改了工作表中的一个单元格,就会触发事件“Listbox1_click”,并覆盖用户表单上的数据。

Application.EnableEvents=false
UserForms
。如果禁用了以下事件,则必须在事件开始和退出事件子项时创建属性并检查其值:

Private Sub ListBox1_Click()

Application.EnableEvents = False
Dim i As Long, fRow As Long

For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then
        If i > 0 Then
            HSht.Range("cRow").Value = i + 1
            fRow = HSht.Range("cRow").Value
            Call getData(fRow)
            HSht.Range("LRow").Value = getlastRow()
            Me.ItemLbl.Caption = "Item number :" & HSht.Range("cRow").Value - 1 & " of " & HSht.Range("LRow").Value - 1
        End If
        Exit For
    End If
Next i

Application.EnableEvents = True
End Sub
' Top of UserForm-Class
Public EnableEvents As Boolean ' if Private code outside the userform can't change value.
'One should add a Letter/Getter to have more control over the property (exposing the variable that stores a property-value isn't recommended I think, with Get/Let we can perform checks or just make the Letter private, but the Getter public)
Private Sub UserForm_Initialize()
    Me.EnableEvents = True
End Sub

Private Sub ListBox1_Click()
If Me.EnableEvents = False Then 'the first three lines of code suppress the events-code execution if EnableEvents = False and must be on top of every event that you want to have disabled.
  Exit Sub
End If
 'Me.EnableEvents = False should be set on top of button code and Me.EnableEvents = True at buttom if other events of from should be suppressed.

Dim i As Long, fRow As Long

For i = 0 To ListBox1.ListCount - 1
  ...

End Sub

Private Sub cmdUpdate_Click()
If Me.EnableEvents = False Then 'the first three lines of code suppress the events-code execution and must be on top of every event that you want to have disabled.
  Exit Sub
End If
Me.EnableEvents = False 'disable Form-Events

... 'Button-Code

Me.EnableEvents = True 'reenable events
End Sub

你不认为按钮后面的代码导致了你的问题吗?那就表演吧!您试图禁用/启用此代码中的事件?@ComputerVersteher我在每个代码中都禁用了事件subroutine@ComputerVersteher按钮单击有多行,如sheet1.range(“A1”)=me.textbox1.value,这会触发listbox1,因为它的rowsource范围来自该范围