Copy only the data shown with adding a blank row between all copied rows

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello. I have this code to copy only the data shown in the same format to Sheet 2 I want to modify the code so that I can copy the data with an empty row between all the copied rows

VBA Code:
Sub COPY()

Dim SH1 As Worksheet, SH2 As Worksheet
Dim lr As String
Set SH1 = Sheet1
Set SH2 = sheet2
lr = SH1.Range("A5").SpecialCells(xlCellTypeLastCell).Address

SH2.Cells.Clear

SH1.Range("A5:" & lr).SpecialCells(xlCellTypeVisible).COPY _
Destination:=SH2.Range("A5")
SH1.Range("A5:G5").COPY Destination:=SH2.Range("A5")
Application.CutCopyMode = Fals
SH2.Columns("A:G").EntireColumn.AutoFit
End Sub

As for adding empty rows, I used this code, but it is slow due to the number of rows copied
Code:
Dim r As Long, y As Long
    y = SH2.Range("A" & SH2.Rows.Count).End(xlUp).Row
    For r = y To 7 Step -1
        SH2.Cells(r, 1).EntireRow.Insert Shift:=xlDown

    Next r
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Here's one way. Write code to:
Use a helper column to put sequential numbers for the data rows
Copy the numbers and paste below
Sort based on the helper column
Delete helper column
 
Upvote 0
Here's one way. Write code to:
Use a helper column to put sequential numbers for the data rows
Copy the numbers and paste below
Sort based on the helper column
Delete helper column
Hello, thank you for your interest, but frankly I do not have enough experience to do such a thing. I hope to find someone to help me with that.

This is the code that is currently used

Code:
Sub Copy()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim lr As Long

Application.ScreenUpdating = False
Set wsData = Sheet1
Set wsDest = sheet2

lr = wsData.Cells(Rows.Count, "A").End(xlUp).Row

wsDest.Cells.Clear

   wsData.Range("A5:G" & lr).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A5")

 wsDest.Columns("A:G").EntireColumn.AutoFit
 wsDest.Activate

Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Try this:
VBA Code:
Sub Copy()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim lr As Long
Application.ScreenUpdating = False
Set wsData = Sheet1
Set wsDest = Sheet2
lr = wsData.Cells(Rows.Count, "A").End(xlUp).Row
wsDest.Cells.Clear
wsData.Range("A5:G" & lr).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A5")
wsDest.Columns("A:G").EntireColumn.AutoFit
wsDest.Activate

Dim rng As Range
lr = Cells(Rows.Count, "A").End(3).Row
Set rng = Range("H5:H" & lr)
rng = Evaluate("ROW(" & rng.Address & ")")
rng.Copy Cells(lr + 1, "H")
ActiveSheet.Sort.SortFields.Clear
rng.Resize(rng.Count * 2).EntireRow.Sort Key1:=[H5]
[H:H].Delete

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub Copy()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim lr As Long
Application.ScreenUpdating = False
Set wsData = Sheet1
Set wsDest = Sheet2
lr = wsData.Cells(Rows.Count, "A").End(xlUp).Row
wsDest.Cells.Clear
wsData.Range("A5:G" & lr).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A5")
wsDest.Columns("A:G").EntireColumn.AutoFit
wsDest.Activate

Dim rng As Range
lr = Cells(Rows.Count, "A").End(3).Row
Set rng = Range("H5:H" & lr)
rng = Evaluate("ROW(" & rng.Address & ")")
rng.Copy Cells(lr + 1, "H")
ActiveSheet.Sort.SortFields.Clear
rng.Resize(rng.Count * 2).EntireRow.Sort Key1:=[H5]
[H:H].Delete

Application.ScreenUpdating = True
End Sub
Thank you very much, it was successful.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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