Auto Range? VBA

hollytrab

New Member
Joined
Jan 29, 2025
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Is there a way for excel to create a range simply because a specific column contains certain text?

I have a spreadsheet sheet 1 A:R. If Column C contains "Basic Life Insurance", I want a range created of all rows that contain "Basic Life Insurance" in column C and name the range BasicLife.
 
Specify the estimated maximum number of occurrences of the phrase in column C.

Artik
 
Upvote 0
Try it:
VBA Code:
Sub AAA()
    Dim rngSource As Range
    Dim Address1 As String
    Dim rngFound As Range
    Dim rng As Range

    Set rngSource = Intersect(ActiveSheet.UsedRange, Columns("C"))

    With rngSource
        Set rng = .Find("Basic Life Insurance", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

        If Not rng Is Nothing Then
            Address1 = rng.Address
            Set rngFound = rng

            Do
                Set rngFound = Union(rngFound, rng)
                Set rng = .FindNext(After:=rng)
            Loop While rng.Address <> Address1

            ThisWorkbook.Names.Add Name:="BasicLife", RefersTo:=rngFound
        End If

    End With
   
End Sub

Artik
 
Upvote 0
Try it:
VBA Code:
Sub AAA()
    Dim rngSource As Range
    Dim Address1 As String
    Dim rngFound As Range
    Dim rng As Range

    Set rngSource = Intersect(ActiveSheet.UsedRange, Columns("C"))

    With rngSource
        Set rng = .Find("Basic Life Insurance", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

        If Not rng Is Nothing Then
            Address1 = rng.Address
            Set rngFound = rng

            Do
                Set rngFound = Union(rngFound, rng)
                Set rng = .FindNext(After:=rng)
            Loop While rng.Address <> Address1

            ThisWorkbook.Names.Add Name:="BasicLife", RefersTo:=rngFound
        End If

    End With
  
End Sub

Artik
Hello, this works however, I need it to grab the whole row, not just the cell in column C. Row range A:Y. Can you help me tweak your original post?
 
Upvote 0
VBA Code:
Sub BBB()
    Dim rngSource As Range
    Dim Address1 As String
    Dim rngFound As Range
    Dim rng As Range

    Set rngSource = Intersect(ActiveSheet.UsedRange, Columns("C"))

    With rngSource
        Set rng = .Find("Basic Life Insurance", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

        If Not rng Is Nothing Then
            Address1 = rng.Address
            Set rngFound = Range(Cells(rng.Row, "A"), Cells(rng.Row, "Y"))

            Do
                Set rngFound = Union(rngFound, Range(Cells(rng.Row, "A"), Cells(rng.Row, "Y")))
                Set rng = .FindNext(After:=rng)
            Loop While rng.Address <> Address1

            ThisWorkbook.Names.Add Name:="BasicLife", RefersTo:=rngFound
        End If

    End With
  
End Sub

Artik
 
Upvote 0
Solution
VBA Code:
Sub BBB()
    Dim rngSource As Range
    Dim Address1 As String
    Dim rngFound As Range
    Dim rng As Range

    Set rngSource = Intersect(ActiveSheet.UsedRange, Columns("C"))

    With rngSource
        Set rng = .Find("Basic Life Insurance", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

        If Not rng Is Nothing Then
            Address1 = rng.Address
            Set rngFound = Range(Cells(rng.Row, "A"), Cells(rng.Row, "Y"))

            Do
                Set rngFound = Union(rngFound, Range(Cells(rng.Row, "A"), Cells(rng.Row, "Y")))
                Set rng = .FindNext(After:=rng)
            Loop While rng.Address <> Address1

            ThisWorkbook.Names.Add Name:="BasicLife", RefersTo:=rngFound
        End If

    End With
 
End Sub

Artik
Amazing!!! Exactly what I needed. Thank you!!!!
 
Upvote 0

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