Extract numbers between hyphens

Mr Krabs

New Member
Joined
Jun 4, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,
I need some help from experts since my VBA skills is not enough for this task.

I have this data looking like this "AB-123456-1". I want to copy only the numbers in between the hyphens, i.e. "123456" in this case. What makes it a bit complicated is that the number of digits may vary. Below are some examples of how the data might look:
AB-13654-4
AB-5849631-8
AB-894651-12

The data is generated in one cell only using XLOOKUP and I will assign the macro to a form control button. The copied number will be pasted manually outside of Excel.

Is there a way to do this using VBA?

Great thanks in advance!

//Mr Krabs
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Mr Krabs,

The following will put the result into a message box (assumes the text is in cell A1 of the active sheet):

VBA Code:
MsgBox Split(Range("A1"), "-")(1)

Regards,

Robert
 
Upvote 0
You could also do this with formula, where is the data? Do you know how many rows?

Pseudocode:
VBA Code:
num = Split(YourValue, "-")(1)
 
Upvote 0
Solution
Hi Mr Krabs,

The following will put the result into a message box (assumes the text is in cell A1 of the active sheet):

VBA Code:
MsgBox Split(Range("A1"), "-")(1)

Regards,

Robert
This indeed pulls only the numbers between the hyphens. It's not possible to select the text in the message box though.
 
Upvote 0
You could also do this with formula, where is the data? Do you know how many rows?

Pseudocode:
VBA Code:
num = Split(YourValue, "-")(1)
The data is located in cell E7. Is there a way to get the numbers in another cell or directly in the clipboard?
 
Upvote 0
You have displayed multiple values in the example, are these values all in the same cell separated by a new line?

If this is the case, how would you like to display the result - all in the same cell?
 
Upvote 0
You have displayed multiple values in the example, are these values all in the same cell separated by a new line?

If this is the case, how would you like to display the result - all in the same cell?
Ah, sorry I was not clear. Those were only different examples of how the data could be structured. Cell E7 contain only one row of data.

I managed to get the function I wanted with below code (using A2 as a helper):

VBA Code:
num = Split(Range("E7"), "-")(1)
Range("A2").Value = num
Range("A2").Copy
 
Upvote 0
That's great, if you had data in range E7:E?? then you could use a loop as below that would place the result in column F.
VBA Code:
Sub test()
    Dim rng As Range, rCell As Range
    
    Set rng = Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
    
    For Each rCell In rng
        rCell.Offset(, 1) = Split(rCell.Value, "-")(1)
    Next rCell
End Sub
 
Upvote 0
That's great, if you had data in range E7:E?? then you could use a loop as below that would place the result in column F.
VBA Code:
Sub test()
    Dim rng As Range, rCell As Range
   
    Set rng = Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
   
    For Each rCell In rng
        rCell.Offset(, 1) = Split(rCell.Value, "-")(1)
    Next rCell
End Sub
Thanks a lot for the help :)
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,657
Latest member
giadungthienduyen

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