gpt4 book ai didi

excel - 覆盖范围内的值,该范围是列表框的源范围

转载 作者:行者123 更新时间:2023-12-04 20:10:04 25 4
gpt4 key购买 nike

我在用户窗体上有一个列表框,它有一个源范围,我试图通过提供来自用户窗体的值来覆盖它,但是一旦我覆盖特定单元格,事件 ListBox1_Click()启动这是不可取的,因为它会重新填充用户窗体上的数据。

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 个文本框来查看/修改数据,但是您还有多列列表框来查看和滚动表格格式的数据,当我向上或向下滚动时,我会在其中获取特定的行数据用户窗体上的文本框,我还有一个按钮,上面写着“覆盖”,以防我想通过用户窗体修改工作表上的数据。但是一旦它修改了工作表中的一个单元格,就会触发“Listbox1_click”事件并覆盖用户窗体上的数据。

最佳答案

Application.EnableEvents = false won't affect UserForms .如果事件被禁用,您必须创建一个属性并在事件开始和退出事件子时检查它的值,例如:

' 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

关于excel - 覆盖范围内的值,该范围是列表框的源范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52902292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com