+ Reply to Thread
Results 1 to 12 of 12

Thread: Name defining based on XML input

  1. #1
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Name defining based on XML input

    I am looking to make my VBA more robust against errors. I based my project on the idea that the source document would stay the same. Largely it does but sometimes a column will be in a differrent order, this currently throws out my macro.

    I was wondering if I could define the columns based on the XML headers so if the headers were
    name1, name2, name3, name4, name5, name6
    I could use code similar to this to define it.
    Public Sub NameDef()
    
    Dim LastRow
    With Sheets("Sheet1")
      LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
      For Each cll In .Range("A1:CA1").Cells
        If Application.IsText(cll.Value) Then
          Range(cll.Offset(1), .Cells(LastRow, cll.Column)).Name = cll.Value
        End If
      Next cll
    End With
    End Sub
    then if the names came in a different order I could perhaps do something like this.
    So if they came as
    name2, name1, name3, name4, name6, name5
    and I only want to keep a few I could do something like this.
    Namekeep(name1, name2, name4, name5)
    NameSort(name1, name4, name5, name2)
    I thought a keep function may be more robust. If there is a column of data missing delete would error but if keep could be used it wouldn't matter what was thrown away only what was kept..

    This is just theory but is any of this possible, I don't want to have to keep creating an importXML macro each time the column header is out of order.

    All help greatly appreciated. As I now know I totally need to rewrite my macro I want to make it strong and error resistant.

    Thanks
    Last edited by flebber; 08-16-2010 at 08:56 PM.

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Name defining based on XML input

    Hi

    How about using an advanced filter that will bring in the ones you want to a consistent place. You then work on your consistent place.

    If there is a required column missing, then it would pay to pre check your data and issue a warning message if the column was missing.

    rylo

  3. #3
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: Name defining based on XML input

    Quote Originally Posted by rylo View Post
    Hi

    How about using an advanced filter that will bring in the ones you want to a consistent place. You then work on your consistent place.

    If there is a required column missing, then it would pay to pre check your data and issue a warning message if the column was missing.

    rylo
    What do you mean by advanced filter to import colums? That is basically what I am trying to acheive with named ranges? Do you have a refernce or link that you have used before to design an advanced import filter so I could read up on it sounds like something I would want to use.

  4. #4
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: Name defining based on XML input

    Been Searching around on the advanced filter and VBA. I found the below code, the concept of the advanced filter seems correct however I do note that in the code example below he defines the columns to bring across by cell reference. If the contents of the cell reference changes then the macro fails, that is what I need to circumvent. Any ideas?
    Sub UniqueCustomerProduct()
       
        Dim IRange As Range
        Dim ORange As Range
        
        Worksheets("SalesReport").Select
        Range("J1:AZ1").EntireColumn.Delete
        
        ' Find the size of today's dataset
        FinalRow = Cells(65536, 1).End(xlUp).Row
        NextCol = Cells(1, 255).End(xlToLeft).Column + 2
        
        ' Set up output range. Copy heading from D1 there
        Range("D1").Copy Destination:=Cells(1, NextCol)
        Range("B1").Copy Destination:=Cells(1, NextCol + 1)
        Set ORange = Cells(1, NextCol).Resize(1, 2)
        
        ' Define the Input Range
        Set IRange = Range("A1").Resize(FinalRow, NextCol - 2)
        
        ' Do the Advanced Filter to get unique list of customers & product
        IRange.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ORange, Unique:=True
            
        ' Determine how many unique rows we have
        LastRow = Cells(65536, NextCol).End(xlUp).Row
        
        ' Sort the data
        Cells(1, NextCol).Resize(LastRow, 2).Sort Key1:=Cells(1, NextCol), Order1:=xlAscending, Key2:=Cells(1, NextCol + 1), Order2:=xlAscending, Header:=xlYes
        
    End Sub

  5. #5
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Name defining based on XML input

    Hi

    Create and attach an example file that shows your general structure, and where things should go. Make it generic enough that it will cover your real situation. I'm sure we can come up with some code to suit.

    rylo

  6. #6
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: Name defining based on XML input

    I have attached an excel spreadsheet which is how it opens up from my xml import. I have highlighted in orange the columns I keep. You will notice columns with data such as
    1-2-4-5 $20
    , they are important columns and I have a macro which splits them into 4 more columns so that each number and dollar have there own column.

    I just need a consistent column set so macro works.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: Name defining based on XML input

    Its late night here, I will actually ge a good opportunity to do a lot of work on this tommorrow if anyone has a good idea on te import function to ad flexibility to XML I would realy appreciate it.

  8. #8
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Name defining based on XML input

    Hi

    In sheet2 range A1:AA1enter the list of headings you want to extract.

    Then run
    Sub aaa()
      With Sheets("Sheet1")
        .Range("A1").CurrentRegion.AdvancedFilter copytorange:=Sheets("Sheet2").Range("A1:aa1"), action:=xlFilterCopy
      End With
    End Sub
    rylo

  9. #9
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: Name defining based on XML input

    Quote Originally Posted by rylo View Post
    Hi

    In sheet2 range A1:AA1enter the list of headings you want to extract.

    Then run
    Sub aaa()
      With Sheets("Sheet1")
        .Range("A1").CurrentRegion.AdvancedFilter copytorange:=Sheets("Sheet2").Range("A1:aa1"), action:=xlFilterCopy
      End With
    End Sub
    rylo
    Thanks for the help! Would i need to do that Manually everytime or can I create that as a macro before the advanced filter?

  10. #10
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: Name defining based on XML input

    I am getting an error "the extract range has a missing or illegal field name" for the .Range("A1") of the advanced filter. This is the code I adapted to yours to automate the process.
    Sub InsertColumnHeadingSheet2()
    '
    ' InsertColumnHeadingSheet2 Macro
    '
    
    '
        Workbooks.Add
        Sheets("Sheet2").Select
        Sheets("Sheet2").Name = "Datasheet"
        Range("A1") = "venue"
        Range("B1") = "date"
        Range("C1") = "rail"
        Range("D1") = "name"
        Range("E1") = "mediumname"
        Range("F1") = "distance"
        Range("G1") = "age"
        Range("H1") = "weightcondition"
        Range("I1") = "totalprize"
        Range("J1") = "first"
        Range("K1") = "second"
        Range("L1") = "third"
        Range("M1") = "saddlecloth"
        Range("N1") = "horse"
        Range("O1") = "id5"
        Range("P1") = "barrier "
        Range("Q1") = "weight"
        Range("R1") = "rating"
        Range("S1") = "description"
        Range("T1") = "age6"
        Range("U1") = "career"
        Range("V1") = "goodtrack"
        Range("W1") = "deadtrack"
        Range("X1") = "slowtrack"
        Range("Y1") = "firstup"
        Range("Z1") = "secondup"
        Range("AA1") = "thistrack"
        Range("AB1") = "thisdistance"
        
        Dim SaveDate As String
        Filename = Application.GetOpenFilename(FileFilter:="Excel Files (*.xml*), *.xml*", Title:="Choose File To Copy", MultiSelect:=False)
        If Filename = "False" Then Exit Sub
        ActiveWorkbook.XmlImport URL:=Dir(Filename), ImportMap:=Nothing, Overwrite:=True, Destination:=Worksheets("sheet1").Range("$A$1")
        MsgBox "You selected " & Dir(Filename)
        
        With Sheets("Sheet1")
        .Range("A1").CurrentRegion.AdvancedFilter copytorange:=Sheets("Datasheet").Range("A1:ab1"), Action:=xlFilterCopy
        End With
        
        ' Ammending date in first row to display dd-mm-yyyy
        DateAmmend = Worksheets("Datasheet").Cells(Rows.Count, 1).End(xlUp).Row
        For k = 2 To DateAmmend
            If Cells(k, 2).Value > 0 Then
            Cells(k, 2).NumberFormat = "[$-C09]dd-mmmm-yyyy;@"
            End If
        Next k
        LastRow3 = Range("B" & Rows.Count).End(xlUp).Row
        Range("B2:B" & LastRow3).NumberFormat = "[$-C09]dd-mmmm-yyyy;@"
        
        SaveDate = (Worksheets("Datasheet").Range("A2").Value & Worksheets("Sheet1").Range("B2").Text)
        ActiveWorkbook.SaveAs Filename:= _
            "C:\Documents and Settings\Family\My Documents\Racing" & SaveDate & ".xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        
    End Sub

  11. #11
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Name defining based on XML input

    Hi

    Have you made sure that all the headings are actually in the source data sheet, and that the spelling is exactly the same? No extra spaces in the original data perhaps?

    rylo

  12. #12
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: Name defining based on XML input

    wow found one space in one word and that was gumming up the works.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0