+ Reply to Thread
Results 1 to 21 of 21

VBA code to select multiple columns with upper bound (j) & lower bound(i)

  1. #1
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi,

    Instead of using Application.Union,

    Please Login or Register  to view this content.
    can we select multiple column using Range("xx,xx,xx,xx,xx") with upper bound (j) & lower bound(i) ?
    I have tried this but got error
    Please Login or Register  to view this content.
    can anyone help me?

  2. #2
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Aren't you forgetting a bunch of quote marks? Try this:

    Range("EP & j & ":EP" & i, "ER" & j & ":ER" & i, "ET" & j & ":ET" & i, "EV" & j & ":EV" & i, "EX" & j & ":EY" & i).Select

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi, Faridwahidi,

    please note if you create two threads of the same topic by mistake (http://www.excelforum.com/excel-prog...-bound-i.html] and donīt let people work in vain.


    This is a duplicate post and as such does not comply with Rule 5 of our forum rules.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi Ciao,

    I am not aware that it was duplicated.

    sorry for inconveniences caused.

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    What's wrong with the Union approach?

    Another alternative
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  6. #6
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi Andy,

    Nothing wrong with the Union approach, just look for alternative method.

    It has been resolved
    Please Login or Register  to view this content.
    but your alternative method
    Please Login or Register  to view this content.
    does not work

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    works fine if you have valid i and j values.

  8. #8
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Of course i and j is valid because both approach below working fine

    Please Login or Register  to view this content.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Please Login or Register  to view this content.
    I missed the 2 column spread at EX:EY, is that what you meant?

  10. #10
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi Andy,

    I don't know what went wrong with intersect approach. I have already changed the missing columns before but still not working

    j and i definitely valid,
    Please Login or Register  to view this content.
    or maybe you have missed any syntax?

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    These 3 lines all give me the same result.

    Please Login or Register  to view this content.
    The attached actually puts values in the range.
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi Andy,

    Please refer to my attached file.

    Using both approach it works fine but not for intersect.
    Attached Files Attached Files

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Did you check the actual values of i and j? If so you would have seen that they were reversed compared to what you stated originally, "upper bound (j) & lower bound(i)"
    In fact j is the lower and i upper.

    Please Login or Register  to view this content.

  14. #14
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi Andy,

    It works now


    but I am a bit confused,

    i = Range("G" & Rows.Count).End(xlUp).Row,
    it will go up and stop if found non-empty cells in column "G" (the last row in column "G"), shoud be lower bound ?

    And

    j = Range("G9:G" & Rows.Count).End(xlDown).Row,
    From "G9" it will go to down and stop if found non-empty cells in column "G", shoud be upper bound?

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Well I guess it depends on what we mean by upper and lower.

    For me, in programming context, lower is start point and upper is end point.

    In your code, i comes up from last row and returns 27 or end of data. This is lower down the sheet but I consider this the upper value for coding.

    Your j code could be,
    Please Login or Register  to view this content.
    It selects the row nearest the top, which you consider upper but again in code terms I see this as lower.

  16. #16
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi Andy,

    Thanks for the opinion, you are at expertise level but I am only at beginner level. Too many things I need to learn.
    I take note your advise when dealing with other expertise in future.

    Just one more question,

    I am using
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    because it is for filtering function, am I right or wrong?

  17. #17
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    What filtering? There is no filtering in the workbook you posted so it's difficult to have what difference it would make.

  18. #18
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Hi Andy,

    Please check carefully the workbook I have attached,
    Please Login or Register  to view this content.
    Filtered at rows 12, but due rows 9 to 12 have been merged,
    my declaration as follow;

    i = Range("G" & Rows.Count).End(xlUp).Row
    j = Range("G9:G" & Rows.Count).End(xlDown).Row

  19. #19
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    So actually j is not variable. It will always be 13.

  20. #20
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    I thought j will not only remain at rows 13, if the data at rows 13 does not contains filtered criteria,

    Please Login or Register  to view this content.
    So j will go down from "G9" until it founds non-empty cells?
    j = Range("G9:G" & Rows.Count).End(xlDown).Row

    or I can use, j = Range("G9").End(xlDown).Row

    correct me if I am wrong.

  21. #21
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,428

    Re: VBA code to select multiple columns with upper bound (j) & lower bound(i)

    Without going through your code in detail I don't see you doing any special to excluded filtered cells.

    So just stick with the method you currently have in order to determine top row, if it works.

+ 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. [SOLVED] Can you declare only a lower bound for an array and let the upper bound float?
    By Granite-Granny in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2014, 01:40 PM
  2. Percentile Array with an upper and lower bound
    By KiaCzar in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-23-2013, 12:14 PM
  3. Forms, Bound Columns etc
    By Michael Beckinsale in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2006, 06:55 AM
  4. [SOLVED] Data Bound Controls in VBA??
    By Trip in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-08-2005, 12:52 PM
  5. Replies: 6
    Last Post: 07-05-2005, 03:05 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