+ Reply to Thread
Results 1 to 5 of 5

How to select range of cells that equal certain value...need help improving code.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-04-2014
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    27

    How to select range of cells that equal certain value...need help improving code.

    I have this code to select range of cells that equal to China in a master sheet. However i know this code can be simplified. Can you guys help me? Basically i want to select a column A cells of a country in a mastersheet. Can't figure out how to improve this.


    Sub macro65()
    
    
    Dim first As Integer
    Dim second As Integer
    
    
    For Each Cell In Range("A:A")
        If Cell.Value = "CHINA" Then
        first = Cell.Row
        Exit For
        End If
    Next Cell
    second = first
    
    For Each Cell In Range("A:A")
        If Cell.Value = "CHINA" Then
        second = second + 1
        End If
    Next Cell
    
    Range(Cells(first, 1), Cells(second - 1, 1)).Select
    
    
    
    
    
    End Sub

    Thank You

  2. #2
    Valued Forum Contributor
    Join Date
    04-09-2013
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    391

    Re: How to select range of cells that equal certain value...need help improving code.

    Hi,

    If you column A don't contains blank cells in between you can use filter technique to select the cells with China in it with the below code.

    Option Explicit
    
    Sub test()
    Dim lr As Long
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    
        
        Range("A1:A" & lr).AutoFilter
        ActiveSheet.Range("$A$1:$A$" & lr).AutoFilter Field:=1, Criteria1:="CHINA"
        Range("A1:A" & lr).SpecialCells(xlCellTypeVisible).Select
        
        Selection.AutoFilter
    End Sub

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,707

    Re: How to select range of cells that equal certain value...need help improving code.

    Or something like this maybe.
    Sub Maybe()
        Dim lr As Long, a As Long, b As Long
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        a = Range("A1:A" & lr).Find("CHINA").Row
        b = Range(Cells(a, 1), Cells(lr, 1)).Find("CHINA").Row
        Range("A" & a & ":A" & b).Select
    End Sub
    or this
    Sub Find_China()
        Dim rFound1 As Range, rFound2 As Range
        On Error Resume Next
        Set rFound1 = Columns(1).Find(What:="CHINA", LookIn:=xlValues, After:=Cells(1, 1))
        Set rFound2 = Columns(1).Find(What:="CHINA", LookIn:=xlValues, After:=rFound1)
        On Error GoTo 0
        Range(rFound1, rFound2).Select
    End Sub
    Last edited by jolivanes; 10-24-2014 at 02:50 PM. Reason: more info

  4. #4
    Registered User
    Join Date
    04-04-2014
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: How to select range of cells that equal certain value...need help improving code.

    Awsesome thank you!!

  5. #5
    Valued Forum Contributor
    Join Date
    04-09-2013
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    391

    Re: How to select range of cells that equal certain value...need help improving code.

    Thats because in cell A5 & A6 you have extra trailing space what you see as "BS" is actually "BS ". SO deleting those extra spaces will give you the result.

    See the file.
    Attached Files Attached Files

+ 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] VBA Code to select Range of cells as Email Body In Outlook
    By naveenmarapaka in forum Outlook Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2013, 06:43 AM
  2. improving code by removing select
    By Zealotwraith in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2013, 04:13 PM
  3. VBA code to select cells, row by row in range, based on condition
    By Vera22 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-31-2012, 11:12 AM
  4. Need code to select a range of cells based on criteria in column H
    By davekippen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-06-2011, 01:40 PM
  5. Code to select a range of cells based on cursor location
    By kys2000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-08-2007, 05:57 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