0

天,

我一直在寻找解决这个问题的方法,所以希望这里有人知道发生了什么!

到目前为止我得到的代码如下。我的理解是我需要将 i 和 n 定义为 Long,因为它们使用数组变体?目前,我在以下行后收到“运行时错误'1004':应用程序定义或对象定义错误”错误:

If Cells(i, m) < Cells(i, (m-1)) Then

我已经尝试用整数替换 i 和 m 变量,纯粹是为了测试目的,但问题仍然存在。了解我,我缩进了一些错误的东西:|

Option Explicit

Public Sub Show_Arrows()

Dim i As Integer
Dim n As Integer
Dim m As Integer

Dim Increasing() As Variant
Dim Decreasing() As Variant

Decreasing = Array(10, 11, 16, 17, 18, 19)

Increasing = Array(12, 13, 22, 25, 26, 31, 32, 36, 37, 38, 39, 40, 41, 44, 45, 46, 47, 50, 51, 52, 55, 58)

m = (Month(Date) - 4)

For i = LBound(Decreasing) To UBound(Decreasing)
    Cells(i, 16).Select
    If Cells(i, m) < Cells(i, (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic Programming\Arrows - Vanessa\Green.png").Select
    ElseIf Cells(i, m) > Cells(i, (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic Programming\Arrows - Vanessa\Red.png").Select
    End If
Next

For n = LBound(Increasing) To UBound(Increasing)
    Cells(n, 16).Select
    If Cells(n, m) > Cells(n, (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic     Programming\Arrows - Vanessa\Green.png").Select
    ElseIf Cells(n, m) < Cells(n, (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic Programming\Arrows - Vanessa\Red.png").Select
    End If
Next

End Sub
4

2 回答 2

0

LBound(Decreasing) 为 0。您不能选择索引为零的单元格。这就是为什么从 1 开始 For 循环或更改代码以避免索引为 0 的行的原因。试试这个代码:

Option Explicit
Public Sub Show_Arrows()

Dim i As Integer
Dim n As Integer
Dim m As Integer

Dim Increasing() As Variant
Dim Decreasing() As Variant

Decreasing = Array(10, 11, 16, 17, 18, 19)

Increasing = Array(12, 13, 22, 25, 26, 31, 32, 36, 37, 38, 39, 40, 41, 44, 45, 46, 47, 50, 51, 52, 55, 58)

m = (Month(Date) - 4)

For i = 1 To UBound(Decreasing) + 1
    Cells(Decreasing(i - 1), 16).Select
    If Cells(Decreasing(i - 1), m) < Cells(Decreasing(i - 1), (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic Programming\Arrows - Vanessa\Green.png").Select
    ElseIf Cells(Decreasing(i - 1), m) > Cells(Decreasing(i - 1), (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic Programming\Arrows - Vanessa\Red.png").Select
    End If
Next

For n = 1 To UBound(Increasing) + 1
    Cells(Increasing(n - 1), 16).Select
    If Cells(Increasing(n - 1), m) > Cells(Increasing(n - 1), (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic Programming\Arrows - Vanessa\Green.png").Select
    ElseIf Cells(Increasing(n - 1), m) < Cells(Increasing(n - 1), (m - 1)) Then
        ActiveSheet.Pictures.Insert("C:\Users\jamie.rosssmith\Documents\Visual Basic Programming\Arrows - Vanessa\Red.png").Select
    End If
Next


End Sub
于 2013-09-24T04:35:02.097 回答
0

除非您指定,否则数组索引从零开始。为确保它从 1 开始,请在 Option Explicit 下方添加 Option Base 1 语句。

接下来,有点烦人的是,Excel 不理解 Cells。它确实理解 worksheet.cells 或 range.cells。要使用单元格,请声明一个工作表或范围对象变量并为其分配一个值。例如,将 wks 调暗为工作表。设置 wks = thisworkbook.worksheets("Sheet1")。

当你使用一个数组时,我总是在初始化它的大小时使用 Redim 语句。您的数组之一的代码正在减少 = array()。我会说 Redim 递减() = 数组()。

虽然您可以使用您编码的方法导入图片,但它不太可能最终出现在您想要的特定单元格中。

因此,如果可以,请重新评估您的方法并考虑是否没有其他方法可以达到预期的结果。

于 2013-09-24T09:02:48.563 回答