Új hozzászólás Aktív témák

  • JagdPanther

    aktív tag

    sziasztok!

    Alábbi kóddal összemásolok több, azonos struktúrájú file-ban lévő sorokat egy (ugyanolyan struktúrájú) gyűjtő file-ba, soronként egymás alá. A kód lefuttatásakor mindig felülírja a korábban a gyűjtőfile-ban szereplő sorokat.
    Ezen szeretnék úgy változtatni, hogy a kód újrafuttatásakor mindig a legalsó, azaz üresen marad sorba kezdje el másolni a sorokat.
    Légyszi segítsetek, nem jövök rá hogyan írjam át!

    Sub MergeSelectedWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim SelectedFiles() As Variant
    Dim NRow As Long
    Dim FileName As String
    Dim NFile As Long
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range


    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Sheets("Sheet1")

    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Users\pc\Sajat_tarhely\Munka\Karbantartás\Gyűjtőszámlák"

    ' Set the current directory to the the folder path.
    ChDrive FolderPath
    ChDir FolderPath

    ' Open the file dialog box and filter on Excel files, allowing multiple files
    ' to be selected.
    SelectedFiles = Application.GetOpenFilename( _
    filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)

    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 4

    ' Loop through the list of returned file names
    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
    ' Set FileName to be the current workbook file name to open.
    FileName = SelectedFiles(NFile)

    ' Open the current workbook.
    Set WorkBk = Workbooks.Open(FileName)

    ' Set the source range to be A6 through last row.
    Dim LastRow As Long
    LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
    After:=WorkBk.Worksheets(1).Cells.Range("A1"), _
    SearchDirection:=xlPrevious, _
    LookIn:=xlValues, _
    SearchOrder:=xlByRows).Row
    Set SourceRange = WorkBk.Worksheets(1).Range("A6:V" & LastRow)

    ' Set the destination range to start at column A and be the same size as the source range.
    Set DestRange = SummarySheet.Range("A" & NRow)
    Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
    SourceRange.Columns.Count)

    ' Copy over the values from the source to the destination.
    DestRange.Value = SourceRange.Value

    ' Increase NRow so that we know where to copy data next.
    NRow = NRow + DestRange.Rows.Count

    ' Close the source workbook without saving changes.
    WorkBk.Close savechanges:=False
    Next NFile


    End Sub

Új hozzászólás Aktív témák