Macro to Select Cells containing Text

seenai

Board Regular
Joined
Mar 31, 2013
Messages
54
Hi,

I have a Range that contains Text like below :
Date
Time
Sr No
Associate

I need a macro to select CELLS that match the any of the words.
Else need to select Cell A1.

Request your help to create a macro.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi
As I Understand
your data in column A then try
VBA Code:
Sub test()
    Dim rg As Range
    Dim a As Variant
    Dim i, ii
    a = Array("Date", "Time", "Sr No", "Associate")
    For i = 1 To 10000
        For ii = 0 To UBound(a)
            If Cells(i, 1) = a(ii) Then
                If rg Is Nothing Then
                    Set rg = Cells(i, 1)
                Else
                    Set rg = Union(rg, Cells(i, 1))
                End If: End If
        Next: Next
    rg.Select
End Sub
 
Upvote 0
Hi, Thanks for Quick reply.

I am getting error as below :
1617530880733.png


My Data is as below. Highlighted in Black are part of range.

1617530922120.png
 
Upvote 0
In this case
VBA Code:
Sub test()
    Dim rg As Range
    Dim a As Variant
    Dim i, ii
    a = Array("Date", "Time", "Sr No", "Associate")
    For i = 1 To 100
        For ii = 0 To UBound(a)
            If Cells(1, i) = a(ii) Then
                If rg Is Nothing Then
                    Set rg = Cells(1, i)
                Else
                    Set rg = Union(rg, Cells(1, i))
                End If: End If
        Next: Next
    rg.Select
End Sub
 
Upvote 0
In this case
VBA Code:
Sub test()
    Dim rg As Range
    Dim a As Variant
    Dim i, ii
    a = Array("Date", "Time", "Sr No", "Associate")
    For i = 1 To 100
        For ii = 0 To UBound(a)
            If Cells(1, i) = a(ii) Then
                If rg Is Nothing Then
                    Set rg = Cells(1, i)
                Else
                    Set rg = Union(rg, Cells(1, i))
                End If: End If
        Next: Next
    rg.Select
End Sub
Getting same error :
1617531412766.png
 
Upvote 0
What is the error you get?
It's working fine for excel 2013
 
Upvote 0
I see
You have to edit the array in the code
VBA Code:
  a = Array("Date", "Time", "Sr No", "Associate")
Case sensitive
And just in case replace with this line of code
VBA Code:
 If Trim(Cells(1, i)) = a(ii) Then
 
Upvote 0
1617532568381.png


Sub test()
Dim rg As Range
Dim a As Variant
Dim i, ii
a = Array("Date", "Time", "Sr No", "Associate")
For i = 1 To 100
For ii = 0 To UBound(a)
If Trim(Cells(1, i)) = a(ii) Then
If rg Is Nothing Then
Set rg = Cells(1, i)
Else
Set rg = Union(rg, Cells(1, i))
End If: End If
Next: Next
rg.Select
End Sub

I see
You have to edit the array in the code
VBA Code:
  a = Array("Date", "Time", "Sr No", "Associate")
Case sensitive
And just in case replace with this line of code
VBA Code:
 If Trim(Cells(1, i)) = a(ii) Then
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

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