2017-01-17 4 views
2

すでにオープンしているワークブックの参照に問題があり、ExcelでVBAに新しく追加されました。以下のコードは、私がProductionBookをアクティブ化しようとする行に着くまでうまく機能します。ここでは「範囲外の下付き文字」エラーが表示され、ワー​​クブックがアクティブ化されていないと思われます。どのように私はこの問題を克服することができますか?前もって感謝します。公開されているExcelワークブックを参照する

Sub UpdateSales() 

'---Code written in the open workbook defined as ProductionBook 

Dim ProductionBook As Workbook 'Master file 
Dim sbSolvents As Workbook 'S&P BW-query workbook 
Dim path1 As String 

'---------Define path directory for S&P workbook------------- 
path1 = "C:\Users\scullycs\Desktop\P&O\BW Files\Shipped & Pending\January\ISOP.xlsm" 

Set sbSolvents = Workbooks.Open(path1) 

With sbSolvents 
    Range("j19:j36").Select 
    Selection.Copy 
End With 

Set ProductionBook = Workbooks("Master Production File.xlsm") 

With ProductionBook 
    Worksheets("S&OP Progress").Range("F11").PasteSpecial (xlPasteValues) 'this is where I get the subscript error 
End With 

End Sub 
+0

あなたの問題は、あなたが –

+0

からコピーしたいsbSolvents'ワークブックを使用すると、ソリューションおよび以下の私のコードを試してみました ''内のどのWorksheet'を定義していませんか? –

答えて

0

(下から3行目には、あなたがからコピーすることsbSolventsであなたのシートに「シート1」を変更する必要があります)以下のコードを試してみてください。

Sub UpdateSales() 

'---Code written in the open workbook defined as ProductionBook 

Dim ProductionBook As Workbook 'Master file 
Dim sbSolvents As Workbook 'S&P BW-query workbook 
Dim path1 As String 

'---------Define path directory for S&P workbook------------- 
path1 = "C:\Users\scullycs\Desktop\P&O\BW Files\Shipped & Pending\January\ISOP.xlsm" 

Set sbSolvents = Workbooks.Open(path1) 
Set ProductionBook = Workbooks("Master Production File.xlsm") 

' modify "Sheet1" in the line below to your sheet name 
sbSolvents.Worksheets("Sheet1").Range("J19:J36").Copy 
ProductionBook.Worksheets("S&OP Progress").Range("F11").PasteSpecial xlPasteValues 

End Sub 
+0

これは完璧に機能します。ありがとう、シャイ。 –

+0

@ Chris.Sようこそ、覚えておいてください –

関連する問題