相同的代码不能使用新参数

时间:2014-08-26 22:04:49

标签: excel-vba excel-2013 vba excel

如果相关的水费和灯光费用延迟一个月或更长时间,我正试图将此代码粘贴到House#上。我有完全相同的代码与同一工作簿中的另一组相似数据运行完全相同(虽然不同的工作表),但我得到"运行时错误' 1004':应用程序定义或对象 - 定义错误"当我运行它时在指示的行上。为什么这些代码在这些情况下不起作用?

'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

修改

现在代码没有给我一个错误,但它没有从IF语句中的行给我任何结果

2 个答案:

答案 0 :(得分:1)

在你的评论中,你说细胞&#34; C3&#34; = 26-08-2014&#34; C4&#34;空白&#34; C5&#34; 19-08-2014。所以...当你运行你的宏时,它当然会跳过IF代码,因为月份是8月,它不满足IF条件。

尝试使用代码提取,但日期为&#34; C3&#34; = 26-06-2014,&#34; C4&#34; = 26-05-2014,&#34; C5&#34; = 26-03-2014。我想你会发现它有效。

答案 1 :(得分:0)

错误在此特定细分中:

Month(Worksheets("Proxima Lectura").Range(colNameAgua & aguaNumb).Value) < myDate - 1

您需要做的就是将myDate的声明更改为

Dim myDate as Integer

问题是你是从字符串中减去的(myDate在开头是一个字符串)。