简体   繁体   中英

Run-time error using variant array in Excel 2010 VBA

G'day,

I've searched high and low for the solution to this issue, so hopefully someone here will know what's going on!

The code I've got so far is below. My understanding is that I need to define i and n as Long, as they use the array variant? At the moment, I'm get the "Run-time error '1004': Application-defined or object-defined error" error after the line:

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

I've tried replacing the i and m variables with integers, purely for testing purposes, but the problem persists. Knowing me, I've indented something incorrectly :|

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) is 0. You can not select cell with zero index. That's why start your For loop from 1 or change your code to avoid rows with 0 index. Try this code:

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

Unless specified by you an array index starts at zero. To ensure it starts at 1 add the Option Base 1 statement underneath Option Explicit.

Next, somewhat annoyingly, Excel doesn't understand Cells. It does understand worksheet.cells or range.cells. To work with cells declare a worksheet or range object variable and assign it a value. For example, Dim wks as worksheet. Set wks = thisworkbook.worksheets("Sheet1").

When you use an array, I would always use Redim statement when initialising it's size. Your code for one of your arrays is decreasing = array(). Mine would say Redim decreasing() = array().

While you can import a picture using the approach you've coded it is unlikely to end up in the specific cell you desire.

Thus, if you can, reevaluate your approach and consider if there isn't another way to achieve the desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM