VBA: Find Text in a column and place in a new sheet along with value from another column

hew

New Member
Joined
Mar 28, 2013
Messages
2
Hi All,
I'm new to VBA, and have been searching for threads with similar issue but can't seem to find exactly what i'm trying to do.

I have price data in column A, and a text column in column F. I am searching for 2 specific text "BUYOPEN" and "SELLCLOSE" in column F. Once those are found, i am attempting to find its corresponding price from the same row in column A.

The macro i have so far, is finding the text "BUYOPEN" and placing the corresponding price in the next column. but i would like to add/update the code to summarize this data in a 2nd worksheet.
So everytime there is a "BUYOPEN" or "SELLCLOSE", it will list each occurence along with the corresponding price in a new worksheet.

appreciate any help. thanks


Code:
Sub FindWord()     
Dim FindString As String     
Dim FindCell As Range          

FindString = ("BUYOPEN")    
 If FindString = "" Then Exit Sub          

For Each FindCell In Range(ActiveSheet.Range("F1"), ActiveSheet.Range("F300").End(xlUp)).Cells         
If InStr(FindCell, FindString) > 0 Then FindCell.Offset(, 1) = FindCell.Offset(, -5)             

Next FindCell 

End Sub



sample data
51.6323
0
0
0
SELL
0
52.08857
0
0
0
SELL
0
52.1537
0
0
0
BUY
BUYOPEN
52.1537
52.29881
0
0
0
BUY
0
52.42449
0
0
0
BUY
0
52.60843
0
0
0
BUY
0

<TBODY>
</TBODY>


</PRE>
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi, and welcome to the board.
I'm not completely sure I understand just what you're looking to do, but if I'm guessing right then perhaps something like this will help.
It will use sheet2, columns A & B as the destination when one of your FindStrings are found. (Change that to suit.)
Code:
Sub FindWords()
Dim FindString1 As String, FindString2 As String
Dim FindCell As Range
FindString1 = "BUYOPEN"
FindString2 = "SELLCLOSE"

For Each FindCell In Range(ActiveSheet.Range("F1"), ActiveSheet.Range("F300").End(xlUp)).Cells
  If InStr(FindCell, FindString1) > 0 Or InStr(FindCell, FindString2) > 0 Then
    With Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp)(2)
      .Value = FindCell.Value
      .Offset(, 1).Value = FindCell.Offset(, -5)
    End With
  End If
Next FindCell

End Sub
Note that (as posted) it will only find your strings if they are all capitalized.
If you would like it to not be case sensitive you can put the line:
Option Compare Text at the very top of the code module.

Hope it helps.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

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