2016-05-20 37 views
2

私は Run-time error '1004' Method 'Range' of object '_Global' failedに走り続けます。実行時エラー '1004'メソッド '_Global'オブジェクトの 'Range'が失敗しました - 動的テーブル名、ループ、ワークシート間検索

「受信」と呼ばれるワークシートには、ダイナミックな名前(テーブルの名前が時々変更される)を持つテーブル(ワークシート内の唯一のテーブル)があります。サブルーチンの冒頭に「invoiceTable」を追加します。基本的には、invoiceTableの行をループし、テーブル内のデータに従って他のワークシートのデータを変更したいとします。

invoiceTableの第1列は「フレーム」と呼ばれ、第5列は「数量受信」と呼ばれます。 InvoiceTableにリストされているフレームに従ってInvoiceTableをループし、 '在庫管理'というワークシートのデータを更新しようとしています。 invoiceTableの各フレームについて、「数量受領」を受け取り、それを特定のフレームのインデックスで「在庫管理」の列Tに追加したいと思います。

同様に、「単価」という見出しの下にあるinvoiceTableから値を取り出し、「在庫管理」のF列の値をinvoiceTableの各フレームの最新の購入価格で更新したいとします。

ActiveSheet.ListObjects(1).Name = "invoiceTable" 

For row = 1 To Range("invoiceTable").Rows.Count 
    If Range("invoiceTable[Frame]")(row).Value <> 0 Then 
     Dim frame As String 
     Dim purchQ As Integer 
     Dim price As Long 

     frame = Range("invoiceTable[Frame]")(row).Value 
     price = Range("invoiceTable[Unit Price]")(row).Value 
     purchQ = Range("invoiceTable[Quantity Received]")(row).Value 

     Sheets("Inventory Management").Select 
     Range("=OFFSET('Inventory Management'!F5,MATCH(frame,'Inventory Management'!B6:B41,0),0)").Value = price 
     Range("=OFFSET('Inventory Management'!T5,MATCH(frame,'Inventory Management'!B6:B41,0),0)").Value = Range("=OFFSET('Inventory Management'!T5,MATCH(frame,'Inventory Management'!B6:B41,0),0)").Value + purchQ 
     Sheets("Receiving").Select 
    End If 
Next 
+0

あなたは、このような 'レンジ(「invoiceTable」)のように複数のシートで作業している場合は、あなたの範囲を修飾します。 Rows.Count' – findwindow

+0

エラートリガーはどの行にありますか? –

+0

これはこれまでに見たRangeオブジェクトの中で最も複雑な使い方です。 –

答えて

0

在庫管理シートを扱う4行の代わりにこのコードを試してみてください。

With Worksheets("Inventory Management") 
    Dim FindFrame As Range 
    Set FindFrame = .Range("B6:B41").Find(frame) 
    If Not FindFrame Is Nothing Then 
     .Cells(FindFrame.Row, 6) = Price 
     .Cells(FindFrame.Row, 20) = .Cells(FindFrame.Row, 20) + purchq 
    End If 
End With 
+0

ありがとう! FindFrame = .Range( "B6:B41")。Find(frame)は、 'frame'がRangeの項目のいずれかと全く同じ場合でも常にNothingを返すようです"B6:B:41") – alexmcintosh

関連する問題