Home » excel » vba – Outputting to two worksheets

vba – Outputting to two worksheets

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have the below function that outputs records to either a worksheet called CI or one called Error. I added an additional IF statement where if my source ‘col’ column contains the word “TITER” then I want it to output to the “Error” worksheet. This seems to be working and outputting the appropriate records to the Error tab. However I noticed that it is also outputting these same records to the “CI” worksheet as well. I have the IF code nested in the main Else statement, but I’m thinking it doesn’t belong there. Any help is appreciated!

Public lstrow As Long, strDate As Variant, stredate As Variant
Sub importbuild()
lstrow = Worksheets("Data").Range("G" & Rows.Count).End(xlUp).Row

Function DateOnlyLoad(col As String, col2 As String, colcode As String)
    Dim i As Long, j As Long, k As Long
    j = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
    k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1

    For i = 2 To lstrow
        strDate = spacedate(Worksheets("Data").Range(col & i).Value)
        stredate = spacedate(Worksheets("Data").Range(col2 & i).Value)

        If (Len(strDate) = 0 And (col2 = "NA" Or Len(stredate) = 0)) Or InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "EXP") > 0 Then
            GoTo EmptyRange
        Else
            If InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "TITER") > 0 Then
                Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
                Worksheets("Error").Range("D" & k).Value = "REVIEW MMR1 DATES"
                k = k + 1
            End If
            Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
            Worksheets("CI").Range("D" & j).Value = colcode
            Worksheets("CI").Range("E" & j).Value = datecleanup(strDate)
            Worksheets("CI").Range("L" & j).Value = dateclean(strDate)
            Worksheets("CI").Range("M" & j).Value = strDate
            If col2 <> "NA" Then
                If IsEmpty(stredate) = False Then
                    Worksheets("CI").Range("F" & j).Value = datecleanup(stredate)
                End If
            End If
            j = j + 1
        End If
EmptyRange:
    Next i
End Function
How to&Answers:

Please review and compare to original code. You can see the quick change made. Indentation helps so much to spot errors and/or opportunities to improve the code.

Function DateOnlyLoad(col As String, col2 As String, colcode As String)
    Dim i As Long, j As Long, k As Long
    j = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
    k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1

    For i = 2 To lstrow
        strDate = spacedate(Worksheets("Data").Range(col & i).Value)
        stredate = spacedate(Worksheets("Data").Range(col2 & i).Value)

        If (Len(strDate) = 0 And (col2 = "NA" Or Len(stredate) = 0)) Or InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "EXP") > 0 Then
            GoTo EmptyRange
        Else
            If InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "TITER") > 0 Then
                Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
                Worksheets("Error").Range("D" & k).Value = "REVIEW MMR1 DATES"
                k = k + 1
            Else
                Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
                Worksheets("CI").Range("D" & j).Value = colcode
                Worksheets("CI").Range("E" & j).Value = datecleanup(strDate)
                Worksheets("CI").Range("L" & j).Value = dateclean(strDate)
                Worksheets("CI").Range("M" & j).Value = strDate
                If col2 <> "NA" Then
                    If IsEmpty(stredate) = False Then
                        Worksheets("CI").Range("F" & j).Value = datecleanup(stredate)
                    End If
                End If
                j = j + 1
            End If
        End If
EmptyRange:
    Next i
End Function