2016-05-25 12 views
0

私はfor -loopは、複数のファイルからデータをピックアップして使用しようとしているが、何らかの形で、単一のセル範囲または名前付き範囲のいずれかが動作していないループスルーの場合:VBA名前付き範囲エラー

Sub GetTTData() 
    ' Original from: 
    ' http://ccm.net/faq/24666-excel-vba-copy-data-to-another-workbook 

    ' CopyOpenItems Macro 
    ' Copy open items to sheet. 
    ' 
    ' Keyboard Shortcut: Ctrl+Shift+O 
    ' 
    Dim wbReport    As Workbook 'workbook where the data is to be pasted 
    Dim wbTTData    As Workbook 'workbook from where the data is to copied 
    Dim strName    As String 'name of the source sheet/ target workbook 
    Dim fileName    As String 
    Dim RAWDataSheet   As Worksheet 
    Dim SourceDataSheet  As Worksheet 
    Dim RAWData    As Range 
    Dim SourceData   As Range 
    Dim portName    As String 
    Dim rCell    As Range 
    Dim files    As Range 

    'set to the current active workbook (the source book) 
    Set wbReport = ThisWorkbook 
    'select Departure Airport 
    portName = Worksheets("Control").Range("Dep_Airport") 
    Set files = Worksheets("Control").Range("All_Files") 

    'select cell A2 on the target book 
    Set RAWDataSheet = wbReport.Worksheets("RAW TT Data") 
    RAWDataSheet.Activate 
    Set RAWData = RAWDataSheet.Range(Range("A2"), ActiveCell.SpecialCells(xlLastCell)) 

    'clear existing values form target book 
    RAWData.ClearContents 

    For Each rCell In Range("files") 

     'take source of TT Data from workbook and open file 
     fileName = Worksheets("Control").Range("File_Path") + Worksheets("Control").Range("rCell") 
     Set wbTTData = Workbooks.Open(fileName) 

     'activate the source book 
     wbTTData.Activate 

     'clear any thing on clipboard to maximize available memory 
     Application.CutCopyMode = False 

     'copy the range from source book 
     Set SourceDataSheet = wbTTData.Sheets("Sheet1") 

     'filter RAW TT Data for Departure Airport 
     Set RAWData = SourceDataSheet.Range(Range("A1"), ActiveCell.SpecialCells(xlLastCell)) 
     RAWData.AutoFilter Field:=3, Criteria1:=Array(portName), Operator:=xlFilterValues 

     Set SourceData = SourceDataSheet.Range(Range("A2"), ActiveCell.SpecialCells(xlLastCell)) 
     SourceData.Copy 

     'paste the data on the target book 
     RAWDataSheet.Range("A2").PasteSpecial 

     'clear any thing on clipboard to maximize available memory 
     Application.CutCopyMode = False 

     'save the target book 
     wbReport.Save 

     'close the workbook 
     wbTTData.Close savechanges:=False 

    Next rCell 

    'activate the source book again 
    wbReport.Activate 

    'clear memory 
    Set wbReport = Nothing 
    Set wbTTData = Nothing 
    Set SourceDataSheet = Nothing 
    Set SourceData = Nothing 

End Sub 

答えて

1

あなたはあなたを始めるときに

Set files = Worksheets("Control").Range("All_Files") 

Dim files As Range 

そして、あなたはここでそれを設定します。あなたはこの変数を持っていますRループあなたのコードは次のとおりです。

For Each rCell In Range("files") 

私はあなたが読んで、あなたのコードの意味を考える:

For Each rCell In files 
関連する問題