简体   繁体   English

Excel Vba:循环用户窗体控件以计数和识别空文本框

[英]Excel Vba: Looping through userform controls to count and identify empty textboxes

I might not express the problem as I should but pls bare with me, here it goes我可能不会像我应该的那样表达这个问题,但请告诉我,就这样吧

I'm trying to create a user data entry form to capture services per patient and save entry to my spreadsheet (database)我正在尝试创建一个用户数据输入表单来捕获每个患者的服务并将条目保存到我的电子表格(数据库)

The patient details gets populated automatically without user entry (prior to a pre-entry form, created in the app)无需用户输入即可自动填充患者详细信息(在应用程序中创建的预输入表单之前)

In the database, There's a single column to capture every service entered在数据库中,有一个列来捕获输入的每个服务

So I decided that when a user enters multiple services and clicks on save所以我决定当用户输入多个服务并点击保存时

The spreadsheet (database) will capture the patient details per every service电子表格(数据库)将捕获每个服务的患者详细信息

That is, on one row .... there will be patient detail and then a single service也就是说,在一行上......会有耐心的细节,然后是单一的服务

On the second row... the details will repeat but the service will be changed based on the second service provided by the user在第二行...详细信息将重复,但服务将根据用户提供的第二个服务进行更改

And so on等等

Problem问题

(I don't know how to loop through the empty textboxes to identify the number of rows patient details will be repeated when user clicks save. (我不知道如何遍历空文本框以识别当用户单击保存时将重复患者详细信息的行数。

If I get that, then I'll work my way to populate the services by each count of rows)如果我明白了,那么我将按照每个行数来填充服务)

Here's the code:这是代码:

Ps: returned an error on "for each ccont In Me.Controls" Ps:在“for each ccont In Me.Controls”上返回错误

Private Sub Save_Click()



Dim zh As Worksheet

Dim LaastRow As Long

Dim x As Integer

Dim ccont As Controls

Dim y As Long



Set zh = ThisWorkbook.Sheets("PublicDatabase")

LaastRow = [Counta(PublicDatabase!A:A)] + 1



For Each ccont In Me.Controls

If Me.txt_QtyAccomodation.Value = "" Or _

Me.txt_QtyConsultation.Value = "" Or _

Me.txt_QtyHaematology.Value = "" Or _

Me.txt_QtyAccomodation.Value = "" Or _

Me.txt_QtyHistopathology.Value = "" Or _

Me.txt_QtyNursingcare.Value = "" Or _

Me.txt_QtyOthers1.Value = "" Or _

Me.txt_QtyOthers2.Value = "" Or _

Me.txt_QtyMicrobiology.Value = "" Or _

Me.txt_QtyReviews.Value = "" Then

Exit For



Else



y = y + 1



End If



For x = 1 To LaastRow + y



With zh

.Cells(LaastRow, 1) = LaastRow - 1

.Cells(LaastRow, 2) = "DEFENCE HEALTH MAINTENANCE LTD (PUBLIC)"

.Cells(LaastRow, 3) = PubDefense.Txt_NameOfpDEFENSE.Value

.Cells(LaastRow, 4) = "Nil"

.Cells(LaastRow, 5) = PubDefense.Txt_RefferrinProviderDefense.Value

.Cells(LaastRow, 6) = PubDefense.Txt_NHISNoDefense.Value

.Cells(LaastRow, 7) = PubDefense.Txt_AuthorizCodeDefense.Value

.Cells(LaastRow, 8) = PubDefense.txt_HmoCodeDefense.Value

.Cells(LaastRow, 9) = PubDefense.Txt_DateOftreatmentDefense.Value

.Cells(LaastRow, 10) = PubDefense.Txt_DateOFadmDefense.Value

.Cells(LaastRow, 11) = PubDefense.Txt_DateOFdisDefense.Value

.Cells(LaastRow, 12) = "Nil"

.Cells(LaastRow, 13) = "Nil"

.Cells(LaastRow, 14) = "Nil"

.Cells(LaastRow, 15) = "Nil"

.Cells(LaastRow, 16) = "Nil"

.Cells(LaastRow, 17) = PubDefense.Txt_DiagnosisDefense.Value

.Cells(LaastRow, 18) = PubDefense.Txt_PatAddressDEfense.Value



End With



Next x

Next ccont



MsgBox "entered"



End Sub

Loop through an array of the text box names checking values that are not empty.循环遍历文本框名称数组,检查非空值。

Private Sub CommandButton1_Click()

    Dim dept
    Dim zh As Worksheet, LastRow As Long, i 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")
        
    Dim ccont As Control
    For i = 0 To UBound(dept)
        Set ccont = Me.Controls("Txt_Qty" & dept(i))
        If ccont.Value <> "" Then
            zh.Cells(LastRow, 1).Resize(, 18) = PatientDetails(LastRow)
            LastRow = LastRow + 1
        End If
    Next
    MsgBox "Entered", vbInformation
End Sub

Private Function PatientDetails(n As Long) As Variant
    Dim ar(1 To 1, 1 To 18)
    'With PubDefense
        ar(1, 1) = n - 1
        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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM