mercredi 1 août 2018

error 1004 ? if else reasonable to use

I have a macro-enabled worksheet running in excel 2016. I have four sheets "Orders", "Intake", "Hard Data", "Office Visit".

I have created a userform which has a command button (CommandButtonAbsorbIntakeData). I have pasted the code below in the hopes that someone can spot any issues I have that would produce a error 1004. I understand the error to indicate there is no Match found. I am using an if else statement as a trigger since the match function returns a value. If the value is greater that or equal to 1, a piece of code will run. If it is anything other than that, another code will run. My issue is that when a match IS NOT found, my else code does not run. Is if else not a good option with match? Thanks for any feedback.

    Option Explicit
    Public Sub CommandButtonAbsorbIntakeData_Click()
    Dim lrowintakes As Variant
    Dim patient As Variant
    Dim myworksheet As Worksheet
    Dim myworksheet2 As Worksheet
    Dim myworksheet3 As Worksheet
    Dim myfirstblankrow As Variant
    Dim patientexistence As Variant


    patient = Me.TextBoxIntakeName.Value & " " &  Me.TextBoxIntakeDateOfBirth.Value

    lrowintakes = Application.WorksheetFunction.Match(patient, Sheets("Hard Data").Range("A1:A200"), 0)

           If lrowintakes >= 1 Then
        Set myworksheet = Worksheets("Hard Data")
            With myworksheet
                .Cells(lrowintakes, 1) = Me.TextBoxIntakeName.Value & " " & Me.TextBoxIntakeDateOfBirth.Value
                .Cells(lrowintakes, 2) = Me.TextBoxIntakeName.Value
                .Cells(lrowintakes, 3) = Me.TextBoxIntakeDateOfBirth.Value
                .Cells(lrowintakes, 14) = Me.TextBoxIntakeMeds1.Value
                .Cells(lrowintakes, 15) = Me.TextBoxIntakeMeds2.Value
                .Cells(lrowintakes, 16) = Me.TextBoxIntakeMeds3.Value
                .Cells(lrowintakes, 17) = Me.TextBoxIntakeMeds4.Value
                .Cells(lrowintakes, 18) = Me.TextBoxIntakeMeds5.Value
                .Cells(lrowintakes, 19) = Me.TextBoxIntakeMeds6.Value
                .Cells(lrowintakes, 20) = Me.TextBoxIntakeMeds7.Value
                .Cells(lrowintakes, 21) = Me.TextBoxIntakeMeds8.Value
                .Cells(lrowintakes, 22) = Me.TextBoxIntakeMeds9.Value
                .Cells(lrowintakes, 23) = Me.TextBoxIntakeMedicalProblems1.Value
                .Cells(lrowintakes, 24) = Me.TextBoxIntakeMedicalProblems2.Value
                .Cells(lrowintakes, 25) = Me.TextBoxIntakeMedicalProblems3.Value
                .Cells(lrowintakes, 26) = Me.TextBoxIntakeMedicalProblems4.Value
                .Cells(lrowintakes, 27) = Me.TextBoxIntakeMedicalProblems5.Value
                .Cells(lrowintakes, 28) = Me.TextBoxIntakeMedicalProblems6.Value
                .Cells(lrowintakes, 29) = Me.TextBoxIntakeMedicalProblems7.Value
                .Cells(lrowintakes, 30) = Me.TextBoxIntakeMedicalProblems8.Value
                .Cells(lrowintakes, 31) = Me.TextBoxIntakeMedicalProblems9.Value
                .Cells(lrowintakes, 32) = Me.TextBoxIntakeSurgeries1.Value
                .Cells(lrowintakes, 33) = Me.TextBoxIntakeSurgeries2.Value
                .Cells(lrowintakes, 34) = Me.TextBoxIntakeSurgeries3.Value
                .Cells(lrowintakes, 35) = Me.TextBoxIntakeSurgeries4.Value
                .Cells(lrowintakes, 36) = Me.TextBoxIntakeSurgeries5.Value
                .Cells(lrowintakes, 37) = Me.TextBoxIntakeSurgeries6.Value
                .Cells(lrowintakes, 38) = Me.TextBoxIntakeSurgeries7.Value
                .Cells(lrowintakes, 39) = Me.TextBoxIntakeSurgeries8.Value
                .Cells(lrowintakes, 40) = Me.TextBoxIntakeSurgeries9.Value
                .Cells(lrowintakes, 41) = Me.TextBoxIntakeFamilyHistory
            End With

    Else
    'add information in next available row in Hard Data Sheet, concatenates name and DOB for datatag column
        Set myworksheet = Worksheets("Hard Data")
        With myworksheet
            myfirstblankrow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
            .Cells(myfirstblankrow, 1) = Me.TextBoxIntakeName.Value & " " & Me.TextBoxIntakeDateOfBirth.Value
            .Cells(myfirstblankrow, 2) = Me.TextBoxIntakeName.Value
            .Cells(myfirstblankrow, 3) = Me.TextBoxIntakeDateOfBirth.Value
            .Cells(myfirstblankrow, 14) = Me.TextBoxIntakeMeds1.Value
            .Cells(myfirstblankrow, 15) = Me.TextBoxIntakeMeds2.Value
            .Cells(myfirstblankrow, 16) = Me.TextBoxIntakeMeds3.Value
            .Cells(myfirstblankrow, 17) = Me.TextBoxIntakeMeds4.Value
            .Cells(myfirstblankrow, 18) = Me.TextBoxIntakeMeds5.Value
            .Cells(myfirstblankrow, 19) = Me.TextBoxIntakeMeds6.Value
            .Cells(myfirstblankrow, 20) = Me.TextBoxIntakeMeds7.Value
            .Cells(myfirstblankrow, 21) = Me.TextBoxIntakeMeds8.Value
            .Cells(myfirstblankrow, 22) = Me.TextBoxIntakeMeds9.Value
            .Cells(myfirstblankrow, 23) = Me.TextBoxIntakeMedicalProblems1.Value
            .Cells(myfirstblankrow, 24) = Me.TextBoxIntakeMedicalProblems2.Value
            .Cells(myfirstblankrow, 25) = Me.TextBoxIntakeMedicalProblems3.Value
            .Cells(myfirstblankrow, 26) = Me.TextBoxIntakeMedicalProblems4.Value
            .Cells(myfirstblankrow, 27) = Me.TextBoxIntakeMedicalProblems5.Value
            .Cells(myfirstblankrow, 28) = Me.TextBoxIntakeMedicalProblems6.Value
            .Cells(myfirstblankrow, 29) = Me.TextBoxIntakeMedicalProblems7.Value
            .Cells(myfirstblankrow, 30) = Me.TextBoxIntakeMedicalProblems8.Value
            .Cells(myfirstblankrow, 31) = Me.TextBoxIntakeMedicalProblems9.Value
            .Cells(myfirstblankrow, 32) = Me.TextBoxIntakeSurgeries1.Value
            .Cells(myfirstblankrow, 33) = Me.TextBoxIntakeSurgeries2.Value
            .Cells(myfirstblankrow, 34) = Me.TextBoxIntakeSurgeries3.Value
            .Cells(myfirstblankrow, 35) = Me.TextBoxIntakeSurgeries4.Value
            .Cells(myfirstblankrow, 36) = Me.TextBoxIntakeSurgeries5.Value
            .Cells(myfirstblankrow, 37) = Me.TextBoxIntakeSurgeries6.Value
            .Cells(myfirstblankrow, 38) = Me.TextBoxIntakeSurgeries7.Value
            .Cells(myfirstblankrow, 39) = Me.TextBoxIntakeSurgeries8.Value
            .Cells(myfirstblankrow, 40) = Me.TextBoxIntakeSurgeries9.Value
            .Cells(myfirstblankrow, 41) = Me.TextBoxIntakeFamilyHistory.Value
        End With
        'add datatag to OFFICE VISITS
        Set myworksheet2 = Worksheets("Office Visits")
            With myworksheet2
                myfirstblankrow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                .Cells(myfirstblankrow, 1).Value = Me.TextBoxIntakeName & " " & Me.TextBoxIntakeDateOfBirth
            End With
        'add datatag to ORDERS
        Set myworksheet3 = Worksheets("Orders")
            With myworksheet3
                myfirstblankrow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                .Cells(myfirstblankrow, 1).Value = Me.TextBoxIntakeName & " " & Me.TextBoxIntakeDateOfBirth
            End With

    End If


    End Sub

Aucun commentaire:

Enregistrer un commentaire