How to construct a loop - find cell A by content, but paste cell B into another workbook.

markthewatercooler2

New Member
Joined
Jan 30, 2014
Messages
1
Hey guys,

I've been lurking in the existing threads, but not sure how to approach it yet. Should be straightforward, but I'm not very competent with syntax. Consider this table.

Patient id - 336DiagnosedDiabetes Type 1
Age32
Insurance numberGP notes text
Patient id - 118DiagnosedDiabetes Type 1
Age21
Insurance numberGP notes text
Patient id - 724DiagnosedDiabetes Type 2
Age46
Insurance numberGP notes text

<tbody>
</tbody>


Etc. Imagine hundreds of records. I would now like to separate Type 1 people into a separate workbook in the following way.

Insurance numberPatient id - 336GP notes text
Insurance numberPatient id - 118GP notes text

<tbody>
</tbody>

What kind of a loop do I need to use to tell Excel to only take certain cells and keep doing this until the document ends?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Assuming that there are three rows per record and that the second line on some rows is due to word wrap, See if this procedure does what you want. I suggest you test it on a copy of your file, or a mock up, before you apply it to your original. It is also assumed that the data resides in columns A:C with not header row. Copy this prcedure to your standard code module 1.
Code:
Sub newSht()
Dim sh As Worksheet, lr As Long, rng As Range, c As Range, wb As Workbook, nuSh As Worksheet, rg As Range
Set sh = Sheets(1) 'Edit sheet name - this is source sheet
lr = sh.Cells(Rows.Count, 3).End(xlUp).Row
Set rng = sh.Range("C1:C" & lr)
Set wb = Workbooks.Add
Set nuSh = wb.Sheets(1)
wb.SaveAs "Type 1.xlsx" 'If you are not using Excel2007 or > then edit the file extension
    For Each c In rng
        If Right(c.Value, 6) = "Type 1" Then
            Set rg = nuSh.Cells(Rows.Count, 1).End(xlUp)(2)
            rg = c.Offset(2, -1).Value
            rg.Offset(0, 1) = c.Offset(0, -2).Value
            rg.Offset(0, 2) = c.Offset(2, 0)
        End If
    Next
wb.Close True
End Sub

How it works. A new workbook will be created and named "Type 1". The code will walk down column C of the source worksheet to locate the phrase "Type 1". When found it will then copy the three designated elements to the new workbook, sheet 1. When completed, it will close and save the new workbook.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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