![](/img/trans.png)
[英]Excel VBA Run-time error '13': Type mismatch, when passing a Variant Array to Application.Index method
[英]Run-time error using variant array in Excel 2010 VBA
G'day,
我一直在寻找解决这个问题的方法,所以希望这里的人会知道发生了什么!
到目前为止,我得到的代码如下。 我的理解是,由于它们使用数组变量,因此我需要将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
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
除非您指定,否则数组索引将从零开始。 为了确保它从1开始,在Option Explicit下添加Option Base 1语句。
接下来,有些令人讨厌的是,Excel无法理解单元格。 它确实了解worksheet.cells或range.cells。 要使用单元格,请声明工作表或范围对象变量并为其分配值。 例如,Dim wks作为工作表。 设置wks = thisworkbook.worksheets(“ Sheet1”)。
当您使用数组时,在初始化其大小时总是会使用Redim语句。 您的数组之一的代码递减= array()。 我会说Redim减少()=阵列()。
虽然可以使用编码方法导入图片,但最终不会出现在所需的特定单元格中。
因此,如果可以的话,请重新评估您的方法,并考虑是否有其他方法可以达到期望的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.