+ Reply to Thread
Results 1 to 5 of 5

Create a range from another range

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2013
    Location
    Riyadh, Saudi Arabia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Create a range from another range

    Hi,

    I am a beginner with VBA.
    I want to write a code that can extract particluar cells from a named range.
    For example I want to extract all the cells in column 1 say "Myrange" which contain Apples as a separate range i.e. desiredrange

    MYRANGE DESIREDRANGE
    1 7 Apples
    2 11 Apples
    3 15 Apples
    4
    5
    6
    7 Apples
    8
    9
    10
    11 Apples
    12
    13
    14
    15 Apples

    I tried to writing this code but doesn't work. Please help me out. Thanks

    Function extractarray(X As Range, Y As String) As Range
    Dim Templist()
    Dim I As Integer, J As Integer
    J = 0
    For I = 1 To X.Rows.Count
    If Not InStr(X.Cells(I, 1), Y) = 0 Then
    J = J + 1
    End If
    Next I
    Numcount = J
    K = 0
    ReDim Templist(1 To Numcount)
    For I = 1 To X.Rows.Count
    If Not InStr(X.Cells(I, 1), Y) = 0 Then
    K = 0 + K
    Templist(K) = X.Value
    End If
    Next I
    extractarray = Templist
    End Function

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Create a range from another range

    Maybe something like this:

    Option Explicit
    
    Function fSubRange(rng As Range, sSubRng As String) As String
    
    ' worksheet call: =fSubRange(MyRange,"Apples")
    ' worksheet call: =fSubRange($A$2:$A$16,"Apples")
    
    Dim rSubRng As Range, cell As Range
    
    For Each cell In rng
        If cell.Offset(, 1).Value = sSubRng Then
            If rSubRng Is Nothing Then
                Set rSubRng = cell
            Else
                Set rSubRng = Union(rSubRng, cell)
            End If
        End If
    Next 'cell
    
    fSubRange = rSubRng.Address
    
    End Function

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-01-2013
    Location
    Riyadh, Saudi Arabia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Create a range from another range

    Hi TMS,

    When I use this function as an array formula, it doesn't work.

    Actually the function I need is a replacement of excel's data filter function.
    If I use the excel data filter function it will filter all the cells that do not contain text "Apples".
    But I need a formula to do this for me. The cells which I want to be displayed contain other text besides "apples".

    I will reexplain what I need
    My datarange is below

    13
    123
    231
    213
    123
    45
    7 Apples
    321
    213
    10
    11 Apples
    23
    132
    213
    15 Apples
    2131
    123
    213

    And I need function/ formula to return the range below

    7 Apples
    11 Apples
    15 Apples

  4. #4
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Create a range from another range

    Hi,

    this might help.

    Sub String_Search()
    'code by xlbiznes
    Dim rng As Range
    Dim cl As Range
    Dim ctr As Integer ' to hold the instances of our search
    ctr = 1
    Set rng = Range("myrange")
    For Each cl In rng ' loop through the range
        
        If InStr(1, cl.Text, "Apples", vbTextCompare) > 1 Then ' search within the string for the word Apples.
        Range("e" & ctr) = cl.Text 'if found start populating the column e with the instance and increment the ctr by 1
        ctr = ctr + 1
        End If
        
    Next cl
    MsgBox "Process Completed !" & ctr - 1 & " Instances Found", vbInformation, "Xlbiznes"
    End Sub
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  5. #5
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,790

    Re: Create a range from another range

    Maybe this.
    Attached Files Attached Files
    Last edited by skywriter; 10-03-2015 at 01:01 PM.

+ 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] Help! - Copy Range (Sheet1,A10:F50), Paste Range (Sheet 2,A1:F41), Offset Range & Repeat
    By cjtimmer in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-30-2015, 06:27 PM
  2. Choosing from a range to create another range
    By Jaytee in forum Excel General
    Replies: 2
    Last Post: 06-04-2014, 03:35 PM
  3. send to range, popup box to input what the range should be each time/select range
    By lax2734 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2012, 01:37 AM
  4. Replies: 1
    Last Post: 04-16-2012, 05:27 PM
  5. Need to create named range but with variable range
    By bhodge10 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2011, 02:15 PM
  6. How do I create this range?
    By monica in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-08-2006, 10:50 AM
  7. [SOLVED] Create/copy combo boxes in one range if condition is met in a different range
    By LB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-29-2005, 08:05 PM

Tags for this Thread

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