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

smide

Board Regular
Joined
Dec 20, 2015
Messages
162
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

A
B
C
D
E
F
G
H
I
1
home
moneylines
pattern
shoping
search
lines
linesid
petshop
.....

<tbody>
</tbody>

Sheet 1 (after macro run)

A
B
1
2
3
2
4
4
5
6
6
7
7
8
8
....

<tbody>
</tbody>
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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. (y)
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,555
Members
449,385
Latest member
KMGLarson

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