VBA to scan each line for specific letters only returning columns C & E of C to I

antinora

Board Regular
Joined
Dec 4, 2013
Messages
87
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Greetings: Here is a table of teacher data. In each cell is course code, course name, pass percentage, & department all in a single string. I plan on using Text-to-Column after I run the VBA below. The VBA (which I've used in the past) sets up a array of tabs, one for each department. The code then scans each line & populates each tab with cells containing the same department name using Case commands.

I've used this code for student class data & it has worked out to 9 columns.

When I run it with data below (in reality we have 168 teacher meaning 169 rows) the code only populates columns C & E for each tab. Also, it doesn't scan all rows.

I am unsure if my LBound to UBound is in error. Being a VBA novice I am unsure which command tells the VBA to scan each line.

Any help is appreciated by both me & my students. Thanks

John A.

Excel 2010
ABCDEFGHI
TEACHERCLASS1CLASS2CLASS3CLASS4CLASS5CLASS6CLASS7CLASS8
ABBRUSCATOMES33QQT/1-Common Core Algebra 3 Term 3 of 3-0.5454545455-MathematicsMES41Q9C/1-Common Core Algebra 1 Term 1 of 4-0.7894736842-MathematicsMES41QEL/1-Common Core Algebra 1 Term 1 of 4-0.4444444444-MathematicsMES42Q9C/1-Common Core Algebra 1 Term 2 of 4-0.7894736842-MathematicsMES42QEL/1-Common Core Algebra 1 Term 1 of 4-0.4444444444-Mathematics
AMALFITANOPFS11/1-Fitness For Life-0.5957446809-Phys EdPFS11/2-Fitness For Life-0.5777777778-Phys EdPFS11/3-Fitness For Life-0.6744186047-Phys EdPGS11/4-Strength Training-0.72-Phys EdPGS11/6-Strength Training-0.6222222222-Phys Ed
ANDREEAS11QWR/1-Wilson Reading-0.6-ELAEAS11QWR/2-Wilson Reading-0.8333333333-ELAEAS11QWR/3-Wilson Reading-1-ELAEES85QQM/1-English 5-0.7333333333-ELASLS21Q9S/13-Living Environment 1-1-ScienceSLS21QLS/30-Living Environment 1 LAB-1-ScienceZY/2-Regents Skills-0.6-OSPAA
ANTHONYSES21QLR/19-Earth Science 1 LAB-0.8095238095-ScienceSES21QLR/9-Earth Science 1 LAB-0.5833333333-ScienceSES21QLX/14-Earth Science 1 LAB-0.6818181818-ScienceSES21QLX/3-Earth Science 1 LAB-0.8620689655-ScienceSES21QQR/8-Earth Science 1-0.8571428571-ScienceSES21QQR/9-Earth Science 1-0.8333333333-ScienceSES21QQX/14-Earth Science 1-0.7727272727-ScienceSES21QQX/3-Earth Science 1-0.8518518519-Science

<tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]

[TD="align: right"][/TD]

[TD="align: center"]5[/TD]

</tbody>
Sheet1
VBA:

Code:
Sub breakOut()Dim sh As Worksheet, c As Range, fn As Range, sAry As Variant
Set sh = Sheets("sheet1")
sAry = Array("ELA", "Mathematics", "Science", "Social Studies", "OSPAA", "Art", " Foreign Language", "Phys Ed")
    For i = LBound(sAry) To UBound(sAry)
        Sheets.Add After:=sh
        ActiveSheet.Name = sAry(i)
        sh.Range("A1:B1").Copy ActiveSheet.Range("A1")
        'ActiveSheet.Range("C1") = "Subj/Tchr"
        ActiveSheet.Columns("A:C").AutoFit
        For Each c In sh.Range("A2", sh.Cells(Rows.Count, 1).End(xlUp))
            Select Case sAry(i)
                Case "ELA"
                    ltr = "ELA"
                Case "Mathematics"
                    ltr = "Mathematics"
                Case "Science"
                    ltr = "Science"
                Case "Social Studies"
                    ltr = "Social Studies"
                Case "OSPAA"
                    ltr = "OSPAA"
                Case "Art"
                    ltr = "Art"
                Case "Foreign Language"
                    ltr = "Foreign Language"
                Case "Phys Ed"
                    ltr = "Phys Ed"
                                   
            End Select
            Set fn = c.Offset(, 3).Resize(1, 9).Find(ltr, , xlValues)
              If Not fn Is Nothing Then
                    Sheets(sAry(i)).Cells(Rows.Count, 1).End(xlUp)(2) = c.Value
                    Sheets(sAry(i)).Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = c.Offset(, 1).Value
                    Sheets(sAry(i)).Cells(Rows.Count, 1).End(xlUp).Offset(, 2) = fn.Value
                End If
        Next
    Next
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello antinora, I believe this code will work for you. One thing I noticed when I was looking over your original code is that you appear to have your Sheet1 tab named "sheet1", with a lowercase s. This is fine, just so long as you're aware of it, since the default sheet name uses a capital S. The sheet name will need to match the code, whether it's a lowercase or uppercase S.

