Results 1 to 5 of 5

Thread: [RESOLVED] Programmatically add an excel table...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Resolved [RESOLVED] Programmatically add an excel table...

    Hey guys,

    Im having trouble with a piece of code. From MSDN's site it looks like it should work - plus I made a macro in excel and they're almost identical.

    Code:
            Dim rng As Excel.Range = xlSheet.Range("D65536").End(Excel.XlDirection.xlUp)
            xlSheet.ListObjects.Add(Excel.XlSourceType.xlSourceRange, xlSheet.Range("A2:D" & rng.Address.ToString), , Excel.XlYesNoGuess.xlYes).Name = "Table1"
            xlSheet.Range("Table1[#All]").Select()
            xlSheet.ListObjects("Table1").TableStyle = "Table Style 2"

    It's getting hung up on the listobjects.add line and it's just throwing an unhandled exception error.

    Any help is always appreciated. Thanks Guys.

    Zach

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Programmatically add an excel table...

    Is this VB.Net or VBA? If it's VBA let me know so that I may move it for you.

    I'm assuming you are getting that error because you have:
    Code:
    xlSheet.ListObjects.Add(Excel.XlSourceType.xlSourceRange, xlSheet.Range("A2:D" & rng.Address.ToString), , Excel.XlYesNoGuess.xlYes).Name = "Table1"
    I don't know much about Excel, but I do know that in most(if not all) cases you cannot have a blank parameter with other parameters following.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: Programmatically add an excel table...

    This is for vb.net

    From everything I've read - this should work:

    Code:
    xlSheet.ListObjects.Add(SourceType:=Excel.XlSourceType.xlSourceRange, XlListObjectHasHeaders:=Excel.XlYesNoGuess.xlYes, Destination:=xlSheet.Range("A2:D" & rng.Address.ToString)).Name = "Table1"

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Programmatically add an excel table...

    Interesting, I only have AddEx

    Code:
    Dim oRang1 As Excel.Range = xlWorkSheet.Range("A1")
    Dim oRang2 As Excel.Range = xlWorkSheet.Range("A1").End(Excel.XlDirection.xlToRight).End(Excel.XlDirection.xlDown)
    Dim FinalRange As Excel.Range = xlWorkSheet.Range(oRang1, oRang2)
    FinalRange.Select()
    xlWorkSheet.ListObjects.AddEx(Excel.XlListObjectSourceType.xlSrcRange, FinalRange, , Excel.XlYesNoGuess.xlGuess).Name = "Kevin"

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: Programmatically add an excel table...

    Turns out the problem was two fold.

    Excel.XlSourceType.xlSourceRange, should of been Excel.XlListObjectSourceType.xlSrcRange ... crazy they would have two libraries so similar

    And .Add should of been .AddEx

    Thank you very much - you have helped me again Kevin.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width