Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在用户窗体中的VBA for excel中设置文本框值仅在每次尝试时有效_Excel_Vba - Fatal编程技术网

在用户窗体中的VBA for excel中设置文本框值仅在每次尝试时有效

在用户窗体中的VBA for excel中设置文本框值仅在每次尝试时有效,excel,vba,Excel,Vba,我目前正在构建一个宏,当你点击一个单元格时,它会打开一个带有文本框的用户表单。然后,我用您单击的单元格所在行的数据填充文本框 我的问题是: 当我点击第4行的单元格时,会打开一个文本框为空的用户表单。 如果我单击该行中的另一个单元格,它将打开UserForm,并正确填写单元格 然后,如果我单击另一行中的一个单元格,它将打开带有前一行数据的userform 同样,如果我单击同一行中的另一个单元格,它将正确打开 我的代码如下: Private Sub Worksheet_BeforeDoubleCli

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

我的问题是: 当我点击第4行的单元格时,会打开一个文本框为空的用户表单。 如果我单击该行中的另一个单元格,它将打开UserForm,并正确填写单元格

然后,如果我单击另一行中的一个单元格,它将打开带有前一行数据的userform

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

我的代码如下:

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

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

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

什么是
选择。选择
好用?在工作表事件中,您实际上不需要参考
Activesheet
。和的阅读建议。原因是,在
Userforn.Show
上,事件的代码执行会暂停,直到您隐藏/销毁表单。然后将这些值分配给userform(如果仍然存在)上的控件。因此,在第一次执行时,userform打开并显示EDM,但没有写入任何数据。直到它隐藏为止。因此,您可以在第二次执行时看到第一次的值,依此类推。在显示后添加
卸载userform(但仅用于测试)什么会破坏实例,因此所有执行都不会有数据..IMO即使使用默认的userform实例,也应该使用
With
End With
。要使kep-DRY(不要重复自己),应该使用循环来设置控件并遍历其中的列/控件。这需要将控制权分配给列,可以通过几种方式完成(例如,控制权名称将列作为fu###后缀或使用标记或数组)。。为了避免代码中不必要的暂停,请考虑使用用户窗体中的CUDE,而不是Tabl。