【问题标题】:Excel Vba: Looping through userform controls to count and identify empty textboxesExcel Vba:循环通过用户窗体控件来计数和识别空文本框
【发布时间】:2021-12-05 18:35:10
【问题描述】:

@cdp1802 这是我尝试过的 哪个是错误的

我添加了其他部门并尝试查看将其他控件的值存储在数组中的可能性....显然不是最好的方法

主要是用户需要在指定数量之前输入服务,然后有一个代码已经将服务的费率和指定的数量相乘 但在其他情况下要在数据库中捕获它......基于每个“Txt_Qty”条目 Txt_qtyAccomodation...有一个名为“txt_accomodation”的服务条目文本框 Txt_qtyHaematology... 有一个名为“cmb_haematology”的服务条目文本框 以此类推

我还需要记录数量和成本 ....

私有子命令按钮1_Click()

Dim dept, details, service, Quantity, Cost, i As Long
Dim zh As Worksheet, LastRow As Long

Set zh = ThisWorkbook.Sheets("PublicDatabase")
LastRow = zh.Cells(Rows.Count, "A").End(xlUp).Row + 1


dept = Array("Accomodation", "Consultation", "Haematology", _
    "Histopathology", "Nursingcare", "Others1", _
    "Others2", "Microbiology", "Reviews", "Radiology1", "Radiology2", "Radiology3", "Pharmacy1", _
    "Pharmacy2", "Pharmacy3", "Phamacy4", "Pharmacy5", "Pharmacy6")
    
service = Array(Me.txt_initialconsultation.Value, Me.txt_Review.Value, Me.txt_nursingCare.Value, _
Me.txt_Accomodation.Value, Me.cmb_haematology.Value, Me.cmb_histopathology.Value, Me.cmb_Microbiology, _
Me.cmb_Other1.Value, Me.cmb_other2.Value, Me.Cmb_Radiology1.Value, Me.Cmb_Radiology2.Value, _
Me.Cmb_radiology3.Value, Me.Cmb_Pharmacy1.Value, Me.Cmb_Pharmacy2.Value, Me.Cmb_Pharmacy3.Value, _
Me.Cmb_Pharmacy4.Value, Me.Cmb_Pharmacy5.Value, Me.Cmb_Pharmacy6.Value)

Quantity = Array(Me.txt_QtyAccomodation.Value, Me.txt_QtyConsultation.Value, Me.txt_QtyHaematology.Value, _
Me.txt_QtyHistopathology.Value, Me.txt_QtyMicrobiology.Value, Me.txt_QtyNursingcare.Value, Me.txt_QtyOthers1.Value, _
Me.txt_QtyOthers2.Value, Me.txt_QtyPhamacy4.Value, Me.txt_QtyPharmacy1.Value, Me.txt_QtyPharmacy2.Value, Me.txt_QtyPharmacy3.Value, _
Me.txt_QtyPharmacy5.Value, Me.txt_QtyPharmacy6.Value, Me.txt_QtyRadiology1.Value, Me.txt_QtyRadiology2.Value, Me.txt_QtyRadiology3, _
Me.txt_QtyReviews.Value)

Cost = Array(Me.txt_costAccomodation.Value, Me.txt_CostCOnsultation.Value, Me.txt_CostHaematology.Value, _
Me.txt_CostHistopathology.Value, Me.txt_CostMicrobiology.Value, Me.txt_CostOthers1.Value, _
Me.txt_CostNursingCare.Value, Me.txt_CostOthers2.Value, Me.txt_CostPharmacy1.Value, Me.txt_CostPharmacy2.Value, _
Me.txt_CostPharmacy3.Value, Me.txt_CostPharmacy4.Value, Me.txt_CostPharmacy5.Value, Me.txt_CostPharmacy6.Value, _
Me.txt_CostRadiology1.Value, Me.txt_CostRadiology2.Value, Me.txt_CostRadiology3.Value)

   
details = PatientDetails()
   
Dim ccont As Control
For i = 0 To UBound(dept)
    Set ccont = Me.Controls("Txt_Qty" & dept(i))
    If ccont.Value <> "" Then
        
        details(1, 1) = LastRow - 1
        zh.Cells(LastRow, 1).Resize(, 18) = details
        zh.Cells(LastRow, 19) = service
        zh.Cells(LastRow, 20) = Quantity
        zh.Cells(LastRow, 22) = Cost
LastRow = LastRow + 1
    End If
Next
MsgBox "Entered", vbInformation

