Named Ranges

bailey537

Board Regular
Joined
Jun 30, 2008
Messages
65
Hi

Is there a way to define a named range from a selection of cells that begin with a particular letter e.g. X from A1:A1000?

Cheers
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,196
You could perhaps go through the cells, finding each one that began with X and combine them using union in VB, but it could wind up being too complex a definition. What is is you're trying to achieve?
 

bailey537

Board Regular
Joined
Jun 30, 2008
Messages
65
I have a list of codes and i want to basicly determine how many begin with X quickly. As it is a big list I don't want to loop through them all. I also need to know their locations within the list
 

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,196
Wouldn't filtering the list achieve this? You could use a countif if you just wanted a count

=COUNTIF(A:A,"X*")

or if you really want to create the range:

Code:
Sub ComplexRange()
    Dim complexRng As Range, rng As Range, rStr As String
    Set rng = Columns(1).Find(what:="X*", lookat:=xlWhole)
    If Not rng Is Nothing Then
        rStr = rng.Address
        Do
            If complexRng Is Nothing Then
                Set complexRng = rng
            Else
                Set complexRng = Union(complexRng, rng)
            End If
            Set rng = Columns(1).FindNext(rng)
        Loop While rng.Address <> rStr
    End If
    complexRng.Select
End Sub
 

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,196

ADVERTISEMENT

actually, change

Code:
 complexRng.select
to
Code:
complexRng.Name = "myComplexRange"
If you want to actually name the range
 

bailey537

Board Regular
Joined
Jun 30, 2008
Messages
65
cheers for your help. I went with your first idea about the filtering and then naming the range with the visible cells. Do you know how to find the length of the range once this is generated?
 

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,196
you could try counting the cells using .cells.count

Code:
msgbox range("myComplexRange").cells.count
or maybe
Code:
msgbox range("myComplexRange").rows.count
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,387
Messages
5,511,019
Members
408,823
Latest member
TJKnight

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top