Loop through Read File

coveredinbutter

New Member
Joined
Dec 20, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have the following code working to read in a .csv file to another sheet based on a specific pattern:

VBA Code:
 With wsSM
        For i = 2 To .UsedRange.Rows.Count
            If Cells(i, 1).Value = group Then
            teamPattern = Cells(i, 2).Value
            Exit For
            Else
            Cells(i, 2).Value = Cells(i, 2).Value
            End If
         Next i
    End With

    rx.Pattern = teamPattern
    
    'disable file read if people.csv moved to sharepoint
    Open filePath For Input As #1
    Do Until EOF(1)
        Line Input #1, line
        splitLine = split(line, ",")
        
        If cTeam = -1 Then
            For i = 0 To UBound(splitLine)
                If splitLine(i) = "Team" Then cTeam = i
                If splitLine(i) = "Deactivated" Then cDeact = i
                If splitLine(i) = "Enable Scheduling" Then cSched = i
                If splitLine(i) = "Skill Mappings" Then cSkills = i
                If splitLine(i) = "Employee ID" Then cID = i
                If splitLine(i) = "First Name" Then cFName = i
                If splitLine(i) = "Last Name" Then cLName = i
            Next i
        End If
        
        On Error Resume Next
        If splitLine(cDeact) = "" And splitLine(cSched) = "TRUE" Then
            If rx.Test(splitLine(cTeam)) Then
                skills = split(splitLine(cSkills), ";")
                wsData.Range("A" & x) = splitLine(cID)
                wsData.Range("B" & x) = splitLine(cFName) & " " & splitLine(cLName)
                wsData.Range("C" & x) = splitLine(cTeam)
                wsData.Range("D" & x) = group
                
                mc = 5
           
                For m = LBound(skills) To UBound(skills)

                    wsData.Cells(x, mc).Value = skills(m)
                    mc = mc + 1

                Next m

                 x = x + 1
                            
ContinueDo:
            End If
        End If
    Loop
    Close #1

I want to replace the first section with a loop that I also have working (below) that fills an array and instead of having it define rx.pattern just once and check for just one pattern, have it loop through all the values in the array and read in the file based on each pattern in the array. I can't seem to get this last part working. Hopefully, that makes sense.

VBA Code:
For Each gp In arr_Groups

    With wsSM
        For i = 2 To .UsedRange.Rows.Count
            If Cells(i, 1).Value = gp Then
            customPattern(g) = Cells(i, 2).Value
            g = g + 1
            Exit For
            Else
            Cells(i, 2).Value = Cells(i, 2).Value
            End If
         Next i
    End With
    
    Next gp
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,646
Latest member
tudou

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