VBA Search and Copy Data to Another Sheet

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I have these data below and I was thinking of VBA codes where it will create 3 sheets based on Subject.

The 3 Sheets name is based on Subject as well:
Sheet 1 = Math
Sheet 2 = Science
Sheet 3 = History

Each tab has the same header as the raw data. If the subject is math then it will copy the data to Math tab. same thing with Science and History.

NumberNamePointsSource 2Subject
001Chen1000-Math
002Lia500-Math
003Ron400-Science
004May100-History

<tbody>
</tbody>



Any help will be much appreciated. :)
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Is the data you want to filter on academic subjects as shown, or is it something completely different?
 
Upvote 0
Hi Fluff,

You are correct. It is based on the academic subject. After running the macro, an excel sheet will be generated with four sheet tabs: 1) All Data, 2) Math, 3) Science, 4) History.

Also, if there's "Algebra" in the Subject column, it will be added under Math category.



Thanks a lot for the help.
 
Upvote 0
Try this. It won't name the one sheet 'History', because it is a reserved name. So this code will name the sheet 'Hist'.

Code:
Sub CopySubjectsOver()
Application.ScreenUpdating = False
Dim Main As Worksheet:      Set Main = Sheets("All Data")
Dim AR() As Variant:        AR = Main.Range("A1:E" & Main.Range("A" & Rows.Count).End(xlUp).Row).Value
Dim wName As String
Dim ws As Worksheet

For i = LBound(AR) + 1 To UBound(AR)
    Select Case AR(i, 5)
        Case "Math", "Algebra"
            wName = "Math"
        Case "Science"
            wName = "Science"
        Case "History"
            wName = "Hist"
    End Select
    
    If Not WorksheetExists(wName) Then
        Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
        ws.Name = wName
        ws.Range("A1:E1") = Application.Index(AR, 1, 0, 1)
    Else
        Set ws = Sheets(wName)
    End If
    
    ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(1, 5).Value = Application.Index(AR, i, 0, 1)
    
Next i
Main.Activate

Application.ScreenUpdating = True
End Sub

Function WorksheetExists(sName As String) As Boolean
    WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)")
End Function
 
Upvote 0
Hello Unknownymous,

Here's another option:-


Code:
Sub CreateNewShtsTransferData()

       Dim sht As Worksheet, lr As Long, x As Long
       Dim IdO As Object
       Dim key As Variant

       Set sht = Sheet1  '----> Sheet1 being your "Master" sheet?
       Set IdO = CreateObject("Scripting.Dictionary")
             
Application.ScreenUpdating = False
Application.DisplayAlerts = False
                 
lr = sht.Range("A" & Rows.Count).End(xlUp).Row
sht.Range("F2:F" & lr) = "=IF(OR(E2=""Math"",E2=""Algebra""),""Maths"",IF(E2=""History"",""Hist"",E2))"

For x = 2 To lr
       If Not IdO.Exists(sht.Range("F" & x).Value) Then
             IdO.Add sht.Range("F" & x).Value, 1
       End If
Next x

For Each key In IdO.keys
       If Not Evaluate("ISREF('" & key & "'!A1)") Then
       Worksheets.Add(After:=Sheets(Sheets.Count)).Name = key
End If
   
With sht.[A1].CurrentRegion
        .AutoFilter 6, key
        .Columns("A:E").Copy Sheets(key).[A1]
        sht.[A1].AutoFilter
        End With
Next key

sht.Select
sht.Columns(6).Clear
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "All done!", vbExclamation

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Nice VCOOLIO! Yours is much faster. The autofilter is clearly the better way to do this. I ran speed tests and yours was blazing fast compared to mine.

Just for grins I tried to see if it could go any faster. I shaved about a tenth of a second off running it on 12,000 rows, and it's essentially your code. Either way, here's the update.

Code:
Sub CopySubjectsOver()
Application.ScreenUpdating = False
Dim Main As Worksheet:      Set Main = Sheets("All Data")
Dim AR() As Variant:        AR = Main.Range("A1:E" & Main.Range("A" & Rows.Count).End(xlUp).Row).Value
Dim SD As Object:           Set SD = CreateObject("Scripting.Dictionary")

For i = LBound(AR) + 1 To UBound(AR)
    Select Case AR(i, 5)
        Case "Math", "Algebra"
            wName = "Math"
        Case "Science"
            wName = "Science"
        Case "History"
            wName = "Hist"
    End Select
    
    If Not SD.exists(wName) Then SD.Add wName, 1
    
Next i

For Each key In SD.keys
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = key
    With Main.[A1].CurrentRegion
        If key = "Hist" Then
            .AutoFilter 5, "History"
        Else
            .AutoFilter 5, key
        End If
        .Columns("A:E").Copy Sheets(key).[A1]
        Main.[A1].AutoFilter
    End With
Next key

Main.Activate

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Good evening IRobbo,

Thanks for that. It nice to see someone else take an interest and make comparisons for the sake of further efficiency. You've actually made my day (I know, I need to get a life!).

All the best IRobbo and keep those codes coming!

Cheerio,
vcoolio.
 
Upvote 0
You're welcome Unknownymous.
Glad we could help and thanks for the feed back.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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