+ Reply to Thread
Results 1 to 5 of 5

Defining 2 separate ranges

  1. #1
    Registered User
    Join Date
    11-01-2013
    Location
    Essex UK
    MS-Off Ver
    Excel 2003
    Posts
    95

    Defining 2 separate ranges

    I want to define and select 2 separate ranges in the form
    Range("A1: G1, A3:G3").Select

    but I want to apply variables the Row Numbers in the form
    Range("A(x): G(x), A(y):G(y)").Select

    I have been unable to establish the correct syntax to achieve this.

    The nearest I've got is:-

    Range("C" & (x) & ":" & "G" & (x), "C" & (Y) & ":" & "G" & (Y)).Select
    where x=1 and y=3 so that there are 2 separate ranges separated by a blank row.

    However this produces a solid range block from C1 to G3.

    Where is my syntax wrong?

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    Your error stands in the comma outside of the string …

    Easier than your syntax style is using the Replace VBA text function
    and more easier is following the TBTO second main rule so as a beginner starter :

    PHP Code: 
        With ActiveSheet.Columns("A:G").Rows
            Union
    (.Item(X), .Item(Y)).Select
        End With 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Registered User
    Join Date
    11-01-2013
    Location
    Essex UK
    MS-Off Ver
    Excel 2003
    Posts
    95

    Re: Hi ! Try this !

    Merci Marc
    Your 3 lines of code worked perfectly - thank you.
    Just for completeness, in "my" syntax, you said the error was in the comma outside the string....
    how would this be corrected?
    Also, I did not understand " TBTO second main rule " perhaps you could explain.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    • Your initial codeline : Range("A1:G1,A3:G3").Select …

      In your attempt the comma is not within the string and you forgot a & operator :
      Range("C" & (x) & ":" & "G" & (x), "C" & (Y) & ":" & "G" & (Y)).Select …

      And the ( ) for the variables x or y are useless : Range("A" & x & ":G" & x & ",A" & y & ":G" & y).Select

    • The replace way : Range(Replace( Replace("A#:G#,A¤:G¤", "#", x), "¤", y)).Select

    • For the more easier way : Think, But Think Object !

  5. #5
    Registered User
    Join Date
    11-01-2013
    Location
    Essex UK
    MS-Off Ver
    Excel 2003
    Posts
    95

    Re: Defining 2 separate ranges

    Many thanks Marc.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Defining changing ranges with vba
    By djb1970 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-07-2009, 01:46 PM
  2. Defining Dynamic ranges
    By kysaul in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-07-2008, 05:39 PM
  3. Defining Ranges
    By rokuk in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-08-2007, 06:58 PM
  4. Defining Ranges using macros
    By bhussey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-18-2006, 06:26 PM
  5. [SOLVED] Defining Ranges
    By Steve in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-28-2005, 03:05 AM
  6. Automatically Defining Ranges
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-05-2005, 05:06 PM
  7. Re-defining Ranges in VBA
    By aehan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2005, 02:06 PM
  8. defining ranges using Cells
    By ruuustar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-04-2005, 12:24 PM

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.6.0 RC 1