OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for your assistance. I have searched all over the web and throughout different forums, but I could not find anything simple enough to modify or use as a starting point.
I would like a function which will extract a number when either followed by a special character or ending with a special character.
The function would be something like where I would specify if I am trying to find data before the character or after. The following is a sample code which I have started and at the end I have placed some sample data.
Sample data with results are as follows:
I would like a function which will extract a number when either followed by a special character or ending with a special character.
The function would be something like where I would specify if I am trying to find data before the character or after. The following is a sample code which I have started and at the end I have placed some sample data.
VBA Code:
Option Explicit
Sub Sample()
Dim i As Long
Dim j As Long
Dim LRC As Long
Dim ColNo As Long
Dim aCell As Range
Dim Rng As Range
Dim News As String
Dim CharacterSpecial As Variant
Dim WhereLocated As String
Dim NumberExtract As Variant
'Set Range
ColNo = 7
LRC = Cells(Rows.Count, ColNo).End(xlUp).Row
Set Rng = Range(Cells(9, ColNo), Cells(LRC, ColNo))
For Each aCell In Rng
News = Cells(aCell.Row, ColNo)
For j = 4 To 5
If j = 4 Then
WhereLocated = "Before"
CharacterSpecial = "$"
ElseIf j = 5 Then
WhereLocated = "After"
CharacterSpecial = "%"
End If
NumberExtract = NumberExtractF(News, CharacterSpecial, WhereLocated)
Cells(aCell.Row, j) = NumberExtract
Next j
Next
End Sub
Function NumberExtractF(News, CharacterSpecial As Variant, WhereLocated As String) As Variant
If WhereLocated = "Before" Then
'Extract the number and the character before it
ElseIf WhereLocated = "After" Then
'Extract the number and the character after it
End If
End Function
Sample data with results are as follows:
Stocks - Analysis Tool, Sub - 52 Week Lows - (Active).xlsm | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | |||
1 | ||||||||||||||
2 | ||||||||||||||
8 | PRICE | % | NEWS | |||||||||||
9 | This one doesn't have any info. | |||||||||||||
10 | $55.25 | 25.24% | He made $55.25 which is 25.24% more than last time. | |||||||||||
11 | $100.25 | 10.00% | He made 10% that last time which was $100.25. | |||||||||||
12 | $100.10 | 50.00% | $100.10 50% | |||||||||||
13 | $150.00 | 50.25% | 50.25% $150.00 | |||||||||||
14 | $55.25 | 100.00% | This one is bunched together$55.25 100% | |||||||||||
Sheet2 (2) |