【问题标题】:Same code not working with new parameters相同的代码不适用于新参数
【发布时间】:2014-08-26 22:04:49
【问题描述】:

如果相关的水费和电费账单延迟一个月或更长时间,我正在尝试让此代码粘贴房屋 #。我有这个完全相同的代码在同一个工作簿(尽管不同的工作表)中使用另一组类似的数据运行得非常好,但是我在指示的行上得到“Run.time error '1004': Application-defined or object-defined error”当我运行它时。为什么这段代码在这些情况下不起作用?

'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 语句中的行的任何结果

【问题讨论】:

  • 您使用的是什么版本的 Excel?
  • 我使用的是 Excel 2013
  • 我对那段代码没有任何错误。它必须是其他东西,无论是在您的工作表中,还是在另一本书中等。没有更多细节/屏幕截图等,很难/不可能说。
  • 除非我有 10 个代表点,否则 Stackoverflow 不会让我发布图片......但是,我添加了一些消息框,似乎我需要工作的行被跳过了。它绝对没有给我任何结果(a.k.a. 它不会改变任何它应该改变的信息)
  • 你能进入“Proxima Lectura”表中的单元格C3、C4、C5吗?进入单元格(通过双击)并复制列标签上方文本框中显示的所有内容?我预计这是某种日期,但需要确切了解您在这些单元格中的内容。

标签: excel excel-2013 vba


【解决方案1】:

在您的 cmets 中,您说单元格“C3”= 26-08-2014 “C4”空白“C5”19-08-2014。所以...当您运行宏时,它当然会跳过IF 代码,因为月份是八月,不满足IF 条件。

尝试您的代码提取,但日期为“C3”= 26-06-2014、“C4”= 26-05-2014、“C5”= 26-03-2014。我想你会发现它有效。

【讨论】:

  • 哇......我觉得每当有人指出我的错误时,他们总是那么明显......我想这就是学习和学习的重点,对吧?
【解决方案2】:

错误在这个特定的段:

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

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

Dim myDate as Integer

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

【讨论】:

  • 虽然是不好的做法,但 Excel VBA 2010 实际上会强制 myDate 为整数进行计算...这里没有错误。
  • IF语句内的行出现错误。好吧,真的,它似乎只是被跳过了。运行它不会导致任何变化。
  • 我想我已经解决了这个错误。您的单元格range(colNameAgua &amp; aguaNumb) 是否包含日期值?
  • 确实如此,但这就是我调用MONTH() 函数的原因。还有其他方法吗?
  • 为什么要从 mydate 中删除 1?
猜你喜欢
  • 2022-10-01
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 2014-01-25
  • 2021-08-15
  • 2021-08-09
相关资源
最近更新 更多