Find String in Column A, Cut & Paste to Other Columns

dgr

Board Regular
Joined
Apr 24, 2005
Messages
176
Hi,
I'm using Excel 2007. I'm looking for a VBA macro solution.

In Column A, I've got text like this:
<b>Column A</b>
This is my Year
apple
boy

cat
snake


This is Year 2003
banana
girl
leaf
elephant


2004 is a good Year
dog
wolf
apple

mango

I want to find <b>Year</b> including all records below it, cut & paste it in the Column next to it. Therefore, eventually all Column headers will contain the word <b>Year</b> in it. The end result should look something like this:
<b>Column B</b>----------<b>Column C</b>----------------<b>Column D</b>
This is my Year----This is Year 2003--------2004 is a good Year
apple----------------banana-------------------dog
boy------------------girl-----------------------wolf
----------------------leaf-----------------------apple
cat-------------------elephant------------------
snake-------------------------------------------mango

the word <b>Year</b> is case sensitive. Could you show me the code please?

Thanks.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi.

Assuming your list begins in A1:

Code:
Sub Years()

Dim YearRows() As String
Dim Rng As Range
Dim LRow As Long, n As Long

LRow = Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = Range("A1:A" & 1 + LRow)

n = 1

For Each r In Rng
    If InStr(1, r, "Year") > 0 Or r.Row = LRow + 1 Then
        ReDim Preserve YearRows(n)
        YearRows(n) = r.Row
        n = n + 1
    End If
Next r

For i = 1 To UBound(YearRows) - 1
    Range("A" & YearRows(i) & ":A" & YearRows(i + 1) - 1).Copy Destination:=Cells(1, i + 1)
Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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