Put Totals / Grand Totals data in adjacent columns.

I have code that pulls data into a file from CSVs. That data contains rows of Totals and Grand Totals.

For instance:
Total for Mixed Drinks A-L
Total for Mixed Drinks M-Z
Grand Total for Mixed Drinks

Total For Draft Beer
Total For Bottled Beer
Grand Total For Beer

After my import macro runs, I want to take all the "Total for" rows and put into Column C, adjacent to each record. I want to take all the "Grand Total for" rows and put into Column B, adjacent to each record.

I will be deleting the text "Total for" and "Grand Total For", so if this can be removed within the macro, that would be great. I do not need the actual totals values--just the text, i.e., "Mixed Drinks" and "Beer". These labels may change, so they can't be defined.

After these are moved, all "totals" and "grand totals" rows and all blank rows should be deleted. I can do that myself unless it is simple enough to incorporate into this macro.

Files are at www.annetroy.com/ee and the file that has data imported is revandcats.xlsm.
LVL 22
Anne TroyEast Coast ManagerAsked:
Who is Participating?

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

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:
The link says

Not found
Anne TroyEast Coast ManagerAuthor Commented:
I'm so sorry!  www.annetroy.com/eeq
Saqib Husain, SyedEngineerCommented:
Is this what you want?

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

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:

I thought this was going to be really tough to get. I cannot thank you enough!
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.