You are here:

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

Advertisement


Question
Hi Tom ! I am so sorry that i could not explained my question. the below code.....

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

the code above runs in two parts...

1. creates a new sheet with ranges selected through application input box.

2. then in second part it creates the "salary Type" range sheets.

but when it creates the sheet with " salary type" data, only the sheet name is present but all the sheets are blank. that i did not wanted.

i wanted all the sheets with same pattern as it is created on 1st part but with sheet names in "salary type".

I think now i m clear.

Answer
Amit,

Perhaps this is what you want:

Sub Extractdatatonewsheet()
Dim curr As Worksheet
Dim rg As Range
Dim ss As String, s As String, x As Long
Dim rng As Range, shNew As Worksheet, r As Range, cell As Range
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)


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
    rg.Copy

    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 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.