views:

2776

answers:

2

I am new to Excel macros. I have some columns with headings scattered in many sheets. I would like to type a heading in some column which has the cursor and have the column with that heading copy-pasted to the column with the cursor.

Is it possible to do this by recording a macro? How? If not, how do I do it programmatically?

A: 

Does the heading appear many times across the different sheets? If not, then I would suggest using a simple if statement

if ('columnheading' = 'column to check','line1ref', 'line1refin other sheet')

You will have to check each column to do this.

Are there many sheets, are the columns always in the same position?

+1  A: 

Here are a few notes on code to copy a found column.

Dim ws As Worksheet
Dim r As Range
Dim CopyTo As String

'You must run the code from a suitable active cell '
strFind = ActiveCell.Value
'Assumes the column data will be written into the next row down '
CopyTo = ActiveCell.Offset(1, 0).Address

'Look at each worksheet'
For Each ws In ActiveWorkbook.Worksheets
    'Skip the active worksheet '
    If ws.Name <> ActiveSheet.Name Then
        'Get the last cell and row'
        lc = ws.Cells.SpecialCells(xlCellTypeLastCell).Column
        lr = ws.Cells.SpecialCells(xlCellTypeLastCell).Row

        'Use the first row for search range '
        Set r = ws.Range(ws.Cells(1, 1), ws.Cells(1, lc))

        'Find the first whole cell to match the active cell '
        Set f = r.Find(strFind, , , xlWhole)
        'If it is found ... '
        If Not f Is Nothing Then
           'Copy the whole column to the copy position '
           ws.Range(ws.Cells(2, f.Column), _
           ws.Cells(lr, f.Column)).Copy (ActiveSheet.Range(CopyTo))
        End If
    End If
Next
Remou
Thanks a lot for your time. Unfortunately, I get this error on the copying: Run-time error '1004': Copy method of Range class failed.
namin
Oops. I made some corrections. :)
Remou
It works now. Thanks a lot. :)
namin