Share via

Excel VBA Union method

Anonymous
2017-04-03T16:01:18+00:00

Two of the worksheet-defined ranges in my spreadsheet are manipulated by macros that can either extend (as new entries are added) or contract (as entries are removed) the ranges. It is highly convenient to maintain these as separate ranges.

However, in the spreadsheet itself I also have a need to use the data validation function to refer to a list of names that is a combination of the two ranges. I assume the Union method is the way to do this. I have tried inserting the code below into the macro that adds an entry to the list of payees, but I get a syntax error. I am probably committing some elementary error. This is the statement in VBA:

   Set Range("namelist") = Application.Union(Range("payerlist"), Range("payeelist"))

VBA says that the first term - Range("namelist") - is an invalid use of property. Can someone please put me straight? (There may be other problems in what I have written, of course.)

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Anonymous
    2017-04-03T17:39:35+00:00

    Hi

    I'm not sure what you're trying to do, but this works to define a new range.

    Sub testUnion()

     Application.Union(Range("payerlist"), Range("payeelist")).Select

    Selection.Name = "namelist"

    Range("namelist").Select

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2017-04-10T16:22:57+00:00

    Hi,

    just a note,

    you can't set defined name, two sections from two different sheets.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-04-04T09:27:41+00:00

    Thank you, John.

    I have tried this in a test spreadsheet and I can use the united range as a parameter to the data validation facility, provided the two constituent ranges are in the same column, which they can be.

    You will have realised that my gradually-acquired understanding of Visual Basic has some significant gaps.

    Regards, Ron

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-04-04T09:13:47+00:00

    Hello, All three ranges mentioned had already been defined in the worksheet.

    Regards, Ron

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2017-04-03T16:18:10+00:00

    Hi,

    Did you define a range "namelist"?

    Was this answer helpful?

    0 comments No comments