Code To Copy Highlighted Row Insert & Add A, B etc On End Of Data In C

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I need a code that when I highlight a row(s) it copies and inserts the row beneath and adds A & B to the end of data in C.

In the sample below if I highlight all three rows the data in C is different in C so I will need a total of 6 rows with A & B on the end in as the result

Excel Workbook
ABCDEFG
12MZC1820 2001datadatadatadata
13MZC1820 3001datadatadatadata
14MZC1820 4001datadatadatadata
Sheet1


Result

Excel Workbook
ABCDEFGHIJ
12MZC1820 2001Adatadatadatadatadatadatadata
13MZC1820 2001Bdatadatadatadatadatadatadata
14MZC1820 3001Adatadatadatadatadatadatadata
15MZC1820 3001Bdatadatadatadatadatadatadata
16MZC1820 4001Adatadatadatadatadatadatadata
17MZC1820 4001Bdatadatadatadatadatadatadata
Sheet1



Thanks.
 
Thanks Mick thats brilliant. Please pm the link for your charity. Thanks to all that have contributed inc Worf, apo, John, snakehips and of course Mick.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Actually Mick I have just noticed that the data in columns AC onwards are not being copied. Below should help.


Excel 2007
ABCABACADAEAFAGAHAI
494843861CLCAN30D 7011RWD1Test 12TRUEFront Suspension
494943871CLCAN30D 7011RWD2Test 22TRUEFront Suspension
495043881CLCAN30D 7011RWD3Test 32TRUERear Suspension
495143891CLCAN30D 7011RWD4Test 42TRUERear Suspension
Sheet1


If I highlight all these rows and choose 1 this is the result I get.


Excel 2007
ABCABACADAEAFAGAHAI
494843861CLCAN30D 7011ARWD1Test 12TRUEFront Suspension
494943861CLCAN30D 7011ARWD1Test 12TRUEFront Suspension
495043861CLCAN30D 7011ARWD1Test 12TRUEFront Suspension
495143861CLCAN30D 7011ARWD1Test 12TRUEFront Suspension
495243861CLCAN30D 7011BRWD1Test 12TRUEFront Suspension
495343861CLCAN30D 7011BRWD1Test 12TRUEFront Suspension
495443861CLCAN30D 7011BRWD1Test 12TRUEFront Suspension
495543861CLCAN30D 7011BRWD1Test 12TRUEFront Suspension
Sheet1
<br

I should get this result


Excel 2007
ABCABACADAEAFAGAHAI
494843861CLCAN30D 7011ARWD1Test 12TRUEFront Suspension
494943861CLCAN30D 7011ARWD2Test 22TRUEFront Suspension
495043861CLCAN30D 7011ARWD3Test 32TRUERear Suspension
495143861CLCAN30D 7011ARWD4Test 42TRUERear Suspension
495243861CLCAN30D 7011BRWD1Test 12TRUEFront Suspension
495343861CLCAN30D 7011BRWD2Test 22TRUEFront Suspension
495443861CLCAN30D 7011BRWD3Test 32TRUERear Suspension
495543861CLCAN30D 7011BRWD4Test 42TRUERear Suspension
Sheet1


Sorry to be a pain, but if you have gone to all this trouble it needs to right!
 
Upvote 0
If column "AI" (col 35) is you last column then add the line (In Red) below as shown.
This line is at the top of the code.
You can, if you want remove the line above , it does not really matter.
Rich (BB code):
Ltr = Cells("1", Columns.Count).End(xlToLeft).Column
Ltr = 35
Sorry I think I've misread your Query, ignore if not relevant !!!. I will have another look now !!! !!!!!

CharityLinK:-

Donate to RNIB - RNIB

<tbody>
</tbody>
 
Last edited:
Upvote 0
Basically any rows I select have to be copied and inserted identically with A, Bs etc on the end of the data in C. Like the first example in post 32, all the cells are the same in C so when selected all other cells in the rows need to be copied identically.

Should I ignore your post 33?
 
Upvote 0
Donation made Mick, I know you will fix the problem above and the code will be exactly as I need. I wait in anticipation!!
 
