VBA elusive copy multiple rows as per given data in column G

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

VBA that copy as per number of row mention in cells G6:G15 from a columns C, D, E and paste in to as explain continuation....
If rows to be copied start with 1 than to be past in column I, J, & K
If rows to be copied start with X than to be past in column M, N, & O
If rows to be copied start with 2 than to be past in column Q, R, & S

For example:
G6=3 copy 3 rows C6:E8 as start of the copy row C6=X this paste in to M6:O8
G7=6 copy 6 rows C9:E14 as start of the copy row C9=X this paste in to M9:O14
G8=6 copy 6 rows C15:E20 as start of the copy row C15=1 this paste in to I6:K11
G9=5 copy 5 rows C21:E25 as start of the copy row C21=2 this paste in to Q6:S10
And so on...

Example data


Book1
ABCDEFGHIJKLMNOPQRST
1
2
3
4
5C1C2C3Data1X2
6XSX31S1XSX2SX
726X21
8131611311
9XSX5XXSX1
1011211X52
11X3262X2S2
12161S111
13111111
14262612621
151S1171XSX1
16X11X6X
17112322S2
18X11
19112
2026221
212SX21
2211X6X
231X12X
2411S1
25X522
261S11
2711
2812
2911
3011
3111
3211
3311
342X11X
3521S1
3612
37X12X2
38XSX2
3912
402322
412S21
4211
4312
4412
4511
46X6X1
471S12
4822
4911
5011
512X17X
521
531
541
551
561
57X11X
582S2
591
602
611
621
63X6X
641S1
652
662
672
682
692
701
711
722
732
741
751
762
772
781
791
80X17X
811S1
82X
83
84
85
86
87
Sheet2


Thank you in advance

Regards
Kishan
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello Kishan,

Will column "G" be used by the macro to determine the number of rows in columns "C:E" to be copied?
 
Upvote 0
Hello Kishan,

Will column "G" be used by the macro to determine the number of rows in columns "C:E" to be copied?
Thank you Leith Ross, yes column "G" is filled to determine the number of rows to be copied in columns "C:E" so far yes column "G" can be used in the macro no problem.

Regards,
Kishan


 
Upvote 0
Try:
Code:
Sub CopyMove()

    Dim arr()   As Variant
    Dim temp()  As Variant
    Dim x       As Long
    Dim r       As Long
    Dim c       As Long
    
    x = Cells(Rows.count, 7).End(xlUp).row - 5
    r = 6
    arr = Cells(6, 7).Resize(x).Value
    
    Application.ScreenUpdating = False
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        temp = Cells(r, 3).Resize(arr(x, 1), 3).Value
        Select Case UCase(Cells(r, 3).Value)
            Case Is = 1: c = 10
            Case Is = "X": c = 14
            Case Is = 2: c = 18
        End Select
        Cells(Rows.count, c).End(xlUp).Offset(1, -1).Resize(UBound(temp, 1), UBound(temp, 2)).Value = temp
        Erase temp
        r = r + arr(x, 1)
    Next x
    
    Application.ScreenUpdating = True
    
    Erase arr
    
End Sub
 
Last edited:
Upvote 0
Try:
Code:
Sub CopyMove()

    Dim arr()   As Variant
    Dim temp()  As Variant
    Dim x       As Long
    Dim r       As Long
    Dim c       As Long
    
    x = Cells(Rows.count, 7).End(xlUp).row - 5
    r = 6
    arr = Cells(6, 7).Resize(x).Value
    
    Application.ScreenUpdating = False
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        temp = Cells(r, 3).Resize(arr(x, 1), 3).Value
        Select Case UCase(Cells(r, 3).Value)
            Case Is = 1: c = 10
            Case Is = "X": c = 14
            Case Is = 2: c = 18
        End Select
        Cells(Rows.count, c).End(xlUp).Offset(1, -1).Resize(UBound(temp, 1), UBound(temp, 2)).Value = temp
        Erase temp
        r = r + arr(x, 1)
    Next x
    
    Application.ScreenUpdating = True
    
    Erase arr
    
End Sub
Hi JackDanIce,

Awesome! I copied the code and placed it in a new module. It just worked smoothly! At first instance.

Thank you so much for your help.

Kind Regards,
Kishan :)


 
Upvote 0
Hi JackDanIce, Because of my old excel version this has only 256 columns for the coverage of all results I need more columns.

For that reason please could you modify the macro so results can be copied in to the sheet1, starting from column C6

Sorry for the trouble again

Thank you

Kind Regards,
Kishan
 
Upvote 0
I do not understand, your data is across 3 columns, C:E.

You have three values to consider, 1, X and 2

Your ask is to take the data in column C and distribute it across 3 columns I:K, M:O and Q:S

That suggests you only need 3 * 4 columns maximum i.e. 12

Where are the results being copied from? Do you mean the data being copied into C6 has more than 3 columns?
 
Upvote 0
I do not understand, your data is across 3 columns, C:E.

You have three values to consider, 1, X and 2
Hi JackDanIce,

Actual data are in 5 columns C:G this is one set of data. I have total 20 sets

Adding 1 free space within each set data start from "C" and 20 set end in column "DQ" which is column 121, I modified the macro to get first set column C:G data result, in the columns DS:EC using 11 columns (column DR left empty in-between the last set and 1st result) after when I was preparing a sheet to get a result for all 20 of sets I found that 11th set were using column II:IS, IS which is column 253 left only 3 at the end column

So thought if I leave the sets of 20 data in the "sheet Data" and gets result of CopyMove in "sheet1" from column C that will end in column IG (within column 3 to 241) getting all result for 20 sets which will be handy instead of dividing the data in 2 sheets could be the solution

That's why I request you if the data result could be copied in "sheet1" would be easy for me to have data in the "sheet Data" and results in an another "sheet1".

My request please is it possible to modified the macro and get the result in sheet1

Thank you

Kind Regards,
Kishan
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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