结束子

【问题讨论】:

  • 除了您的代码中使用的文本框之外,是否还有其他文本框,可能是空的?我的意思是,它应该被允许为空......如果所有的文本框都应该检查是否为空,为什么还要计算它们?
  • 应该是Dim ccont As Controlsingular
  • 计算其他中空文本框的数量以使用该值来知道行数患者详细信息将在数据库(电子表格)中重复......所以在大图中,当用户输入5种不同的服务在表格上...它可以根据输入的文本框的数量重复患者详细信息(意味着有服务)

标签: excel vba userform


【解决方案1】:

遍历文本框名称数组,检查不为空的值。

Update1 - 增加的服务和费用

Option Explicit
Private Sub CommandButton1_Click()

    Dim dept, details, i As Long
    Dim zh As Worksheet, LastRow As Long
    
    Set zh = ThisWorkbook.Sheets("PublicDatabase")
    LastRow = zh.Cells(Rows.Count, "A").End(xlUp).Row + 1

    dept = Array("Accomodation", "Consultation", "Nursingcare", _
         "Reviews", "Haematology", "Histopathology", _
         "Others1", "Others2", "Microbiology", _
         "Radiology1", "Radiology2", "Radiology3", _
         "Pharmacy1", "Pharmacy2", "Pharmacy3", _
         "Pharmacy4", "Pharmacy5", "Pharmacy6")
          
    details = PatientDetails()
       
    Dim qty As Control, cost As Control, service As Control
    For i = 0 To UBound(dept)
        Set qty = Me.Controls("Txt_Qty" & dept(i))
        Set cost = Me.Controls("Txt_Cost" & dept(i))
        Select Case Left(dept(i), 4)
            Case "Acco", "Cons", "Nurs", "Revi"
                Set service = Me.Controls("Txt_" & dept(i))
            Case Else
                Set service = Me.Controls("Cmb_" & dept(i))
        End Select

        If qty.Value <> "" Then
            details(1, 1) = LastRow - 1
            zh.Cells(LastRow, 1).Resize(, 18) = details
            zh.Cells(LastRow, 19) = service.Value
            zh.Cells(LastRow, 20) = qty.Value
            zh.Cells(LastRow, 22) = cost.Value
            LastRow = LastRow + 1
        End If
    Next
    MsgBox "Entered", vbInformation
End Sub

Private Function PatientDetails() As Variant
    Dim ar(1 To 1, 1 To 18)
    With PubDefense
        ar(1, 1) = 0
        ar(1, 2) = "DEFENCE HEALTH MAINTENANCE LTD (PUBLIC)"
        ar(1, 3) = .Txt_NameOfpDEFENSE.Value
        ar(1, 4) = "Nil"
        ar(1, 5) = .Txt_RefferrinProviderDefense.Value
        ar(1, 6) = .Txt_NHISNoDefense.Value
        ar(1, 7) = .Txt_AuthorizCodeDefense.Value
        ar(1, 8) = .Txt_HmoCodeDefense.Value
        ar(1, 9) = .Txt_DateOftreatmentDefense.Value
        ar(1, 10) = .Txt_DateOFadmDefense.Value
        ar(1, 11) = .Txt_DateOFdisDefense.Value
        ar(1, 12) = "Nil"
        ar(1, 13) = "Nil"
        ar(1, 14) = "Nil"
        ar(1, 15) = "Nil"
        ar(1, 16) = "Nil"
        ar(1, 17) = .Txt_DiagnosisDefense.Value
        ar(1, 18) = .Txt_PatAddressDEfense.Value
    End With
    PatientDetails = ar
End Function

【讨论】:

  • 这工作得无可挑剔......唯一剩下的就是当“txt_qty”重复时,服务输入、数量和服务率如何反映在数据库中!!那是在第 19、20 和 21 列中......因为在用户在 txt_qty 中输入一个值后,他/她必须提供服务......这将乘以输入的数量以获得服务成本......我如何将这个灌输到这段代码@cdp1802
  • @Israel 哪些控件有服务费率?
  • 实际上有超过 9 个“部门”,所以我添加了其他部门并尝试以某种方式存储成本、数量和服务控制..在 zh.cells 中使用的其他(LastRow,列)但显然我偏离了目标……
  • @Israel 你可以编辑你的问题,而不是把代码放在 cmets 中
  • 很抱歉,我会尽快处理
猜你喜欢
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2018-04-20
  • 2014-04-16
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多