Upvote 0
Try this:-
Its a bit rough I needed to rewrite a lot of it.
Code:
[COLOR=Navy]Sub[/COLOR] MG29Mar46
[COLOR=Navy]Dim[/COLOR] n           [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] Message     [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Dim[/COLOR] Title       [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Dim[/COLOR] Default     [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] MyValue     [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] Lst         [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] Fst         [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] Dic         [COLOR=Navy]As[/COLOR] Object
[COLOR=Navy]Dim[/COLOR] R           [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Dim[/COLOR] Ltr        [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
Ltr = Cells("1", Columns.Count).End(xlToLeft).Column
Fst = Selection.Row
Lst = Selection.Row + Selection.Rows.Count - 1


Message = "Enter a Number": Title = "Repeat Numbers": Default = 0
    [COLOR=Navy]On[/COLOR] [COLOR=Navy]Error[/COLOR] [COLOR=Navy]Resume[/COLOR] [COLOR=Navy]Next[/COLOR]
    MyValue = InputBox(Message, Title, Default)
       [COLOR=Navy]On[/COLOR] [COLOR=Navy]Error[/COLOR] GoTo endnow
        [COLOR=Navy]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
            Dic.CompareMode = vbTextCompare
                [COLOR=Navy]If[/COLOR] MyValue = 0 [COLOR=Navy]Then[/COLOR] [COLOR=Navy]Exit[/COLOR] [COLOR=Navy]Sub[/COLOR]
        [COLOR=Navy]For[/COLOR] n = Lst To Fst [COLOR=Navy]Step[/COLOR] -1
            [COLOR=Navy]Set[/COLOR] R = Range("C" & n)
                [COLOR=Navy]If[/COLOR] Not Dic.Exists(R.Value) [COLOR=Navy]Then[/COLOR]
                    Dic.Add R.Value, R
                [COLOR=Navy]Else[/COLOR]
                    [COLOR=Navy]Set[/COLOR] Dic.Item(R.Value) = Union(Dic.Item(R.Value), R)
                [COLOR=Navy]End[/COLOR] If
        [COLOR=Navy]Next[/COLOR] n


[COLOR=Navy]Dim[/COLOR] k       [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]Dim[/COLOR] Dn      [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Dim[/COLOR] c       [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] cols    [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]Dim[/COLOR] Ac      [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] Str     [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Dim[/COLOR] Ray
[COLOR=Navy]Dim[/COLOR] olet [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
cols = Array(1, 4, 6, 8, 15, 17, 34, 35, 43, 46)
Application.ScreenUpdating = False
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] k [COLOR=Navy]In[/COLOR] Dic.keys
  Str = vbNullString
  [COLOR=Navy]For[/COLOR] Ac = 1 To MyValue
    n = IIf(Ac > 9, Ac - 9, Ac)
    Str = Str & "," & Dic.Item(k).Address
    Dic.Item(k).EntireRow.Interior.ColorIndex = cols(n)
    Dic.Item(k).EntireRow.Copy
    Dic.Item(k).EntireRow.Insert
  [COLOR=Navy]Next[/COLOR] Ac
    Dic.Item(k).EntireRow.Interior.ColorIndex = cols(n + 1)
    Str = Str & "," & Dic.Item(k).Address
    Ray = Split(Mid(Str, 2), ",")


[COLOR=Navy]For[/COLOR] Ac = 0 To UBound(Ray)
    [COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] R [COLOR=Navy]In[/COLOR] Range(Ray(Ac))
        [COLOR=Navy]If[/COLOR] Right(R, 1) Like "[A-Z]" And Ac > 0 [COLOR=Navy]Then[/COLOR]
           olet = Chr(Asc(Right(R, 1)) + Ac)
           R = Mid(R, 1, Len(R) - 1) & olet
        [COLOR=Navy]ElseIf[/COLOR] Not Right(R, 1) Like "[A-Z]" [COLOR=Navy]Then[/COLOR]
           R = R & Chr(Ac + 1 + 64)
        [COLOR=Navy]End[/COLOR] If
  [COLOR=Navy]Next[/COLOR] R
[COLOR=Navy]Next[/COLOR] Ac
[COLOR=Navy]Next[/COLOR] k
Application.ScreenUpdating = True
endnow:
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
By jove I think he's cracked it!. Thanks for all this Mick, is there any need to rewrite it if it works ok? BTW I bunged the RNIB a fifty is that ok?
 
Upvote 0
Thank you for that, I think (Hope) its working Ok, it just its layout is a bit messy, anyway only time will tell if its got any bugs !!! (fingers crossed)
 
Upvote 0
Thanks again for your time on this and the previous thread, until next time and your next charity!!
 
Upvote 0

Forum statistics

Threads
1,215,891
Messages
6,127,602
Members
449,388
Latest member
macca_18380

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