Extract text efter a certain character VBA

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
hi!
In ActiveCelli I have text "Summer_0,7"
I want to get all nummer efter character "_".
The following do not work, please help.
Sub FindThenummer()
Dim nm As Long
nm = Right(ActiveCell, InStr(ActiveCell, "_"))
ActiveCell.Offset(0, 1).Value = nm
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Does this do what you want...
Code:
Sub FindThenummer()
  Dim nm As Double
  nm = Split(Replace(ActiveCell, ",", "."), "_")(1)
  ActiveCell.Offset(0, 1).Value = nm
End Sub
 
Upvote 0
no, Rich
it return 7
I am looking för 0,7 (a nummer)
i use ","for decimal insted for "." in Excel
 
Last edited:
Upvote 0
no, Rich
it return 7
I am looking för 0,7
The comma is not my decimal point, so I wasn't sure how your system needed to handle it. Alright, try it this way instead...
Code:
Sub FindThenummer()
  Dim nm As Double
  nm = Split(ActiveCell, "_")(1)
  ActiveCell.Offset(0, 1).Value = nm
End Sub
 
Upvote 0
Tanks Rick!
its working nu.
I wonder what (1) means.
Nu I want to get all text befor character "_" in ActiveCell.Offset(0, 2).Value = nm
 
Upvote 0
I wonder what (1) means.
Nu I want to get all text befor character "_" in ActiveCell.Offset(0, 2).Value = nm
The Split function creates a zero-based array... the (1) is the second element of that array. To get the value before the underbar, replace (1) with (0).
 
Upvote 0
Another way

Code:
 Sub FindThenummer()    
    Dim SPos As Long
    Dim Before As Variant, After As Variant

    With ActiveCell
        SPos = InStr(.Value, "_")
        If SPos > 0 Then
            Before = Left(.Value, SPos - 1)
            After = Mid(.Value, SPos + 1, Len(.Value))
        Else    ' "_" not found
            After = 0    'or whatever you want
            Before = .Value    'or whatever you want
        End If
        .Offset(0, 1).Value = After
        .Offset(0, 2).Value = Before
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,952
Messages
6,122,457
Members
449,083
Latest member
Ava19

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