Macro which can create new excel with 4 sheets per different criteria

jgopalk

New Member
Joined
Jun 14, 2011
Messages
26
Hi there,

I need a macro which can able to create an excel with 3 sheets based on the following criteria.

NameAgeGenderSalary
Ram25M10000
Ganesh23M2000
Saleem10M62300
Antony26M85000
Jebestine30M36900
Sheela26F45630
Karen96F25300
Veni31F63900

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

Here based on the above table, I require a single separate excel with 3 sheets named "Age", "Gender", "Salary". The sheet age must contains Column B red colored text info (the whole row). Gender must contains Column C red color text (the whole row). Salary sheet must contains which there is no color in Column B and C.

Examble.
Age Sheet:
NameAgeGenderSalary
Ganesh23M2000
Antony26M85000
Sheela26F45630

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

"Gender" Sheet:
NameAgeGenderSalary
Saleem10M62300
Antony26M85000
Sheela26F45630

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

"Salary" Sheet:
NameAgeGenderSalary
Ram25M10000
Karen96F25300
Veni31F63900


<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>


Hope I am clear in what am asking.

Kindly take it as challenge and do this. I tried in many ways, but i couldnt do it.

Thanks,
Gopal
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
you can use this:
Code:
Sub NewWorkbook()

Dim cel As Range
Workbooks.Add
With ActiveWorkbook
    .SaveAs ThisWorkbook.Path & "\NewWorkbook.xls"
    .Sheets(1).Name = "Age"
    .Sheets(1).Range("A1:D1") = Array("Name", "Age", "Gender", "Salary")
    .Sheets(2).Name = "Gender"
    Sheets(2).Range("A1:D1") = Array("Name", "Age", "Gender", "Salary")
    .Sheets(3).Name = "Salary"
    Sheets(3).Range("A1:D1") = Array("Name", "Age", "Gender", "Salary")
End With
With ThisWorkbook.Sheets("Sheet1")
    For Each cel In .Range(.Range("A2"), .Range("A2").End(xlDown))
        If cel.Offset(, 1).Font.Color = 255 Then cel.EntireRow.Copy ActiveWorkbook.Sheets("Age").Cells(ActiveWorkbook.Sheets("Age").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
        If cel.Offset(, 2).Font.Color = 255 Then cel.EntireRow.Copy ActiveWorkbook.Sheets("Gender").Cells(ActiveWorkbook.Sheets("Gender").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
        If cel.Offset(, 1).Font.Color <> 255 And cel.Offset(, 2).Font.Color <> 255 Then cel.EntireRow.Copy ActiveWorkbook.Sheets("Salary").Cells(ActiveWorkbook.Sheets("Salary").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
    Next
End With

End Sub

in below line change Sheet1 to the sheet name in which your data table is
With ThisWorkbook.Sheets("Sheet1")
 
Upvote 0
First of all I am terribly sorry for this late reply. I couldn't access my internet due to some issue here. Anyway thank you so much for your reply.

When i save your query and run it, new workbook created with sheet 1 "age" and headers in A1 to D1 with error "Run-time error '9': Subscript out of range". When i click debug the error highlighted in yellow in the code ".Sheets(2).Name = "Gender"". I couldn't understand why this is happening.

Kindly let me know where i went wrong.

Thanks in advance for your time.

Best regards,
Gopal
 
Upvote 0
Hi Gopal

you must be using excel 2013 or changed the excel options to create new workbook with 1 sheet only. anyways put this line
Code:
Application.SheetsInNewWorkbook = 3
before
Code:
Workbooks.Add
that should solve your problem
 
Upvote 0

Forum statistics

Threads
1,217,972
Messages
6,139,698
Members
450,226
Latest member
DrKraGeN

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