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
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,715
Messages
5,833,284
Members
430,201
Latest member
Deepakpilla36

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top