+ Reply to Thread
Results 1 to 16 of 16

Selecting a range based on cell value

  1. #1
    Registered User
    Join Date
    02-29-2008
    Posts
    13

    Selecting a range based on cell value

    Hi,

    Please can anybody advise me how (in VBA) to select a range within a column where all values are the same?

    For example, given the following:

    A B
    1 ID Value
    2 1 A
    3 2 A
    4 3 A
    5 4 B
    6 5 B
    7 6 C

    How would I select range(B2:B4) where all values = A?

    Thanks.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434
    Something like this,

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

  3. #3
    Registered User
    Join Date
    02-29-2008
    Posts
    13
    Thanks Andy. That's brilliant.

    What I ultimately need to do is copy the rows containing each range to a new workbook with the same header.

    So I would end up with 3 files that look at follows:

    A B
    1 ID Value
    2 1 A
    3 2 A
    4 3 A

    and

    A B
    1 ID Value
    5 4 B
    6 5 B

    and

    A B
    1 ID Value
    7 6 C

    I've been playing around with your code but to no avail. I was trying to offset the range to broaden the selection in order to copy it but no joy.

    I'd really appreciate any guidance.

    Thanks again.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434
    Is the information in column A and B only?

    It maybe better to use Autofilter and copy+paste.

  5. #5
    Registered User
    Join Date
    02-29-2008
    Posts
    13
    Quote Originally Posted by Andy Pope View Post
    Is the information in column A and B only?

    It maybe better to use Autofilter and copy+paste.
    Really?

    Somebody already kindly provided me with an Autofilter solution but I didn't quite understand how to extend it so I went in search of a simpler option.

    Perhaps I should have been more persistant.

    If you are interested, I can post a link providing I am allowed to link to other sites here?

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    It might be quicker to use AutoFilter. Perhaps attach a small sample of your data?

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434
    Yes you are allowed to link to other sites. It is actively encouraged that those who cross post include links to other posts.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-29-2008
    Posts
    13
    Thanks very much guys.

    Here is what the sample data looks like.

    \1

    Column "D" is the key one - I need to split this file by "Code".

    The thing that's beating me is the header rows - there are actually 3 but you can't see here because 2 are merged.

    The data begins at "A4" and extends across to "AA" but the sample doesn't stretch that far.

    Andy - that code above would be great if i knew which ranges to edit so it works with my sample.

    Thanks again.
    Last edited by alpha1980; 10-24-2008 at 11:38 AM.

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

    Please Login or Register  to view this content.
    For future reference it's best to state as much relevant information in your original question rather than reveal vital information during the post.

  10. #10
    Registered User
    Join Date
    02-29-2008
    Posts
    13
    Thanks Andy.

    For some reason, the code seems to be generating files containing the top record only.

    I'm guessing it is something I am doing.

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434
    can you post example data file rather than an image of it.

  12. #12
    Registered User
    Join Date
    02-29-2008
    Posts
    13
    Quote Originally Posted by Andy Pope View Post
    can you post example data file rather than an image of it.
    Here you go...
    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,434
    Combination of code change and you having merged cells in the table header.
    If you get rid of the merged cells so the headers are in row 2 and data starts in row 3 this will work.

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    02-29-2008
    Posts
    13
    Thanks once again Andy.

    I see the code works but not with the 40,000 records I am trying to sort!

    Is this likely to be due to a limitation of AdvancedFilter? Or something else?

    I'm learning quickly that I probably should have mentioned the large amount of data at the beginning too...

    Edit - ignore that nonsense - it's working fine.
    Last edited by alpha1980; 10-24-2008 at 05:27 PM.

  15. #15
    Registered User
    Join Date
    02-29-2008
    Posts
    13
    Okay, I have made the following changes to Andy's code:
    • Added Application.GetOpenFileName to select source file.
    • Rather than use Workbooks.Add to create a new workbook, I've created a template .xls that already contains the headers and is called for every iteration of the for loop.
    • The template .xls is renamed with the name of the cell D5 ( i.e autofilter result), saved and closed for each iteration of the for loop.

    All I need to do now is remove the header (Row 2) so it is not copied to the template along with the data.

    Please can anybody help?

    Please Login or Register  to view this content.

  16. #16
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434
    Add an offset of 1 row to the range being copied.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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