Find a value and then copy cells relative to the value

daithiboy

Board Regular
Joined
Jul 1, 2016
Messages
77
Hi All,

I regularly receive a rather poorly put together spreadsheet and I am putting together some macros to make it usable!

I've run into an issue where there is a merged title row that will always contain the word "Clinic". This row number can change regularly. The rows that I wish to copy start two rows below the title row.

I have been trying to write a macro which will search for "*Clinic*" and then offset down by two rows and copy everything from there to the last row, but have failed miserably

I hope someone can help as I have wasted quite a bit of office hours with this today!!:eeek:

Many thanks in advance.
Dave
 
Try:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim ClinicRow As Long
    ClinicRow = ActiveSheet.UsedRange.Find("Clinic", LookIn:=xlValues, lookat:=xlPart).Row
    Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    Range("H" & ClinicRow + 2) = "Regular"
    Range("H" & ClinicRow + 2).Copy Range("H" & ClinicRow + 2 & ":H" & LastRow)
    Range(Cells(ClinicRow + 2, 1), Cells(LastRow, lCol)).Copy Sheets("Sheet2").Cells(1, 1)
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
My code amended
- using columnA to determine last row
- copy instruction included
- paste instruction should be added

Code:
Sub CopyRows()
    Dim r As Long
    With ActiveSheet
        r = .Cells.Find("*clinic*").Row + 2
        .Range("A" & r, .Range("A" & Rows.Count).End(xlUp)).Offset(, 7).Value = "Regular"
        .Range("A" & r, .Range("A" & Rows.Count).End(xlUp)).EntireRow.Copy
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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