简体   繁体   中英

Excel/VBA How to move data from one worksheet to another after last used cell?

I've got a workbook with 9 different worksheets in which 1 of the sheets if 4 of them combined into 1 sheet for further comparison which from a copy and paste point of view is easy but im looking at it from an automation point of view as the length of rows can increase and decrease depending on data.

i need sheet A to copy in first on the left hand side on columns A,B,C which is then followed by sheet B which is inserted directly below sheet a on the same columns. Sheets C and D are similar but on the right of the first 2 sheets in columns H,I,J so they can be compared

I've tried to be clever and run multiple for loops on each sheet copying the data over to this work sheet with A&B sharing a global variable and C&D holding another so they went into the right places. Issue i had with this is long running times and mainly crashing on excel.

i also tried copying an pasting all the columns but didn't work as they vary in length so cant be recorded.

I finally tried a way of setting an row counter to be the last used row of the previous sheet to work but it also resulted in crashing.

Sheets("Income").Select

Dim xell As Range

For Each xell In Range("A1:A3005")

If Not xell.Value = "" Then

xell.EntireRow.Copy
Sheets("Workings").Select
Cells(z, "A").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
Sheets("Income").Select
z = z + 1

End If

Next xell

This is an example of my first attempt with the other sheets code being similar anyone got an idea of how to copy 4 work sheets into 1 in the desired destinations>?

Some things:

First, stop using Sheets().Select. There is no need, it will just flicker madly. You can safely use Sheets("Workings").Cells(...) to reference Cells on a Sheet.

Second, Cells() takes only integers as parameters, not "A". At best, this will be converted to 65 (ASCII) which is wrong for your case. Use Cells(z, 1) instead of Cells(z, "A").

Third, z needs to start at 1, not 0. You didnt initialize it anywhere so it will start as 0, which crashes.

You could try this:

Sub CopyIt()
    allSheets = Array(Array(sheets("A"), sheets("B")), Array(sheets("C"), sheets("D")))
    Set destSheet = sheets("Dest")

    Set startRange = destSheet.Range("A1")

    For Each doubleSheet In allSheets
        For Each srcSheet In doubleSheet
            Set firstEmpty = srcSheet.Columns(1).Find("")
            If Not firstEmpty Is Nothing Then
                RowCount = srcSheet.Columns(1).Find("").Row - 1
                Range(srcSheet.Cells(1, 1), srcSheet.Cells(RowCount, 3)).Copy Destination:=startRange
                Set startRange = startRange.Offset(RowCount, 0)
            End If
        Next
        Set startRange = destSheet.Range("H1")
    Next
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM