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.
 
Another approach?

Code:
Sub Dazzzawm()
Dim i As Long
Dim y As Long
Dim z As Long

y = Selection.Row
z = Selection.Row + Selection.Rows.count - 1
For i = z To y Step -1
    Cells(i, 3).offset(1).EntireRow.Insert
    Rows(i + 1).Value = Rows(i).Value
    Cells(i, 3).offset(1).Value = Cells(i, 3).Value & "B"
    Cells(i, 3).Value = Cells(i, 3).Value & "A"
    Range(Cells(i, 3).offset(, 5), Cells(i, 3).offset(, 7)).Value = Range(Cells(i, 3), Cells(i, 3).offset(, 2)).Value
    Range(Cells(i + 1, 3).offset(, 5), Cells(i + 1, 3).offset(, 7)).Value = Range(Cells(i, 3), Cells(i, 3).offset(, 2)).Value
Next i

End Sub
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Thanks John, but when I run it I first get a box come up saying 'Excel cannot complete this task with available resources. Choose less data or close applications'. I press ok then a box comes up with a red cross saying '400'?
 
Upvote 0
Dazza
Please replace the original dim statement with this new one and test again...

Dim src As Range, t, i&, j&, lr&, chc&, rv
 
Upvote 0
Yeap.. 50 000 rows is too large for Integer..

From the <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> help file:

Integer variables are stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767. Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647.
 
Upvote 0
Thanks Andrew, Worf and John for your help and persistence. Got there in the end!
 
Upvote 0
Hi Andrew/Worf. The code works perfect but I need a couple of amendments please, sorry.

1. At the moment I believe the code copies the rows and places them at the bottom of the file then sorts by C. If possible I need the rows inserted directly below and no sort.

2. Because the files I will use this on will always be changing I may need to go back and a row that has a C on the end of the data I may need to copy and insert with a D on the end. Currently it would copy it and put CA, CB and so on...

3. And finally any rows that are copied and inserted I would like coloured sperately to highlight for me, for example like below.

I am sorry this may be a hassle and had I known I would obviously have told you in the beginning.

Excel 2007
ABCDEFG
1Header1Header2Header3Header4Header5Header6Header7
2datadataMZC1820 2001Adatadatadatadata
3datadataMZC1820 2001Bdatadatadatadata
4datadataMZC1820 2001Cdatadatadatadata
5datadataMZC1820 3001Adatadatadatadata
6datadataMZC1820 3001Bdatadatadatadata
7datadataMZC1820 3001Cdatadatadatadata
8datadataMZC1820 4001Adatadatadatadata
9datadataMZC1820 4001Bdatadatadatadata
10datadataMZC1820 4001Cdatadatadatadata

<tbody>
</tbody>
Sheet1
 
Upvote 0
Hi Dazza

Are you going to use multiple selections like (c4:c7 AND c11:c15 AND c25) or will the selection always be a single block of cells like c5:c9?
 
Upvote 0
It should always be a single block of rows, but the data will sometimes be different in c like the example above.
 
Upvote 0
If I selected all the rows below and selected 2 for example the result i would like is shown in second table.


Excel 2010
ABCDEFGHI
12DATADATAMZC1820 2001DATADATADATADATADATADATA
13DATADATAMZC1820 2001DATADATADATADATADATADATA
14DATADATAMZC1820 3001DATADATADATADATADATADATA
15DATADATAMZC1820 4001DATADATADATADATADATADATA
16DATADATAMZC1820 4001DATADATADATADATADATADATA
17DATADATAMZC1820 4001DATADATADATADATADATADATA
Sheet1


After code with highlighted rows.


Excel 2010
ABCDEFGHI
12DATADATAMZC1820 2001ADATADATADATADATADATADATA
13DATADATAMZC1820 2001ADATADATADATADATADATADATA
14DATADATAMZC1820 2001BDATADATADATADATADATADATA
15DATADATAMZC1820 2001BDATADATADATADATADATADATA
16DATADATAMZC1820 2001CDATADATADATADATADATADATA
17DATADATAMZC1820 2001CDATADATADATADATADATADATA
18DATADATAMZC1820 3001ADATADATADATADATADATADATA
19DATADATAMZC1820 3001BDATADATADATADATADATADATA
20DATADATAMZC1820 3001CDATADATADATADATADATADATA
21DATADATAMZC1820 4001ADATADATADATADATADATADATA
22DATADATAMZC1820 4001ADATADATADATADATADATADATA
23DATADATAMZC1820 4001ADATADATADATADATADATADATA
24DATADATAMZC1820 4001BDATADATADATADATADATADATA
25DATADATAMZC1820 4001BDATADATADATADATADATADATA
26DATADATAMZC1820 4001BDATADATADATADATADATADATA
27DATADATAMZC1820 4001CDATADATADATADATADATADATA
28DATADATAMZC1820 4001CDATADATADATADATADATADATA
29DATADATAMZC1820 4001CDATADATADATADATADATADATA
Sheet1


Should I need to go back and highlight these rows and insert


Excel 2010
ABCDEFGHI
27DATADATAMZC1820 4001CDATADATADATADATADATADATA
28DATADATAMZC1820 4001CDATADATADATADATADATADATA
29DATADATAMZC1820 4001CDATADATADATADATADATADATA
Sheet1



Excel 2010
ABCDEFGHI
30DATADATAMZC1820 4001DDATADATADATADATADATADATA
31DATADATAMZC1820 4001DDATADATADATADATADATADATA
32DATADATAMZC1820 4001DDATADATADATADATADATADATA
Sheet1


Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,951
Members
449,412
Latest member
montand

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