I have a cell with the below text in column N2.
71sdfsdf 2230400DATE of join 1997-06-03PROGRAMME : ACCES PLUSNew Value 534.55Right value 534.55Clear Value 308.50
I need the number after New Value
that is 534.55 in this case to be saved in column O2, The value next to Right Value
that is 534.55 again in column P2 and if Clear Value
is present then the value next to it that is 308.50 to be saved in Q2.
Clear value
won't always be included.
Can we write a macro for this? I have used InStr
to get the value if present in the cell but I am not aware how the cursor position in macro works.
If you're already using the InStr function then all you have to do is plop the value you extracted into the cell you want:
Public Sub YourMethod()
Dim s As String
s = "71sdfsdf 2230400DATE of join 1997-06-03PROGRAMME : ACCES PLUSNew Value 534.55Right value 534.56Clear Value 308.50"
Const NEW_VALUE = "New Value"
Const RIGHT_VALUE = "Right value"
Const CLEAR_VALUE = "Clear Value"
Dim posNewValue As Integer
posNewValue = InStr(1, s, NEW_VALUE, VbCompareMethod.vbTextCompare)
Dim posRightValue As Integer
posRightValue = InStr(posNewValue, s, RIGHT_VALUE, VbCompareMethod.vbTextCompare)
Dim posClearValue As Integer
posClearValue = InStr(posRightValue, s, CLEAR_VALUE, VbCompareMethod.vbTextCompare)
Dim newValue As String
posNewValue = posNewValue + Len(NEW_VALUE)
newValue = Mid$(s, posNewValue, posRightValue - posNewValue)
Dim rightValue As String
Dim clearValue As String
posRightValue = posRightValue + Len(RIGHT_VALUE)
If posClearValue > 0 Then
rightValue = Mid$(s, posRightValue, posClearValue - posRightValue)
posClearValue = posClearValue + Len(CLEAR_VALUE)
clearValue = Mid$(s, posClearValue)
Else
rightValue = Mid$(s, posRightValue)
End If
Sheets("Sheet1").Range("O2").Value = newValue
Sheets("Sheet1").Range("P2").Value = rightValue
Sheets("Sheet1").Range("Q2").Value = clearValue
End Sub
If lenght of numbers is consant you can use something like this:
For i = MyStart To MyLimit 'rows of the range of interest
If InStr(1, "New Value ", Range("N" & i).Value) <> 0 Then _
Range("O" & i).Value = Mid(Range("N" & i).Value, InStr(1, "New Value ", Range("N" & i).Value), 6)
If InStr(1, "Right Value ", Range("N" & i).Value) <> 0 Then _
Range("P" & i).Value = Mid(Range("N" & i).Value, InStr(1, "Right Value ", Range("N" & i).Value), 6)
If InStr(1, "Clear Value ", Range("N" & i).Value) <> 0 Then _
Range("Q" & i).Value = Mid(Range("N" & i).Value, InStr(1, "Clear Value ", Range("N" & i).Value), 6)
Next i
Pay attention to the presence or not of spaces (I suppose they were present)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.