首页 > 解决方案 > 如何用 i=1 到 12 的循环重写这个子,而 'txt_Case_Number(i).Text' 有一个值?

问题描述

我创建了一个搜索用户表单,需要在搜索可能包含部分重复信息的案例编号下显示记录。然后我对该记录进行更改并使用新信息更新工作表。我正在尝试做这样的事情:

While i = 1 to 12
    .Cells(RowFound(i), "A") = txt_Case_Number(i).Text
    .Cells(RowFound(i), "B") = cmboCase_Status(i).Text

下面是我所拥有的,并希望使它开始在上面显示。

Private Sub cmdMakeEdit1_Click()        
    With Sheets("Sheet1")
        .Cells(RowFound1, "A") = txt_Case_Number1.Text
        .Cells(RowFound1, "B") = cmboCase_Status1.Text
        .Cells(RowFound1, "C") = txtDate_of_Seizure1.Text
        .Cells(RowFound1, "D") = txtSeizure_Type1.Text
        .Cells(RowFound1, "L") = TxtCurr1.Text
        .Cells(RowFound1, "N") = TxtCurr_Dep1.Text
        .Cells(RowFound1, "S") = txtPDH_CourtDate1.Text
        .Cells(RowFound1, "U") = txtDays_PDH_Courtdate1.Text
        .Cells(RowFound1, "V") = txtPC_Received1.Text
        .Cells(RowFound1, "W") = txtDays_464_Filing1.Text
        .Cells(RowFound1, "Y") = txt464_Filed1.Text
        .Cells(RowFound1, "Z") = txtDays_Article36_Filing1.Text
        .Cells(RowFound1, "AA") = txtArticle_36_Due1.Text
        .Cells(RowFound1, "AB") = txtArticle_36_Filed1.Text
        .Cells(RowFound1, "AC") = txtDeclaration_Letter1.Text
        .Cells(RowFound1, "AE") = txtCurr_Sent_ISP1.Text
        .Cells(RowFound1, "AF") = txtAmount_Sent1.Text
        .Cells(RowFound1, "AG") = txtYear1.Text
        .Cells(RowFound1, "AH") = cmbo_Make1.Text
        .Cells(RowFound1, "AI") = cmbo_Model1.Text
        .Cells(RowFound1, "AJ") = cmbo_Color1.Text
        .Cells(RowFound1, "AK") = txtPlate1.Text
        .Cells(RowFound1, "AL") = txtVIN1.Text
        .Cells(RowFound1, "AM") = txtLEADS1.Text
        .Cells(RowFound1, "AN") = txtOwners_Name1.Text
        .Cells(RowFound1, "AQ") = txtLien1.Text
        .Cells(RowFound1, "AT") = cmbLocation1.Text
        .Cells(RowFound1, "AU") = txtFull_Name1.Text
        .Cells(RowFound1, "AV") = txtItem_Desc1.Text
        .Cells(RowFound1, "AW") = txtItem_Model1.Text
        .Cells(RowFound1, "AX") = txtItem_Serial1.Text
        .Cells(RowFound1, "AY") = txtOther_Info1.Text
        .Cells(RowFound1, "AZ") = txtItem_PickedUP1.Text
        .Cells(RowFound1, "BA") = txt_Search1_Type.Text

标签: excelvba

解决方案


循环表单控件元素的唯一方法之一是初始化控件并在For-Each循环中使用它。然后您可以在 If 语句中验证 Control 的名称或类型,例如

Dim ctl As Control

For Each ctl in Me.Controls '<-assuming your button is on the form the rest of the controls are
    If TypeName(ctl) = "TextBox" or TypeName(ctl) = "ComboBox" Then
        'Do Stuff'
    End If
Next ctl

推荐阅读