簡體   English   中英

范圍聲明以從一個工作簿復制並粘貼到另一個工作簿

[英]Range declarations to copy from one workbook and paste to another

我想從一個工作簿中復制值並將其粘貼到主工作簿中。

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)

在最后的注釋中,通過在電子表格變量和.Rows之間添加.UsedRange來更改NRow的聲明,可以解決PartyHatPanda指出的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM