Separating names and numbers that are in the same column into separate columns

jdk8008

New Member
Joined
May 2, 2015
Messages
11
Office Version
  1. 2019
Platform
  1. MacOS
Hello there,

I have a problem that hopefully someone can help me with. I need names and invoice numbers in column A to be placed in separate columns (B & C).
If you refer to my example below, column A is the data I have now, and columns B & C are what I need it to be.
  • The name needs to be in the same row as the number indicating that this number belongs to this person.
  • The names need to be listed next to each number.
  • There is a blank cell below each person in my current data. I need these blank cells to remain present in B & C after the data is separated. The result should be two blank cells in between each name after the first person.

ABC
Doe, John
1111Doe, John1111
2222Doe, John2222
Smith, Ron
3333Smith, Ron3333
Jones, Brad
4444Jones, Brad4444
5555Jones, Brad5555
6666Jones, Brad6666

<tbody>
</tbody>

Any help someone can give in VBA form would be great. I have tried, but my knowledge of the syntax is very limited. Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi and welcome to the MrExcel Message Board.

Try this:
Code:
Sub Rearrange()

    Dim ws As Worksheet
    Dim Name As String
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    With ws
        For i = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
            If .Cells(i, "A").Value = "" Then
                    ' do nothing
            ElseIf IsNumeric(.Cells(i, "A").Value) Then
                .Cells(i, "B").Value = Name
                .Cells(i, "C").Value = .Cells(i, "A").Value
            Else
                Name = .Cells(i, "A").Value
            End If
        Next
    End With
    
End Sub

I have assumed a sheet name of Sheet1 but you can overtype that to change it.
Working out the last row in column A using:
Code:
.Cells(.Rows.Count, "A").End(xlUp).Row
is worth remembering. You need to do that very often.
If the cell in column A is blank the macro does nothing;
If it contains a number then it outputs the number and name;
Otherwise it must be a name so it remembers it for the next time.

Perversely, the macro needs to check for things in reverse order. That is, the thing expected last (a blank) needs to be tested for first and the thing it expects first (a name) it checks for last. This applies to all procedural languages.
 
Upvote 0
Give this macro a try...
Code:
Sub NameAndNumbers()
  Dim Ar As Range
  For Each Ar In Columns("A").SpecialCells(xlConstants).Areas
    Ar(1).Offset(1, 1).Resize(Ar.Rows.Count - 1) = Ar(1)
    Ar(1).Offset(1).Resize(Ar.Rows.Count - 1).Copy Ar(1).Offset(1, 2)
  Next
End Sub
 
Upvote 0
This worked beautifully. It definitely helps me understand the syntax as well with the explanation. Thanks! :biggrin:

Hi and welcome to the MrExcel Message Board.

Try this:
Code:
Sub Rearrange()

    Dim ws As Worksheet
    Dim Name As String
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    With ws
        For i = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
            If .Cells(i, "A").Value = "" Then
                    ' do nothing
            ElseIf IsNumeric(.Cells(i, "A").Value) Then
                .Cells(i, "B").Value = Name
                .Cells(i, "C").Value = .Cells(i, "A").Value
            Else
                Name = .Cells(i, "A").Value
            End If
        Next
    End With
    
End Sub

I have assumed a sheet name of Sheet1 but you can overtype that to change it.
Working out the last row in column A using:
Code:
.Cells(.Rows.Count, "A").End(xlUp).Row
is worth remembering. You need to do that very often.
If the cell in column A is blank the macro does nothing;
If it contains a number then it outputs the number and name;
Otherwise it must be a name so it remembers it for the next time.

Perversely, the macro needs to check for things in reverse order. That is, the thing expected last (a blank) needs to be tested for first and the thing it expects first (a name) it checks for last. This applies to all procedural languages.
 
Upvote 0

Forum statistics

Threads
1,215,361
Messages
6,124,500
Members
449,166
Latest member
hokjock

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