【问题标题】:Setting textbox values in VBA for excel in a UserForm only works on every other attempt在 VBA 中为用户窗体中的 excel 设置文本框值仅适用于其他所有尝试
【发布时间】:2020-01-01 22:35:59
【问题描述】:

我目前正在构建一个宏,当您 dobbeltclick 一个单元格时,它会打开一个带有文本框的用户窗体。然后,我使用您单击的单元格所在行中的数据填充文本框。

我的问题是: 当我 dobbeltclick 第 4 行中的单元格时,它会打开一个带有空白文本框的用户窗体。 如果我然后 dobbeltclick 该行中的另一个单元格,它将打开用户窗体,其中单元格已正确填写。

然后,如果我 dobbeltclick 另一行中的一个单元格,它会打开包含前一行数据的用户窗体。

同样,如果我 dobbeltclick 同一行中的另一个单元格,它会正确打开。

我的代码如下:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

        If Intersect(Target, Range("N1:N200", "B3:O200")) Is Nothing Then Exit Sub

        Selection.Select
        currentRow = Selection.Row


        redigerskadeForm.Show

        redigerskadeForm.SagsNrTextbox.value = ActiveSheet.Range("B" & currentRow).value
        redigerskadeForm.referenceTextbox.value = ActiveSheet.Range("C" & currentRow).value
        redigerskadeForm.sagstypeComboBox.value = ActiveSheet.Range("D" & currentRow).value
        redigerskadeForm.adresseTextbox.value = ActiveSheet.Range("E" & currentRow).value
        redigerskadeForm.postTextbox.value = ActiveSheet.Range("F" & currentRow).value
        redigerskadeForm.kontaktTextBox.value = ActiveSheet.Range("G" & currentRow).value
        redigerskadeForm.tlf1Textbox.value = ActiveSheet.Range("H" & currentRow).value
        redigerskadeForm.tlf2Textbox.value = ActiveSheet.Range("I" & currentRow).value
        redigerskadeForm.mailTextbox.value = ActiveSheet.Range("J" & currentRow).value
        redigerskadeForm.ansvarComboBox.value = ActiveSheet.Range("K" & currentRow).value
        redigerskadeForm.opDatoTextbox.value = ActiveSheet.Range("L" & currentRow).value
        redigerskadeForm.foDatoTextbox.value = ActiveSheet.Range("M" & currentRow).value
        redigerskadeForm.statusComboBox.value = ActiveSheet.Range("N" & currentRow).value
        redigerskadeForm.noteTextbox.value = ActiveSheet.Range("o" & currentRow).value



End Sub

【问题讨论】:

  • Selection.select 有什么用处?在工作表事件中,您实际上不需要参考Activesheetuserformsdefault instanceUserform1.show 的阅读建议。
  • 原因是 onUserforn.Showcode 事件的执行被暂停,直到您隐藏/销毁表单。然后将值分配给用户窗体 8 上的控件(如果仍然存在)。所以在第一次执行时,用户窗体打开ang得到显示,但没有写入数据。直到它隐藏。所以你在第二次执行时看到第一次的值,等等。添加Unload Userform after the show(但只是为了测试)什么销毁实例,您将因此没有关于所有执行的数据..

标签: excel vba


【解决方案1】:

代码需要在打开表单之前更改值,如下所示:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

        If Intersect(Target, Range("N1:N200", "B3:O200")) Is Nothing Then Exit Sub

        Selection.Select
        currentRow = Selection.Row

        redigerskadeForm.SagsNrTextbox.value = ActiveSheet.Range("B" & currentRow).value
        redigerskadeForm.referenceTextbox.value = ActiveSheet.Range("C" & currentRow).value
        redigerskadeForm.sagstypeComboBox.value = ActiveSheet.Range("D" & currentRow).value
        redigerskadeForm.adresseTextbox.value = ActiveSheet.Range("E" & currentRow).value
        redigerskadeForm.postTextbox.value = ActiveSheet.Range("F" & currentRow).value
        redigerskadeForm.kontaktTextBox.value = ActiveSheet.Range("G" & currentRow).value
        redigerskadeForm.tlf1Textbox.value = ActiveSheet.Range("H" & currentRow).value
        redigerskadeForm.tlf2Textbox.value = ActiveSheet.Range("I" & currentRow).value
        redigerskadeForm.mailTextbox.value = ActiveSheet.Range("J" & currentRow).value
        redigerskadeForm.ansvarComboBox.value = ActiveSheet.Range("K" & currentRow).value
        redigerskadeForm.opDatoTextbox.value = ActiveSheet.Range("L" & currentRow).value
        redigerskadeForm.foDatoTextbox.value = ActiveSheet.Range("M" & currentRow).value
        redigerskadeForm.statusComboBox.value = ActiveSheet.Range("N" & currentRow).value
        redigerskadeForm.noteTextbox.value = ActiveSheet.Range("o" & currentRow).value

        redigerskadeForm.Show

End Sub

【讨论】:

  • IMO 即使您使用用户表单的默认实例,也应使用 WithEnd With
  • 为了保持 DRY(不要重复自己),您应该使用循环来设置控件并遍历其中的列/控件。这需要将控件分配给列,这可以通过多种方式完成(例如,控件名称包含作为 fu##suffix 的列或使用标签或数组)。为了避免代码中不必要的暂停,请考虑使用来自用户窗体的代码,而不是工作表。
猜你喜欢
  • 2013-08-08
  • 1970-01-01
  • 2016-12-31
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2022-07-02
  • 1970-01-01
相关资源
最近更新 更多