Search code examples
excelexcel-2013vba

Same code not working with new parameters


I am trying to get this code to paste the House # if the associated water and light bills are one month late or more. I have this exact same code running perfectly fine with another set of similar data in the same workbook (though different sheets), but I get "Run.time error '1004': Application-defined or object-defined error" on the indicated line when I run it. Why does this code not work in these circumstances?

'General Variables
    Dim myDate As Integer
    Dim getDate As String
    Dim colPaste As String
    Dim colPensionNumb As String
    Dim colPaste3 As String
    Dim colPaste2 As String
    Dim colCelular As String
    Dim colPaste4 As String
    Dim colPaste5 As String

    'Agua Variables
    Dim aguaNumb As Integer
    Dim aguaNumb2 As Integer
    Dim colNameAgua As String
    Dim colPasteA As String

    'Luz Variables
    Dim luzNumb As Integer
    Dim luzNumb2 As Integer
    Dim colNameLuz As String
    Dim colPasteL As String

    'General Values
    colPaste = "A"
    colCelular = "G"
    colPensionNumb = "H"
    myDate = Month(Date)
    colPaste3 = "E"
    colPaste4 = "F"
    colPaste5 = "G"

    'Agua Values
    colNameAgua = "C"
    colPasteA = "B"

    'Luz Values
    colNameLuz = "D"
    colPasteL = "C"


    'Agua
     For aguaNumb = 3 To 5
        If Month(Worksheets("Proxima Lectura").Range(colNameAgua & aguaNumb).Value) < myDate - 1 Then
            ' ---------THE FOLLOWING LINE IS WHERE THE ERROR OCCURS------------
            Worksheets("Title Page").Range(colPasteA & aguaNumb).Value = Worksheets("Proxima Lectura").Range(colNameAgua & aguaNumb).Value
        End If
     Next aguaNumb

EDIT

Now the code doesn't give me an error, but it's not giving me any results from the line within the IF statement


Solution

  • In your comments you say that cells "C3" = 26-08-2014 "C4" blank "C5" 19-08-2014. So...when you run your macro, of course it will skip the IF code, because the month is August which does not satisfy the IF condition.

    Try your code extract but with dates in "C3" = 26-06-2014, "C4" = 26-05-2014, "C5" = 26-03-2014. I think you will find it works.