Copy and Paste range into separate worksheet

Schaefer156

New Member
Joined
Jul 11, 2022
Messages
8
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,
I am looking to figure out this VBA code I am trying to write for a project I am working on, I have it that it will search for a certain text in column L, once it finds that certain word I want it to copy and paste columns A-K down from the row that it found the certain word until a blank cell and only copy those row, for example, I have the code search for K3 and when it finds it it will copy rows 6-11 to the next worksheet. And I know I can do it by setting the range but if someone comes in and adds a row it will mess up the range.
1657634212517.png
1657634393300.png
 

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.
Try:
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, fnd As Range
    Set fnd = Range("L:L").Find("K3", LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        LastRow = Range("L" & fnd.Row, Range("L" & Rows.Count).End(xlUp)).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlNext).Row - 1
        Rows(fnd.Row & ":" & LastRow).Copy Sheets("Paste").Range("A6")
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
That worked great! Is there a way I can make it only copy until column K or will it always copy the entire rows? And if I want to repeat those steps by searching for K3.1 and so on would it would still work correct?
 
Upvote 0
if I want to repeat those steps by searching for K3.1
Do you want the values for K3.1 to be added to the existing data or to replace the existing data? Also, do you always want to start the pasting at A6.
 
Upvote 0
Correct, I would like the first Paste to start at A6 and then each new section to be pasted 1 empty space underneath already pasted text.
 
Upvote 0
Pretty much the exact same way as in the first photo but it wouldn't be for every section i have in my sheet, only the one marked for the K3.1 and so on.
 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, fnd As Range
    Set fnd = Range("L:L").Find("K3", LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        LastRow = Range("L" & fnd.Row, Range("L" & Rows.Count).End(xlUp)).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlNext).Row - 1
        With Sheets("Sheet2")
            If .Range("A6") = "" Then
                Range("A" & fnd.Row & ":K" & LastRow).Copy .Range("A6")
            Else
                Range("A" & fnd.Row & ":K" & LastRow).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            End If
        End With
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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