Code:
Sub BreakOut()
    Dim i%, sh As Worksheet, c As Range, fn As Range, sAry, FirstClass$, PasteRow%
    
    Application.ScreenUpdating = False
[COLOR=#008000]    'Lowercase "sheet1" in original code[/COLOR]
    Set sh = Sheets("Sheet1")
    sAry = Array("ELA", "Mathematics", "Science", "Social Studies", "OSPAA", "Art", " Foreign Language", "Phys Ed")
    For i = LBound(sAry) To UBound(sAry)
        Sheets.Add After:=sh
        ActiveSheet.Name = sAry(i)
        sh.Range("A1:I1").Copy ActiveSheet.Range("A1")

        For Each c In sh.Range("A2", sh.Cells(Rows.Count, 1).End(xlUp))
            Set fn = c.EntireRow.Find(sAry(i), LookIn:=xlValues, LookAt:=xlPart)
            If Not fn Is Nothing Then
                FirstClass = fn.Address
                PasteRow = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
                Cells(PasteRow, 1) = c
                Do
                    Cells(PasteRow, fn.Column) = fn
                    If sAry(i) = "ELA" Or sAry(i) = "Art" Then
                        If Right(fn.Value, 3) <> sAry(i) Then Cells(PasteRow, fn.Column).ClearContents
                    End If
                    Set fn = c.EntireRow.FindNext(fn)
                Loop While Not fn Is Nothing And fn.Address <> FirstClass
                If WorksheetFunction.CountBlank(Cells(PasteRow, 2).Resize(, 8)) = 8 Then Cells(PasteRow, 1).ClearContents
            End If
        Next c
        Range("B2", Cells(Rows.Count, 1).End(xlUp).Offset(, 8)).WrapText = True
        Columns("B:I").ColumnWidth = 16
        ActiveSheet.Columns.AutoFit
        ActiveSheet.Rows.AutoFit
    Next i
    Application.ScreenUpdating = True
End Sub

I'm making some assumptions on how you want the data to appear. If this shows up in a format you don't want, can you provide specific details on how the information should appear? I also added some code for the ELA and Art departments to prevent them from matching false positives (the Art tab was getting classes with the word Earth in them, and I imagine ELA could run the same risk). Thank you, good luck with this!
 
Last edited:
Upvote 0
Sorry, just came up with a better way of preventing false positives. Try this code instead. This will only find a cell that matches the case of the department, "art" will not trigger "Art".

Code:
Sub BreakOut()
    Dim i%, sh As Worksheet, c As Range, fn As Range, sAry, FirstClass$, PasteRow%
    
    Application.ScreenUpdating = False
    'Lowercase "sheet1" in original code
    Set sh = Sheets("Sheet1")
    sAry = Array("ELA", "Mathematics", "Science", "Social Studies", "OSPAA", "Art", " Foreign Language", "Phys Ed")
    For i = LBound(sAry) To UBound(sAry)
        Sheets.Add After:=sh
        ActiveSheet.Name = sAry(i)
        sh.Range("A1:I1").Copy ActiveSheet.Range("A1")

        For Each c In sh.Range("A2", sh.Cells(Rows.Count, 1).End(xlUp))
            Set fn = c.EntireRow.Find(sAry(i), LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)
            If Not fn Is Nothing Then
                FirstClass = fn.Address
                PasteRow = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
                Do
                    Cells(PasteRow, 1) = c
                    Cells(PasteRow, fn.Column) = fn
                    Set fn = c.EntireRow.FindNext(fn)
                Loop While Not fn Is Nothing And fn.Address <> FirstClass
            End If
        Next c
        Range("B2", Cells(Rows.Count, 1).End(xlUp).Offset(, 8)).WrapText = True
        Columns("B:I").ColumnWidth = 16
        ActiveSheet.Columns.AutoFit
        ActiveSheet.Rows.AutoFit
    Next i
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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