OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 441
- Office Version
- 2019
- Platform
- Windows
Thanks in advance and feedback will be given on any suggestions.
How do I VBA code to find the whole word. For example: The sentence "Dog Fight" I will find the necessary word "Dog" , but if I'm searching for "D" I only want to find it if the sentence explicitly states it. In the case now, it finds "D" in Dog Fight.
How do I VBA code to find the whole word. For example: The sentence "Dog Fight" I will find the necessary word "Dog" , but if I'm searching for "D" I only want to find it if the sentence explicitly states it. In the case now, it finds "D" in Dog Fight.
Code:
Sub Sorter()
'_____________________________________________________________________________________________
'Turn off alerts, screen updates, and automatic calculation
'Turn off Display Alerts
Application.DisplayAlerts = False
'Turn off Screen Update
Application.ScreenUpdating = False
'Calculate to ensure all values are updated
Calculate
'Turn off Automatic Calculations
Application.Calculation = xlManual
'_____________________________________________________________________________________________
'Dimenisoning/Declaring Variables
Dim LastRow As Long
Dim Stock As String
Dim WS_Names As Worksheet
Dim WS_MWD As Worksheet
Dim Rw As Long
Dim i As Long
Dim RangeFind As Range
'_____________________________________________________________________________________________
'Set the worksheet names
Set WS_Names = Sheets("Sheet1")
Set WS_MWD = Sheets("Sheet2")
'_____________________________________________________________________________________________
'Find the last row of data in Sheet1 Sheet
'Activate the sheet first
WS_Names.Activate
'Find the last row
LastRow = Cells.Find(What:="*", After:=Range("A1"), lookat:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'_____________________________________________________________________________________________
'Set the Rw number to start with
WS_Names.Range("A3").Activate
'Loop through and match up the data
For i = 2 To LastRow
'Activate Sheet 1
WS_Names.Activate
'Check to see if either the stock ticker or company name is in a cell
If Range("A" & i).Value = "" Then
'Move on to the next cell
Else
'Set the cell value to the string
Stock = Range("A" & i).Value
'Activate Sheet2 to perform the search
'Activate the sheet
WS_MWD.Activate
'Set the search range and perform the search
Set RangeFind = Range("A:A").Find(Stock, LookIn:=xlValues, lookat:=xlWhole)
'If the value is not found
If RangeFind Is Nothing Then
'Do nothing
'If the value is found
Else
If Range("H" & RangeFind.Row).Value = "" Then
Range("H" & RangeFind.Row).Value = Stock
Else
Range("I" & RangeFind.Row).Value = Stock
End If
End If
End If
Next i
'Turn on alerts, screen updates, and calculate
'Turn On Display Alerts
Application.DisplayAlerts = True
'Turn on Screen Update
Application.ScreenUpdating = True
'Turn off Automatic Calculations
Calculate
'Place the curser in cell A2 of the "Name.Range" Worksheet
WS_MWD.Activate
Range("A2").Select
End Sub
[1/Code]