2016-08-12 73 views
0

我想複製一個工作簿中的值並將其粘貼到主工作簿中。範圍聲明從一個工作簿複製並粘貼到另一個

Set DestRange = DIAAggregation.Range(1 & NRow) 

停止調試器,給我的錯誤信息:

法 '範圍' 對象 「_Workbook」 的失敗

在網上找,我我意識到我沒有完全限定我的範圍,但我沒有看到我能做些什麼來完全限定範圍。

代碼如下,所涉及的行是最後一行。

Sub DIA_Concatenate() 
    ' 
    ' 

    ' 
    ' 

    Dim DIAAggregation As Worksheet 
    Dim DIAMaster As Workbook 
    Dim FolderPath As String 
    Dim NRow As Long 
    Dim FileName As String 
    Dim WorkBk As Workbook 
    Dim SourceRange As Range 
    Dim DestRange As Range 

    Dim Month As String 
    Dim Year As String 

    ' Prompts the user to specify which DIA data 
    ' is being aggregated (Month and Year). 
    ' Useful for checking data source and SaveAs file name. 
    Month = InputBox("What month's data is being aggregated?") 
    Year = InputBox("What year's data is being aggregated?") 

    ' Points the macro to the proper data source 
    ' (UPDATE THIS LINE TO YOUR DATA SOURCE!!!) 
    FolderPath = _ 
     "G:\Analytical Services\General Team Folders\Kyle\DIA Aggregation Bank\" 

    ' Opens the master workbook that is to have data added to it, 
    ' and declares the first sheet for the macro. 
    Set DIAMaster = Workbooks.Open(FolderPath & "*Aggregation*") 
    Set DIAAggregation = DIAMaster.Worksheets(1) 

    ' Incrementer to keep track of where new rows should be appended. 
    NRow = DIAAggregation.Rows.Count + 1 

    Dim LastRow As Long 

    ' Call Dir the first time, 
    ' pointing it to all Excel files in the folder path. 
    FileName = Dir(FolderPath & "*.xl*") 

    ' Loop until all .xl files in the source folder have been read. 

    Do While FileName <> "" 
     If InStr(1, FileName, "Aggregation") > 0 Then 
      FileName = Dir() 
      GoTo Jump 
     End If 

     If InStr(1, FileName, Month) = 0 Then 
      FileName = Dir() 
      GoTo Jump 
     End If 

     ' Open a workbook in the folder. 

     Set WorkBk = Workbooks.Open(FolderPath & FileName) 

     Dim J As Integer 

     ' Loop through data sheets to collect data. 

     For J = 2 To Sheets.Count ' From sheet 2 to last sheet. 
      ' Make the sheet active, find where the data is, 
      ' and select the data. 
      Sheets(J).Activate 

      LastRow = WorkBk.Worksheets(J).Cells.Find(What:="*", _ 
       After:=WorkBk.Worksheets(J).Cells.Range("A1"), _ 
       SearchDirection:=xlPrevious, _ 
       LookIn:=xlFormulas, _ 
       SearchOrder:=xlByRows).Row 

      Set SourceRange = WorkBk.Worksheets(J).Range("A3:E" & LastRow) 

      ' Set the destination range to start at column A and 
      ' be the same size as the source range. 

      Set DestRange = DIAAggregation.Range(1 & NRow) 
+0

我可能是錯的,但我認爲Range方法不喜歡你在那裏的'1'。如果您的意思是列A的1,請嘗試將其替換爲「A」 – PartyHatPanda

+0

在1和「A」之間切換不會改變任何有關錯誤消息 –

+0

的問題。你在這裏設置它爲'NRow = DIAAggregation.Rows.Count + 1',並且這將轉到表單的最後一行(行1048576),然後添加一行。所以你要告訴它去到一張不存在於表單中的行。嘗試使用'NRow = DIAAggregation.Rows.Count.End(xlUp)+ 1'替換它 – PartyHatPanda

回答

1

每最後一個註釋,通過電子表格變量和.Rows之間添加在.UsedRange改變NRow的聲明解決PartyHatPanda指出了問題。

相關問題