Need VBA to place formula derived from source filename.

The following code works beautifully.

I need to take it once more step.

In column A, I need the following formula to be placed.

If filename = RC.csv, then:

="RC "&D2

(and copy down until the last record from the RC.csv file).

If filename = SJ.csv, then:

="SJ "&D2

(and copy down until the last record from the SJ.csv file).

If filename = SL.csv, then:

="SL "&D2

(and copy down until the last record from the SL.csv file).

Sub ImportCSVfiles()

    Dim file As Variant, fileNames As Variant, lastRow As Long
    fileNames = Array("RC", "SJ", "SL")
    ChDir ActiveWorkbook.Path
    
    For Each file In fileNames
        With Workbooks.Open(file & ".csv")
            With .ActiveSheet
                lastRow = .Cells(Rows.Count, "A").End(xlUp).Row
                .Range("A2:C" & lastRow).Copy ThisWorkbook.Worksheets("Data").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
            End With
            .Close False
        End With
    Next
        Range("B2:B" & Range("D" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=SUBSTITUTE(INDEX(RC[2]:R[2015]C[2],MATCH(""Grand Total For *"",RC[2]:R[2015]C[2],0)),""Grand Total For "","""")"
        Range("C2:C" & Range("D" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=SUBSTITUTE(INDEX(RC[1]:R[2015]C[1],MATCH(""Total For *"",RC[1]:R[2015]C[1],0)),""Total For "","""")"
        Range("B2:B" & Range("D" & Rows.Count).End(xlUp).Row).Value = Range("B2:B" & Range("D" & Rows.Count).End(xlUp).Row).Value
        Range("C2:C" & Range("D" & Rows.Count).End(xlUp).Row).Value = Range("c2:c" & Range("D" & Rows.Count).End(xlUp).Row).Value
    ActiveSheet.Range("D:D").AutoFilter Field:=1, Criteria1:="=Total For*", Operator:=xlOr, Criteria2:="=Grand Total*"
    Range("A2:A" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Range("D:D").AutoFilter Field:=1, Criteria1:="="
    Range("A2:A" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Range("D:D").AutoFilter

End Sub

Open in new window


Files can be found at www.annetroy.com/eeq
The latest macro file is AddLookup.xlsm

I am forever in your debt!
LVL 22
Anne TroyEast Coast ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
Sub ImportCSVfiles()

    Dim file As Variant, fileNames As Variant, lastRow As Long
    fileNames = Array("RC", "SJ", "SL")
    ChDir ActiveWorkbook.Path
    Worksheets("Data").Select
    
    For Each file In fileNames
        With Workbooks.Open(file & ".csv")
            With .ActiveSheet
                lastRow = .Cells(Rows.Count, "A").End(xlUp).Row
                firstrow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0).Row
                .Range("A2:C" & lastRow).Copy ThisWorkbook.Worksheets("Data").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
                lastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, "D").End(xlUp).Row
                ThisWorkbook.Worksheets("Data").Range("A" & firstrow & ":A" & lastRow).FormulaR1C1 = "=""" & file & " ""&RC[3]"
                                                                                                     '"=""RC ""&RC[3]"
            End With
            .Close False
        End With
    Next
        Range("B2:B" & Range("D" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=SUBSTITUTE(INDEX(RC[2]:R[2015]C[2],MATCH(""Grand Total For *"",RC[2]:R[2015]C[2],0)),""Grand Total For "","""")"
        Range("C2:C" & Range("D" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=SUBSTITUTE(INDEX(RC[1]:R[2015]C[1],MATCH(""Total For *"",RC[1]:R[2015]C[1],0)),""Total For "","""")"
        Range("B2:B" & Range("D" & Rows.Count).End(xlUp).Row).Value = Range("B2:B" & Range("D" & Rows.Count).End(xlUp).Row).Value
        Range("C2:C" & Range("D" & Rows.Count).End(xlUp).Row).Value = Range("c2:c" & Range("D" & Rows.Count).End(xlUp).Row).Value
    ActiveSheet.Range("D:D").AutoFilter Field:=1, Criteria1:="=Total For*", Operator:=xlOr, Criteria2:="=Grand Total*"
    Range("A2:A" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Range("D:D").AutoFilter Field:=1, Criteria1:="="
    Range("A2:A" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Range("D:D").AutoFilter
 Worksheets("Summary").Select
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anne TroyEast Coast ManagerAuthor Commented:
You are just sooooo awesome!!

Thank you!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.