特定のSubを別のものから参照するときにタイトルに記載されているエラーが発生します。このSubが孤立して実行されるたびに正常に動作します。ここでExcel VBA。実行時エラー '1004': '_Worksheet'オブジェクトのメソッド 'Range'が呼び出されたときにSubが呼び出されました。
はコードです:
Sub UnitExtraction()
'On Sheet2 exists a table. This table has a Unit column.
'The Unit column holds either of the values of: 1,
'2, 3, 4, or 5. The purpose of this Sub routine is to
'extract the rows corresponding to each Unit and store
'them into a respective range, and then paste these Ranges
'to the right of the original table in numerical order.
'LastRow is necessary because the size of the original table can
'fluctuate.
Dim LastRow As Long
Dim ChartRange1 As Range
Dim ChartRange2 As Range
Dim ChartRange3 As Range
Dim ChartRange4 As Range
Dim ChartRange5 As Range
LastRow = Sheet2.UsedRange.Rows.Count
'The original table has 6 columns starting at Y1.
'Each Unit range is initialized with the original
'table's headers.
Set ChartRange1 = Sheet2.Range("Y1:AD1")
Set ChartRange2 = Sheet2.Range("Y1:AD1")
Set ChartRange3 = Sheet2.Range("Y1:AD1")
Set ChartRange4 = Sheet2.Range("Y1:AD1")
Set ChartRange5 = Sheet2.Range("Y1:AD1")
'For some reason this doesn't work unless run in isolation.
'This is the loop that fills each range with its rows.
'We start at Row 2 (below the headers) and continue to
'the last row of the table. The Z column corresponds
'to the Unit column in the table, hence the number 26.
'As you can see we are simply looping through each row
'checking to see which number is held in the Unit
'column and then storing that row in the correct
'range object.
For i = 2 To LastRow
If Sheet2.Cells(i, 26).Value = 1 Then
Set ChartRange1 = Union(ChartRange1, _
Sheet2.Range(Cells(i, 25), Cells(i, 30)))
ElseIf Sheet2.Cells(i, 26).Value = 2 Then
Set ChartRange2 = Union(ChartRange2, _
Sheet2.Range(Cells(i, 25), Cells(i, 30)))
ElseIf Sheet2.Cells(i, 26).Value = 3 Then
Set ChartRange3 = Union(ChartRange3, _
Sheet2.Range(Cells(i, 25), Cells(i, 30)))
ElseIf Sheet2.Cells(i, 26).Value = 4 Then
Set ChartRange4 = Union(ChartRange4, _
Sheet2.Range(Cells(i, 25), Cells(i, 30)))
ElseIf Sheet2.Cells(i, 26).Value = 5 Then
Set ChartRange5 = Union(ChartRange5, _
Sheet2.Range(Cells(i, 25), Cells(i, 30)))
End If
Next
End Sub
私が言ったように、私は単にVBエディタからだけでは、このサブを実行する場合、正常に動作するようです。しかし、私が前のSubからこのSubを呼び出すときは、私は前述のエラーにぶつかります。
具体的には、エラーは最初のElseIf
(これはハイライトされている行です)に発生します。これは、現在2の値が元のテーブルの最初のエントリであるためです。したがって、最初のIfはfalseです。
私はこの質問の複雑さを制限し、誰かがこの情報だけで問題を識別できるようにしたいと考えていましたが、誰かがこのSubにつながるコードを投稿する必要がある場合、私はそうすることができます。
このUnitExtraction
Subは、外部データベースにクエリを行い、Sheet2に元のテーブルを作成する "Query" Sub(呼び出す場合)から呼び出されます。
現在、UnitExtraction
に電話する4つの異なる「クエリ」サブがあります。実行されたクエリサブは、どの統計情報がデータベースから取得されるかを決定する、シート1のユーザー選択オプションボタンに基づいています。ただし、実行される特定のクエリに関係なく、このSubは、すべてのユニットの元のテーブルから各ユニットを抽出するために実行されます。
この問題のお手伝いをありがとうございます。
私は 'Sheet2'がどこにも定義されて表示されていない、それは多分世界的なのですか? –
シート番号はワークシートのExcelのコード名です。これは、ユーザーがワークシート名を変更した場合でも同じである、「難しい名前」です。私は通常、コードネームの使用を好みます。そうすれば、ユーザーはスプレッドシートの「ソフト名」を変更することができます。 http://www.techrepublic.com/blog/10-things/10-ways-to-reference-excel-workbooks-and-sheets-using-vba/ – ReducingDirt
非常に興味深い!毎日何か新しいことを学ぶ。リンクありがとう! :) –