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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

cpod

Board Regular
Joined
Apr 2, 2002
Messages
124
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
 

Glaswegian

Well-known Member
Joined
Oct 14, 2003
Messages
1,487
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:
 

biztekjim

New Member
Joined
Dec 30, 2004
Messages
18
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>
 

zzcom

Active Member
Joined
Aug 4, 2004
Messages
272

ADVERTISEMENT

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
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
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.
 

zzcom

Active Member
Joined
Aug 4, 2004
Messages
272

ADVERTISEMENT

Thanx a lot for your help guys.

(y)
 

Todd Bardoni

Well-known Member
Joined
Aug 29, 2002
Messages
3,042
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.
 
L

Legacy 12630

Guest
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
 

zzcom

Active Member
Joined
Aug 4, 2004
Messages
272
Wow, :eek: a lot of beautiful macros are here.



I really appreciate your help. Thanx a lot guys.
 

Forum statistics

Threads
1,148,053
Messages
5,744,534
Members
423,881
Latest member
Nguyen Vu

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