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

指定しない限り、配列インデックスはゼロから始まります。1 から始まるようにするには、Option Explicit の下に Option Base 1 ステートメントを追加します。

次に、やや厄介なことに、Excel は Cells を理解していません。worksheet.cells または range.cells を理解します。セルを操作するには、ワークシートまたは範囲オブジェクト変数を宣言し、それに値を割り当てます。たとえば、ワークシートとして Dim wks を使用します。wks = thisworkbook.worksheets("Sheet1") を設定します。

配列を使用する場合、サイズを初期化するときは常に Redim ステートメントを使用します。配列の 1 つのコードが減少しています = array()。私の場合、Redim reduction() = array() と言うでしょう。

コーディングしたアプローチを使用して画像をインポートすることはできますが、目的の特定のセルに収まることはまずありません。

したがって、可能であれば、アプローチを再評価し、目的の結果を達成する別の方法がないか検討してください。

于 2013-09-24T09:02:48.563 に答える
0

LBound(減少) は 0 です。インデックスがゼロのセルは選択できません。そのため、For ループを 1 から開始するか、インデックスが 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 に答える