Macro for looping through cells and copying out values based on the value being isnumeric or not

Ronanm

Board Regular
Joined
Nov 13, 2010
Messages
107
Hi

I need to loop through a column and based on whether the value starts with a number, copy this value to the cell to the right of it and append any cells below it which are not numeric and then repeat when you find a cell where the first value is numeric again... to get the results as per below.

9b34 test
9b34 test These are just words
These are just words
1uy8 some more bits1uy8 some more bits Cells and stuff here more text and again
Cells and stuff here
more text
and again
6h89 cell6h89 cell there it is
there it is

<tbody>
</tbody>


Many Thanks
 
Last edited:

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.
Here you go try this out

Code:
Sub Numbers()
Dim r As Range
For Each r In Range("A1:A10") 'Change this range to where your data is
    If IsNumeric(Left(r.Value, 1)) = True Then
        r.Offset(, 1).Value = r.Value
    End If
Next r
End Sub

EDIT**** Wait i just saw the part about appending the ones below... working on it
 
Last edited:
Upvote 0
Alright try this one out

Code:
Sub Numbers()
Dim r As Long
Dim line As String
line = ""
For r = 10 To 1 Step -1 'change the number 10 here to the last row of data 
    If IsNumeric(Left(Cells(r, [COLOR=#ff0000]1[/COLOR]).Value, 1)) = False Then
        line = Cells(r, [COLOR=#ff0000]1[/COLOR]).Value & " " & line
    Else
        line = Cells(r,[COLOR=#ff0000] 1[/COLOR]).Value & " " & line
        Cells(r, [COLOR=#0000ff]2[/COLOR]).Value = line
        line = ""
    End If
Next r
End Sub

The red number 1 is the column A and the blue 2 is column B .... change those to whatever columns you need
 
Last edited:
Upvote 0
Alright try this one out

Code:
Sub Numbers()
Dim r As Long
Dim line As String
line = ""
For r = 10 To 1 Step -1 'change the number 10 here to the last row of data 
    If IsNumeric(Left(Cells(r, [COLOR=#ff0000]1[/COLOR]).Value, 1)) = False Then
        line = Cells(r, [COLOR=#ff0000]1[/COLOR]).Value & " " & line
    Else
        line = Cells(r,[COLOR=#ff0000] 1[/COLOR]).Value & " " & line
        Cells(r, [COLOR=#0000ff]2[/COLOR]).Value = line
        line = ""
    End If
Next r
End Sub

The red number 1 is the column A and the blue 2 is column B .... change those to whatever columns you need

:) This works perfectly. Thank you so much for that. Appreciate it.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,172
Members
448,870
Latest member
max_pedreira

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