Excel VBA用户窗体文本框控件

时间:2019-02-07 04:42:07

标签: excel vba

我有一个多页用户表单,但是我无法获取表单中更新的数据以在excel中更新。无论我在表单中编辑哪一行,我只有的代码都会更新第2行。这是我正在使用的代码。

Dim currentrow As Long

Private Sub btnClose_Click()
  Unload Me
End Sub

Private Sub frmDrgReg_Initialize()
        Dim ws As Worksheet
        Set ws = Worksheets(1)
        Call resetForm

        currentrow = 2
        Me.txtRev = Cells(currentrow, 10)
        Me.txtScale = Cells(currentrow, 11)
        Me.txtProjDesc = Cells(currentrow, 12)
       Me.txtFacDesc = Cells(currentrow, 13)
       Me.txtItemDesc = Cells(currentrow, 14)
       Me.txtType = Cells(currentrow, 15)


End Sub

Private Sub btnApply_Click()
       answer = MsgBox("you are about to apply the changes", vbYesNo + vbQuestion, "Apply Changes")
       If answer = vbYes Then
       currentrow = 2
       Cells(currentrow, 10) = Me.txtRev.Text
       Cells(currentrow, 11) = Me.txtScale.Text
       Cells(currentrow, 12) = txtProjDesc.Text
       Cells(currentrow, 13) = txtFacDesc.Text
       Cells(currentrow, 14) = txtItemDesc.Text
       Cells(currentrow, 15) = txtType.Text
       End If
End Sub

Private Sub ComboBox1_DropButtonClick()
        Dim i As Long, LastRow As Long
        LastRow = Sheets(1).Range("H" & Rows.Count).End(xlUp).Row
        If Me.ComboBox1.ListCount = 0 Then
        For i = 2 To LastRow
        Me.ComboBox1.AddItem Sheets(1).Cells(i, "H").Value
        Next i
        End If

End Sub

Sub btnApply_Click()中,如何使用For Each循环,该循环类似于我在resetForm子程序中使用的循环

Public Sub resetForm()
        Dim ctl As Control
        For Each ctl In frmDrgReg.Controls
                If TypeName(ctl) = "TextBox" Then
                        ctl Value = ""
                End If
        Next ctl

        frmDrgReg.ComboBox1.SetFocus
End Sub

0 个答案:

没有答案