Getting columns numbers of cells with particular string/substring using VBA

smide

Board Regular
Joined
Dec 20, 2015
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello.

I have a report in row1 Sheet2 (columns A-EQ).
I need to extract number of column(s) which contains strings/substrings: lines and shop. Column A = 1, Column B = 2 ...

Results should be placed only in column B in Sheet1 (starting from B3 cell).

Example.

Sheet 2

[TABLE="class: grid, width: 250, align: center"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A
[/TD]
[TD="align: center"]B
[/TD]
[TD="align: center"]C
[/TD]
[TD="align: center"]D
[/TD]
[TD="align: center"]E
[/TD]
[TD="align: center"]F
[/TD]
[TD="align: center"]G
[/TD]
[TD="align: center"]H
[/TD]
[TD="align: center"]I
[/TD]
[/TR]
[TR]
[TD="align: center"]1
[/TD]
[TD="align: center"]home
[/TD]
[TD="align: center"]moneylines
[/TD]
[TD="align: center"]pattern
[/TD]
[TD="align: center"]shoping
[/TD]
[TD="align: center"]search
[/TD]
[TD="align: center"]lines
[/TD]
[TD="align: center"]linesid
[/TD]
[TD="align: center"]petshop
[/TD]
[TD="align: center"].....
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Sheet 1 (after macro run)

[TABLE="class: grid, width: 250, align: center"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A
[/TD]
[TD="align: center"]B
[/TD]
[/TR]
[TR]
[TD="align: center"]1
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]2
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]3
[/TD]
[TD="align: center"][/TD]
[TD="align: center"]2
[/TD]
[/TR]
[TR]
[TD="align: center"]4
[/TD]
[TD="align: center"][/TD]
[TD="align: center"]4
[/TD]
[/TR]
[TR]
[TD="align: center"]5
[/TD]
[TD="align: center"][/TD]
[TD="align: center"]6
[/TD]
[/TR]
[TR]
[TD="align: center"]6
[/TD]
[TD="align: center"][/TD]
[TD="align: center"]7
[/TD]
[/TR]
[TR]
[TD="align: center"]7
[/TD]
[TD="align: center"][/TD]
[TD="align: center"]8
[/TD]
[/TR]
[TR]
[TD="align: center"]8
[/TD]
[TD="align: center"][/TD]
[TD="align: center"]....
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Something here is not right. Did you mean you have multiple rows with data in Sheet2 and then count how many times those strings are in each column?
If not result is just one single number - in your example 5. 5 times in your example you have cells with strings containing "lines" or "shop".

So what is it supposed to be?
 
Upvote 0
Code:
Option Compare Text 'ignore text case
Sub countlinesshop()
cnt = 3
For x = 1 To 147 ' a-eq
If InStr(Sheet2.Cells(1, x), "lines") Or InStr(Sheet2.Cells(1, x), "shop") Then
Sheet1.Cells(cnt, 2) = x
cnt = cnt + 1
End If
Next x
MsgBox "done"
End Sub
 
Last edited:
Upvote 0
Try:
Code:
Sub FindText()
    Application.ScreenUpdating = False
    Dim x As Long
    x = 3
    Dim foundString As Range
    For Each foundString In Sheets("Sheet2").Range("A1:EQ1")
        If foundString Like "*lines*" Or foundString Like "*shop*" Then
            Sheets("Sheet1").Cells(x, "B") = foundString.Column
            x = x + 1
        End If
    Next foundString
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Something here is not right. Did you mean you have multiple rows with data in Sheet2 and then count how many times those strings are in each column?

Maybe my opening post was a bit vague (sorry for that).
I have a data output only in one row (row1 Sheet2) and I need to find cell references (or column numbers as I said) which contains those strings/substrings and return those cell references transferred into column(s) number in output in Sheet1.
 
Upvote 0
Did you look at the posts made by Roderick_E and myself?
 
Upvote 0
Wow, while I was typing my previous post I got two answers!
Thank you Roderick_E and mumps. :beerchug:
 
Upvote 0

Forum statistics

Threads
1,223,719
Messages
6,174,089
Members
452,542
Latest member
Bricklin

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