Extracting cells containing certain characters

MikeH93

New Member
Joined
Jul 21, 2015
Messages
5
I have a lot of data in cells but I am really only interested in certain cells. I want to extract the cell in each row which contains a "-" (there is one in each row) and move them into a column of their own. The entries containing the "-" are not adjacent which makes it tricky to move all of them. I can select all the cells using the find function but then I can't copy all selected cells and as far as I am aware none of the FIND or SEARCH functions can help me.

What I have:

12345
274536-X2315
3456233467-Y
1954-P451267

<tbody>
</tbody>

What I want to get:

12345Important Cells
274536-X231536-X
3456233467-Y67-Y
1954-P45126754-P

<tbody>
</tbody>

Given the range of tasks I've been able to perform using simple tricks and formulas, I thought this would be easy enough but having searched around, I still haven't found a solution. Would I have to look into using VBA or is there an intuitive way of getting this done? My VBA skills are very limited. Any help on the matter would be greatly appreciated.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,

Is it possible that there can be more than one cells with "-" in a row? In the above example you have one "-" cells for each row.
 
Upvote 0
Hi,

If there is only one cell with "-" in each row, try the following formula in F1:

=IF(ISERROR(SEARCH("-",A1)),IF(ISERROR(SEARCH("-",B1)),IF(ISERROR(SEARCH("-",C1)),IF(ISERROR(SEARCH("-",D1)),IF(ISERROR(SEARCH("-",E1)),"",E1),D1),C1),B1),A1)

drag this down the cells.

This is keeping in mind that you have the data in columns A:F
 
Upvote 0
Hi cbatrody. No there will only ever be one cell with one "-" in it for each row, just like the above scenario.
 
Upvote 0
Give this macro a try...
Code:
Sub ExtractDashedCells()
  Dim R As Long
  Application.ScreenUpdating = False
  For R = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    Rows(R).Find("-", , , xlPart).Copy Cells(R, "F")
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I guess I'm now late to this post. Here was my script.
Code:
Sub TestMe()
Application.ScreenUpdating = False
Dim i As Integer
Dim b As Integer
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        For b = 1 To 5
            If InStr(Cells(i, b).Value, "-") Then
                Cells(i, 6).Value = Cells(i, b).Value
                b = b + 1
            End If
        Next
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,310
Messages
6,135,775
Members
449,963
Latest member
ethanong89

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