VBA Code to sort tables and sum

ryancgarrett

Board Regular
Joined
Jun 18, 2011
Messages
122
I have around 170 xml files and I wrote this code to import each file into a separate sheet. After the xml table is imported I have it remove duplicates, filter so only 6 rows are shown and then sum those 6 showing values. It works some of the time but is a little buggy. I haven't coded for years and know this is sloppy, but any help is much appreciated.

Code:
Sub importXML()Dim nextName As String, I As Integer, fileName As String


For I = 1 To 170
nextName = Sheets("Roster Info").Cells(I, 1).Value
fileName = nextName & ".xml"


Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = nextName


    Sheets(nextName).Select
    
    On Error Resume Next
    ActiveWorkbook.XmlImport URL:="C:\Users\rgarret7\Desktop\Unit 2 Survey\" & fileName, _
    importmap:=Nothing, overwrite:=True, Destination:=Range("$A$1")
    
    Sheets(nextName).Range("A1:N217").RemoveDuplicates Columns:=5, Header:= _
    xlYes
    
    Columns("G:G").Select
    Selection.Insert Shift:=xlToRight
    Selection.NumberFormat = "0.00"
    Range("G2").Select
    ActiveCell.FormulaR1C1 = "=LEFT([@answer],1)"
    Range("G2").Select
        
    ActiveSheet.ListObjects("Table" & I).Range.AutoFilter Field:=5, Criteria1:= _
    Array("13", "20", "26", "31", "35", "4"), Operator:=xlFilterValues
    
    Range("G38").Select
    ActiveCell.FormulaR1C1 = _
        "=R[-213]C+R[-204]C+R[-197]C+R[-191]C+R[-186]C+R[-182]C"
    Range("G38").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
    
Next I


End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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