What is wrong with this little macro

zzcom

Active Member
Joined
Aug 4, 2004
Messages
272
I would like to copy a part of every second row and paste it beside previous one. What is wrong with this little macro?

Sub LittleMacro()

Dim i, j As Integer
For i = 2 To 1000 Step 2
For j = 1 To 1000 Step 2
Range("A" & i & ":K " & i).Select
Selection.Cut
Range("L" & j).Select
ActiveSheet.Paste

Next j
Next i
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You don't want two loops here. Try this:

Sub LittleMacro()
Dim i As Integer
With Worksheets("sheet1")
For i = 2 To 1000 Step 2
.Range("A" & i & ":K" & i).Cut (.Range("L" & i - 1))
Next
End With
End Sub
 
Upvote 0
Try this - saves the looping
Code:
Sub LittleMacro()

Dim i As Integer

For i = 2 To 1000 Step 2
    Range("A" & i & ":K" & i).Cut Destination:=Range("L" & i).Offset(-1, 0)
Next i
End Sub
HTH

Regards

EDIT: Too slow again :confused:
 
Upvote 0
Looks like everybody jumped on this one. Well, since I took the time to write and test a solution, I'll throw in my 2 cents. My code isn't as clean as the others, but here it is anyway. The second version allows you to delete the empty rows, but at the end it wastes effort copying empty rows.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> LittleMacro()
<SPAN style="color:#007F00">'Dim i, j As Integer</SPAN>
Dim i As Integer
<SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> 1000 <SPAN style="color:#00007F">Step</SPAN> 2
<SPAN style="color:#007F00">'For j = 1 To 1000 Step 2</SPAN>
    <SPAN style="color:#007F00">'Range("A" & i & ":K " & i).Select</SPAN>
    Range(Cells(i, 1), Cells(i, 11)).Select
    Selection.Cut
    Range("L" & i - 1).Select
    ActiveSheet.Paste
<SPAN style="color:#007F00">'Next j</SPAN>
Next i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> LittleMacroWithDelete()
<SPAN style="color:#007F00">'Dim i, j As Integer</SPAN>
Dim i As Integer
<SPAN style="color:#007F00">'For i = 2 To 1000 'Step 2</SPAN>
For i = 2 To 1000
<SPAN style="color:#007F00">'For j = 1 To 1000 Step 2</SPAN>
    <SPAN style="color:#007F00">'Range("A" & i & ":K " & i).Select</SPAN>
    Range(Cells(i, 1), Cells(i, 11)).Select
    Selection.Cut
    Range("L" & i - 1).Select
    ActiveSheet.Paste
    Rows(i).Delete (xlUp)
<SPAN style="color:#007F00">'Next j</SPAN>
Next i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thanks guys. It seems that someone need to practice very offten in order to stay in a good shape. Just one more question: what means this dot here
.Range

If I remember good never have seen something like this before
 
Upvote 0
In a With range/End With syntax, the .Range / .Cells means that the reference belongs with the range in the With statement.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> y()
<SPAN style="color:#00007F">With</SPAN> Worksheets("Sheet1")
    .Range("A1") = "XXX"
    Range("A1") = "XXX"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Put that in the Thisworkbook module and run it from a sheet other than 1; or, put it in the Sheet2 module and run it...

ALWAYS do these things on a backup/test file.
 
Upvote 0
Not knowing exactly how your data is setup or if you really want the data to be 'cut' out of the original list, how about something along the lines of:-

Code:
Sub Formula_Approach()
i = Range("A65536").End(xlUp).Row
    Range("L1:T" & i) = "=INDEX(C[-11],ROW()+1,1)"
    Range("L1:T" & i).Formula = Range("L1:T" & i).Value
    Range("L1:T1").Insert Shift:=xlDown
End Sub

You'll have to change it to suit your needs but I would think this would be faster than any loop.
 
Upvote 0
Avoiding all looping :-

Code:
Sub LittleMacro()
Dim rng As Range
Application.ScreenUpdating = False
Columns(1).Insert
Set rng = Range([A2], [B65536].End(xlUp)(1, 0))
rng.Offset(, 1).Resize(, 11).Copy [M1]
With rng
    .Formula = "=IF(MOD(ROW(),2)=0,""k"",1)"
    .Value = .Value
    .EntireRow.Sort Key1:=[A2], Order1:=xlDescending, Header:=xlNo
    .SpecialCells(xlCellTypeConstants, 1).Resize(, 23).ClearContents
    .EntireColumn.Delete
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Wow, :eek: a lot of beautiful macros are here.



I really appreciate your help. Thanx a lot guys.
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,186
Members
448,554
Latest member
Gleisner2

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