You are here:

Excel/Extract the desired data with category wise in new sheet with category name

Advertisement


Question
Hello Tom ! How R U? I am using below code that u were provided previously and its working fine but one more thing that i want to add to it so that it can work for me to save lots of my time. so I would request you to help me to do that.... below is the code i m using...

Sub Extractdatatonewsheet()
Dim curr As Worksheet
Dim rg As Range
Dim ss As String, s As String, x As Long
Set curr = ActiveSheet
s = curr.Name
If InStr(1, s, " ", vbTextCompare) Then
    s = "'" & s & "'"
End If
Set rg = Application.InputBox("Select columns you want - if they're not together, hold the ctrl key down", Type:=8)
Set rg = Intersect(Rows(1), rg.EntireColumn)
rg.Copy
Sheets.Add
Range("C1").PasteSpecial
ss = "=IFERROR(INDEX(Sheet1!R1:R1048574,MATCH(RC2,Sheet1!C2,0),MATCH(R1C,Sheet1!R1,0)),"""")"
ss = Replace(ss, "Sheet1", s)
x = Range("IV1").End(xlToLeft).Column - 2
Range("C2").Resize(100, x).FormulaR1C1 = ss


   ' Auto fit
'

'
   Range("a1").Value = "Sr. No."
   Range("B1").Value = "Quary A/C"
   
   Cells.Select
   Cells.EntireColumn.AutoFit
   Application.CutCopyMode = False
   With Selection
       .HorizontalAlignment = xlCenter
       .VerticalAlignment = xlBottom
       .Orientation = 0
       .AddIndent = False
       .IndentLevel = 0
       .ShrinkToFit = False
       .ReadingOrder = xlContext
       .MergeCells = False
   End With
   With Selection
       .HorizontalAlignment = xlCenter
       .VerticalAlignment = xlCenter
       .Orientation = 0
       .AddIndent = False
       .IndentLevel = 0
       .ShrinkToFit = False
       .ReadingOrder = xlContext
       .MergeCells = False
   End With
   Rows("1:1").Select
   Selection.Font.Bold = True
Range("b2").Select

End Sub

The code above is attached to a command button and when i press the button it runs and extracts the selected ranges in a new sheet but what i want to add some more functionality is ---

1. from the sheet where the command button is created has a category description in a column named "SALARY TYPE" and there are some category for related employee. I want when it run it would use this "salary Type" column's data and create a new sheet for every category... for example there are category { gpf je, gpf minest,gpf opt, gpf cl4, cpf je, cpf minest,cpf cl4  etc... when it will run it will create a new sheet with the name "gpf je" then "gpf minist" then "gpf opt" then "gpf cl4" then "cpf je" then "cpf minist" and so on but do not repeat the same name again means i want one category sheet only once.

2. i want it will auto sum every column except top heading column on 4th row in every sheet created .

I am using excel 2007...

Thanks in advance.

Regards
Amit

Answer
Amit,

If I understand what you want, then I believe this will do it.



Sub Extractdatatonewsheet()
Dim curr As Worksheet
Dim rg As Range
Dim ss As String, s As String, x As Long
Set curr = ActiveSheet
s = curr.Name
If InStr(1, s, " ", vbTextCompare) Then
    s = "'" & s & "'"
End If
Set rg = Application.InputBox("Select columns you want - if they're not together, hold the ctrl key down", Type:=8)
Set rg = Intersect(Rows(1), rg.EntireColumn)
rg.Copy
Sheets.Add
Range("C1").PasteSpecial
ss = "=IFERROR(INDEX(Sheet1!R1:R1048574,MATCH(RC2,Sheet1!C2,0),MATCH(R1C,Sheet1!R1,0)),"""")"
ss = Replace(ss, "Sheet1", s)
x = Range("IV1").End(xlToLeft).Column - 2
Range("C2").Resize(100, x).FormulaR1C1 = ss


   ' Auto fit
'

'
   Range("a1").Value = "Sr. No."
   Range("B1").Value = "Quary A/C"
   
   Cells.Select
   Cells.EntireColumn.AutoFit
   Application.CutCopyMode = False
   With Selection
       .HorizontalAlignment = xlCenter
       .VerticalAlignment = xlBottom
       .Orientation = 0
       .AddIndent = False
       .IndentLevel = 0
       .ShrinkToFit = False
       .ReadingOrder = xlContext
       .MergeCells = False
   End With
   With Selection
       .HorizontalAlignment = xlCenter
       .VerticalAlignment = xlCenter
       .Orientation = 0
       .AddIndent = False
       .IndentLevel = 0
       .ShrinkToFit = False
       .ReadingOrder = xlContext
       .MergeCells = False
   End With
   Rows("1:1").Select
   Selection.Font.Bold = True
Range("b2").Select
Dim rng as Range, shNew as Worksheet, r as range, cell as Range

Set rng = curr.Rows(1).Find(What:="SALARY TYPE", _
After:=curr.Cells(1, Columns.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)

If rng Is Nothing Then
 MsgBox "Salary Type Not found"
 Exit Sub
Else
 Set r = curr.Range(rng.Offset(1, 0), curr.Cells(curr.Rows.Count, rng.Column).End(xlUp))
 
 For Each cell In r
  Set shNew = Nothing
  If Len(Trim(cell.Value)) > 0 Then
   On Error Resume Next
     Set shNew = Worksheets(cell.Value)
   On Error GoTo 0
   If shNew Is Nothing Then
     Worksheets.Add After:=Worksheets(Worksheets.Count)
     Worksheets(Worksheets.Count).Name = cell
   End If
 End If
Next
End If

End Sub

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.