creating named ranges using a loop

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
1,975
Office Version
  1. 365
Platform
  1. Windows
i am trying to write a macro to loop through a column of data, creating a named range each time it encounters a certain string. so, it inititially finds the first instance of the string, then finds the next instance, offsets one row back and then names that as the first named range. how do i get it to actually loop through the column until the end of the data?

I can get the first range named but can't figure how to get a loop into my macro to repaeat the process.

Code:
Sub x()

    Dim rngTemp As Range
    Dim rngFind As Range
    Dim rngFirst As Range
    Dim rngLast As Range
    Dim nom As Range
    Dim strFirstAddress As String
    Dim lngRow As Long

    lngRow = Range("A" & Rows.Count).End(xlUp).Row + 1
    With Range("A:A")
        Set rngFind = .Find("VCH*", .Cells(lngRow, 1), LookIn:=xlValues, lookat:=xlPart)

        If Not rngFind Is Nothing Then
            strFirstAddress = rngFind.Address

            Set rngFirst = rngFind
            MsgBox rngFirst.Address '///$a$4
            
            'Set rngTemp = rngFirst
            'MsgBox rngFirst.Address '///$a$4
            
            Set rngFind = .FindNext(rngFind).Offset(-1)
            MsgBox rngFind.Address '///$a$8
i = 1
ActiveWorkbook.Names.Add Name:="rngname" & i, RefersToR1C1:= _
    ActiveSheet.Range(strFirstAddress & ":" & rngFind.Address)
   i = i + 1
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You only seem to be looking for one value, or perhaps more accurately values beginning with VCH?

So is it only one named range where all the values have VCH at the start?

Or are you trying to create a named range every time you find a value starting VCH?



Or do yo
 
Upvote 0
Is this what you want?

Code:
Sub xx()
    Dim i As Long
    Dim rngFind As Range
    Dim strFirstAddress As String
    i = 1
    With Range("A:A")
        Set rngFind = .Find("VCH*", .Cells(.Rows.Count, 1), LookIn:=xlValues, lookat:=xlPart)
        If Not rngFind Is Nothing Then
            strFirstAddress = rngFind.Address
            Do
                rngFind.Name = "rngname" & i
                i = i + 1
                Set rngFind = .FindNext(rngFind)
            Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,540
Messages
6,055,999
Members
444,839
Latest member
laurajames

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
Back
Top