Macro To Keep Column Headers

lkohlri

New Member
Joined
Feb 27, 2012
Messages
7
Currently, I am running the marco below:


Sub FanOut()
Dim ColHead As String
Dim ColHeadCell As Range
Dim iCol As Integer
Dim iRow As Long 'row index on Fan Data sheet
Dim lRow As Integer 'row index on individual destination sheet
Dim Dsheet As Worksheet 'destination worksheet
Dim Fsheet As Worksheet 'fan data worksheet (assumed active)

Again:
ColHead = InputBox("Enter Column Heading", "Identify Column", [d1].Value)
If ColHead = "" Then Exit Sub
Set ColHeadCell = Rows(1).Find(ColHead, lookat:=xlWhole)
If ColHeadCell Is Nothing Then
MsgBox "Heading not found in row 1"
GoTo Again
End If

Set Fsheet = ActiveSheet

iCol = ColHeadCell.Column
'loop through values in selected column
For iRow = 2 To Fsheet.Cells(65536, iCol).End(xlUp).Row
If Not SheetExists(Fsheet.Cells(iRow, iCol).Value) Then
Set Dsheet = Worksheets.Add(after:=Worksheets(Worksheets.Count))
Dsheet.Name = CStr(Fsheet.Cells(iRow, iCol).Value)
Else
Set Dsheet = Worksheets(Fsheet.Cells(iRow, iCol).Value)
End If
lRow = Dsheet.Cells(65536, iCol).End(xlUp).Row
Fsheet.Rows(iRow).Copy Destination:=Dsheet.Rows(lRow + 1)
Next iRow
End Sub

Function SheetExists(SheetId As Variant) As Boolean

' This function checks whether a sheet (can be a worksheet,
' chart sheet, dialog sheet, etc.) exists, and returns
' True if it exists, False otherwise. SheetId can be either
' a sheet name string or an integer number. For example:

' If SheetExists(3) Then Sheets(3).Delete

' deletes the third worksheet in the workbook, if it exists.
' Similarly,

' If SheetExists("Annual Budget") Then Sheets("Annual Budget").Delete

' deletes the sheet named "Annual Budget", if it exists.

Dim Sh As Object
On Error GoTo NoSuch
Set Sh = Sheets(SheetId)
SheetExists = True
Exit Function
NoSuch:
If Err = 9 Then SheetExists = False Else Stop

End Function





This is taking a specified column and it parse out similar items into new worksheets, and naming the worksheet with that unique value. What I am looking for is an addition or another script that will take the column headers from the orginal data and add it to the subsequent data that the macro parsed out. Currently, there is a blank row on each worksheet that is parsed out. Ideally this would be the Column headers. Thanks for any help!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Code:
    [color=green]'loop through values in selected column[/color]
    [color=darkblue]For[/color] iRow = 2 [color=darkblue]To[/color] Fsheet.Cells(65536, iCol).End(xlUp).Row
        [color=darkblue]If[/color] [color=darkblue]Not[/color] SheetExists(Fsheet.Cells(iRow, iCol).Value) [color=darkblue]Then[/color]
            [color=darkblue]Set[/color] Dsheet = Worksheets.Add(after:=Worksheets(Worksheets.Count))
            Dsheet.Name = [color=darkblue]CStr[/color](Fsheet.Cells(iRow, iCol).Value)
            [color=red]Fsheet.Rows(1).Copy Destination:=Dsheet.Rows(1)[/color]
        [color=darkblue]Else[/color]
            [color=darkblue]Set[/color] Dsheet = Worksheets(Fsheet.Cells(iRow, iCol).Value)
        [color=darkblue]End[/color] [color=darkblue]If[/color]
        lRow = Dsheet.Cells(65536, iCol).End(xlUp).Row
        Fsheet.Rows(iRow).Copy Destination:=Dsheet.Rows(lRow + 1)
    [color=darkblue]Next[/color] iRow
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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