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
 

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)
This macro will copy the range to Sheet2.
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(Cells(ClinicRow + 2, 1), Cells(LastRow, lCol)).Copy Sheets("Sheet2").Cells(1, 1)
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this

does it find the correct cell

Code:
Sub TestThis()
    Dim cel As Range:       Set cel = ActiveSheet.Cells.Find("*clinic*")
    MsgBox cel.Address
End Sub

How many rows need copying and where are they pasted?
 
Last edited:
Upvote 0
Many thanks mumps, that worked a treat. I can fiddle with the copy and paste to suit myself.

Yongle - That code found the correct cell - Many thanks.
 
Upvote 0
:beerchug:
Thanks for feedback
This sets up the copy for you, you just need to add the paste instruction
Code:
Sub CopyRows()
    Dim r As Long
    With ActiveSheet
        r = .Cells.Find("*clinic*").Row + 2
        .Range("A" & r, .Range("A" & Rows.Count).End(xlUp)).EntireRow.Copy
    End With
End Sub
 
Last edited:
Upvote 0
I should have added in that last post
- uses column A to determine last row. Amend that to whichever column is appropriate :)
 
Upvote 0
Mumps, Yongle,

Using your either of your find methods. How would I write the code to do the following:

If "clinic" is found then change the value of the cell which is two rows below and in column 8 i.e. If "Clinic" is found in cell A9 then I would like it to set the value of cell H11 to "Regular".

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

assumes column A determines last row


Do you need error handling?
- is there a risk that "clinic" will not be found?
 
Last edited:
Upvote 0
Do you want to change the cell to "Regular" before copying the range to Sheet2 so that it is included in the copied range?
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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