Results 1 to 5 of 5

Thread: Union problem in Excel?

  1. #1

    Thread Starter
    New Member pb1's Avatar
    Join Date
    Jun 2004
    Posts
    7

    Question Union problem in Excel?

    I wish to delete several columns on the same worksheet at one go. So Union was used to group those columns together. But Run-time error 1004 : Method ¡®Union¡¯ of object¡¯_Application¡¯ failed was returned.
    Any way in getting around it please?
    VB Code:
    1. Option Explicit
    2. Dim oExcel As Excel.Application
    3. Dim oWB As Excel.Workbook
    4. Dim oWS As Excel.Worksheet
    5.  
    6. Sub DeleteData ()
    7. Dim rngA As Range
    8.  
    9.         oWB.Activate
    10.         oWB.Sheets(2).Select
    11. 'Autofit Col
    12.         oWB.Sheets(2).Columns ("A:L").EntireColumn.AutoFit    
    13.         Set rngA = oExcel.Union(Columns("F"), Columns("H"))
    14.         rngA.EntireColumn.Delete
    Last edited by pb1; Aug 31st, 2004 at 05:51 PM.
    Give me a hand not a finger.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    If the columns aren't changing then use
    VB Code:
    1. oWB.Sheets(2).Columns("A").entirecolumn.delete
    Where A is the column.

    Never heard of the union before.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    New Member pb1's Avatar
    Join Date
    Jun 2004
    Posts
    7

    Union - range object

    Union is a method of the Application Object. It returns the union of two or more ranges.
    The syntax is:
    VB Code:
    1. Application.Union(Arg1, Arg2, ...)
    where Arg1, Arg2, . . . are the Range objects whose ranges we wish to join together. At least two Range objects must be specified. I succeeded using union in deleting multiple rows but not columns.
    Any advice would be much appreciated.
    Last edited by pb1; Aug 31st, 2004 at 04:57 PM.
    Give me a hand not a finger.

  4. #4
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    maybe stupid but why don't you just delete each column one by one

    VB Code:
    1. columns("A:B").delete
    2. columns(yourcolumn).delete
    3.  
    4. etc

  5. #5

    Thread Starter
    New Member pb1's Avatar
    Join Date
    Jun 2004
    Posts
    7

    Talking Union problem solved !

    Union problem solved:-
    VB Code:
    1. Dim rngA As Range        
    2.    Set rngA = oExcel.Union(Range("F1:F2"), Range("H1:H2"),  _
    3.             Range("J1:J2"), Range("K1:K2"))
    4.    rngA.EntireColumn.Delete
    Give me a hand not a finger.

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