VBA - Move rows with data + first empty row from Sheet1 -- And insert them above other rows in Sheet2.

LisaExcel

New Member
Joined
Nov 9, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all!

I'm stuck! o_O

I'm trying to move all rows with data in them + the first empty row below it from Sheet1.
I then want to insert all those rows above the rows that's already on Sheet 2.

Anyone?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
To get unstuck, you would need to provide more information. Are you just trying to do it with regular copy/paste commands. Do you want to do it automatically; if, so, under what conditions. As good as this forum is, mind reading is a little too far a step.
 
Upvote 0
To get unstuck, you would need to provide more information. Are you just trying to do it with regular copy/paste commands. Do you want to do it automatically; if, so, under what conditions. As good as this forum is, mind reading is a little too far a step.
Oh sorry... ❤️
I meant that on Sheet1 from row 2 the macro should mark all rows with data in them plus an extra row. Then move those rows to Sheet2 and insert them above row 2 on that sheet.
I don't know how to explain it better...
🙂
 
Upvote 0
Your explanation still leaves a lot to the imagination so here is an attempt at deciphering it.
If you were to just copy the data to the bottom of the target table, it is simpler..... Your data to be copied is in a table named tblSource and the target is named tblTarget

VBA Code:
Sub CopyData()
Dim tblSource As ListObject, tblTarget As ListObject
Dim HowManyRowsToInsert As Long
Dim AferWhichRowToInsertRow As Long
    Set tblSource = ActiveWorkbook.Worksheets("Sheet1").ListObjects("tblSource")
    Set tblTarget = ActiveWorkbook.Worksheets("Sheet2").ListObjects("tblTarget")

    HowManyRowsToInsert = tblSource.ListRows.Count + 1 'the +1 gives the extra blank row (which makes no sense)

    AferWhichRowToInsertRow = 0  '0 will be the first row
    For i = 1 To HowManyRowsToInsert
        tblTarget.ListRows.Add (AferWhichRowToInsertRow + i)
    Next
    tblSource.DataBodyRange.Copy
    tblTarget.DataBodyRange(1, 1).PasteSpecial
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Your explanation still leaves a lot to the imagination so here is an attempt at deciphering it.
If you were to just copy the data to the bottom of the target table, it is simpler..... Your data to be copied is in a table named tblSource and the target is named tblTarget

VBA Code:
Sub CopyData()
Dim tblSource As ListObject, tblTarget As ListObject
Dim HowManyRowsToInsert As Long
Dim AferWhichRowToInsertRow As Long
    Set tblSource = ActiveWorkbook.Worksheets("Sheet1").ListObjects("tblSource")
    Set tblTarget = ActiveWorkbook.Worksheets("Sheet2").ListObjects("tblTarget")

    HowManyRowsToInsert = tblSource.ListRows.Count + 1 'the +1 gives the extra blank row (which makes no sense)

    AferWhichRowToInsertRow = 0  '0 will be the first row
    For i = 1 To HowManyRowsToInsert
        tblTarget.ListRows.Add (AferWhichRowToInsertRow + i)
    Next
    tblSource.DataBodyRange.Copy
    tblTarget.DataBodyRange(1, 1).PasteSpecial
    Application.CutCopyMode = False
End Sub

Hi!

Thanks a million, but it doesn't work...

I get this:
Excel.PNG


I want to move these rows...
ExcelMoveFrom.PNG


...to here:
ExcelMoveTo.PNG


:)
 
Upvote 0
Hi!
I can't use Dropbox due to security reasons.
Anyways, I think I'll try to find this out.
Many thanks for your help.
:)
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top