Clever copy & Paste in Vb (or excel)

ianuk1983

Board Regular
Joined
Oct 30, 2006
Messages
104
Hello guys,

Wonder if anyone can help with this one. I have 2 rows one with account numbers the 2nd with mobile number.

i.e
Account Mobile Number
1234 071
1234 072
1234 073
4321 074

What I'm looking to do is paste them into one line. So all mobile numbers in one account

i.e

Account Mob1 Mob2 Mob3.....
1234 071 072 073

Is this possible do you think

Thanks?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim ShNew As Worksheet
    Dim First As Boolean
    Dim r As Long
    Dim c As Integer
    Dim Cell As Range
    Set Sh = Worksheets("Sheet1")
    Set Rng = Sh.Range("A1:A" & Sh.Cells(Sh.Rows.Count, 1).End(xlUp).Row)
    Set ShNew = Worksheets.Add
    First = True
    r = 1
    c = 2
    For Each Cell In Rng
        If Cell.Value = Cell.Offset(1, 0).Value Then
            If First Then
                ShNew.Cells(r, 1).Value = Cell.Value
                First = False
            End If
            ShNew.Cells(r, c).Value = Cell.Offset(0, 1).Value
            c = c + 1
        Else
            ShNew.Cells(r, c).Value = Cell.Offset(0, 1).Value
            First = True
            r = r + 1
            c = 2
        End If
    Next Cell
End Sub
 
Upvote 0

ianuk1983

Board Regular
Joined
Oct 30, 2006
Messages
104
The code is amazing, the only problem is if there is only one mobile number that belongs to the account number it doesn't bring the account number onto the next sheet. just the mobile number. Would you know why?

Thank you very much for this
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
My code assumed there was always more than one entry, so let's try a different approach:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim ShNew As Worksheet
    Dim r As Long
    Dim c As Integer
    Dim Cell As Range
    Set Sh = Worksheets("Sheet1")
    Set Rng = Sh.Range("A1:A" & Sh.Cells(Sh.Rows.Count, 1).End(xlUp).Row)
    Set ShNew = Worksheets.Add
    r = 1
    c = 2
    For Each Cell In Rng
        ShNew.Cells(r, c).Value = Cell.Offset(0, 1).Value
        With Rng
            If WorksheetFunction.CountIf(.Range(.Cells(Cell.Row, 1), .Cells(.Rows.Count, 1)), Cell.Value) = 1 Then
                ShNew.Cells(r, 1).Value = Cell.Value
                r = r + 1
                c = 2
            Else
                c = c + 1
            End If
        End With
    Next Cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,695
Messages
5,988,147
Members
440,130
Latest member
bianca88

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
Top