+ Reply to Thread
Results 1 to 21 of 21

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

Hybrid View

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

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

    Hi,

    Instead of using Application.Union,

     Application.Union(Range("EP" & j & ":EP" & i), _
                              Range("ER" & j & ":ER" & i), _
                              Range("ET" & j & ":ET" & i), _
                              Range("EV" & j & ":EV" & i), _
                              Range("EX" & j & ":EY" & i)).Select
    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
     Range("EP & j & :EP & i, ER & j & :ER & i, ET & j & :ET & i, EV & j & :EV & i, EX & j & :EY" & i).Select
    can anyone help me?

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

    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: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    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
    702

    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,482

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

    What's wrong with the Union approach?

    Another alternative
    intersect(rows(i).resize(j-i+1),range("EP:EP,ER:ER,ET:ET,EV:EV,EY:EY")).select
    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
    702

    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
    Range("EP" & j & ":EP" & i & ",ER" & j & ":ER" & i & ",ET" & j & ":ET" & i & ",EV" & j & ":EV" & i & ",EX" & j & ":EY" & i).Select
    but your alternative method
    intersect(rows(i).resize(j-i+1),range("EP:EP,ER:ER,ET:ET,EV:EV,EY:EY")).select
    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,482

    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
    702

    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

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

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

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

    Intersect(Rows(i).Resize(j - i + 1), Range("EP:EP,ER:ER,ET:ET,EV:EV,EX:EY")).select
    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
    702

    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,
    Upper bound, j = Range("G9:G" & Rows.Count).End(xlDown).Row 
    Lower bound, i = Range("G" & Rows.Count).End(xlUp).Row
    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,482

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

    These 3 lines all give me the same result.

    Sub Test()
    
        Dim J, I
        
        J = Range("G9:G" & Rows.Count).End(xlDown).Row
        I = Range("G" & Rows.Count).End(xlUp).Row
        
        Range("EP" & J & ":EP" & I & ",ER" & J & ":ER" & I & ",ET" & J & ":ET" & I & ",EV" & J & ":EV" & I & ",EX" & J & ":EY" & I).Select
    
        Application.Union(Range("EP" & J & ":EP" & I), _
                          Range("ER" & J & ":ER" & I), _
                          Range("ET" & J & ":ET" & I), _
                          Range("EV" & J & ":EV" & I), _
                          Range("EX" & J & ":EY" & I)).Select
                              
        Intersect(Rows(I).Resize(J - I + 1), Range("EP:EP,ER:ER,ET:ET,EV:EV,EX:EY")).Select
        
    End Sub
    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
    702

    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,482

    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.

            Intersect(Rows(j).Resize(i - j + 1), Range("EP:EP,ER:ER,ET:ET,EV:EV,EX:EY")).Copy

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

    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,482

    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,
    j = Range("G9").End(xlDown).Row
    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
    702

    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
    j = Range("G9:G" & Rows.Count).End(xlDown).Row,
    instead of
    j = Range("G9").End(xlDown).Row
    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,482

    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
    702

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

    Hi Andy,

    Please check carefully the workbook I have attached,
    FilterFunction:
        If ws.Name = "ANNEXURE 1 (ii)" Then
                ActiveSheet.AutoFilterMode = False
                With ActiveSheet
                    .Range("A12:TB" & i).AutoFilter Field:=2, Criteria1:="Business Banking"
                    .Range("A12:TB" & i).AutoFilter Field:=5, Criteria1:="HUNTER"
                End With
       End If
    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,482

    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
    702

    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,

    With ActiveSheet
            .Range("A12:TB" & i).AutoFilter Field:=2, Criteria1:="Business Banking"
            .Range("A12:TB" & i).AutoFilter Field:=5, Criteria1:="HUNTER"
    End With
    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,482

    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. [SOLVED] Forms, Bound Columns etc
    By Michael Beckinsale in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2006, 06:55 AM
  4. 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