How to copy all rows above a specific text

KlaasE

New Member
Joined
Nov 4, 2019
Messages
37
Hello there,

I am trying to write a macro to copy all general info from an order. This is always row 1 to x, where x is the row where the text "Type" is placed. So this row varies per order. Now I was wondering if it is possible to write a macro where it copies all rows up until the row where "Type" is found (including that row).

Thanks!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Where on the row will we find the term "Type"

Will it be found in column A or B or what column?

And copy the rows and paste them where?
 
Upvote 0
The term "Type" will be found somewhere between column A and F, this varies as well. I want to paste them to a different worksheet named "InitialSample", on row 20.

Cheers!
 
Upvote 0
Option Explicit


Code:
Sub FindType()
    Dim c As Long, rng As Long
    Dim lr As Long, crow As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:F" & lr)
    For Each c In rng
        If c = "Type" Then
            crow = c.Row
            Range("A1:F" & crow).Copy
            Sheets("InitialSample").Range("A1").PasteSpecial xlPasteValues
        End If
    Next c
    Exit Sub
End Sub
 
Upvote 0
If the word "Type" is in a cell by itself, then I think this macro should do what you want...
Code:
Sub CopyRowOneDownToRowWithTypeInIt()
  Range("1:" & [A:F].Find("Type", , xlValues, xlWhole, , xlNext, False, , False).Row).Copy Sheets("InitialSample").Range("A20")
End Sub
 
Upvote 0
Just noticed an error in my code. Here is the correction
Code:
Sub FindType()
    Dim c As Long, rng As Long
    Dim lr As Long, crow As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Set rng = Range("A1:F" & lr)
    For Each c In rng
        If c = "Type" Then
            crow = c.Row
            Range("A1:F" & crow).Copy
            Sheets("InitialSample").Range("A20").PasteSpecial xlPasteValues
        End If
    Next c
    Exit Sub
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
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