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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,108,917
Messages
5,525,621
Members
409,657
Latest member
19JimRon72

This Week's Hot Topics

Top