Macro speed - copy/paste rows

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,483
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I'm working on a macro where I have to swap around some rows on a sheet. So I've added some grouping labels in column B, loop though my 150 rows and move blocks in a certain order. Moving means: cut and paste at the top. Roughly speaking, this is what I'm doing (option Explicit is on):
VBA Code:
    Dim Sht As Worksheet
    Dim Rw As Long, StartRw As Long, EndRw As Long, MoveAction As Integer, MoveBlock As Integer, i As Integer
    Dim CopyRwStart As Long, CopyRwEnd As Long, PasteRw As Long

' in a loop, determine the start- and endrow, next,
    Set Sht = Worksheets("MY_SHEET")
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    For i = 1 to 10
         'Do something to determine the start- and endrow and pasterow - blocks are max 10 rows, and the total range everything happens in is 150 rows.
         Sht.rows(CopyRwStart & ":" & CopyRwEnd).Cut
        Sht.Cells(PasteRw, 1).rows("1:1").EntireRow.Insert shift:=xlDown
        Application.CutCopyMode = False
    next i
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlSemiAutomatic
In my macro, there are about 10 of those cut-paste actions (want to move some rows up). The main issues I currently bump into:
- when timing the macro, each cut-paste action takes almost a second, which feels way too long. At my colleagues PC, each cut-paste action takes almost 5 seconds. - so on my PC the total macro runs for 10 seconds, on his almost a minute :-(.
- sometimes, a clipboard-warning pops up (guess Windows native), throwing a warning (not stopping the process).

Does anyone here know a solution to speed this up? And does anyone know if I can in VBA somehow avoid that pesky clipboard warning popping up?
 
Does you want only values or Values with formatting to be shifted.
Everything: formats, formulas, etc. Note: there are no filters or groupings on the sheet (in that sense it's a simple straight forward sheet). Your remark reminded me that maybe one "sort" command will be quicker than my 6 cut-paste ones... Will test that.
 
Upvote 0
Okay, the sorting makes it much, much faster, but all formulas in my sorted ranges get messed up, so that's not a solution :-(. Tried various versions with different formulas and lots of $ signs, but a couple of formulas kept on getting messed up.
 
Upvote 0
XL2BB throws a 1004 if I try to export it. The sorting goes wrong because of this issue: Redirecting - that is not something I can solve on my sheet, as there are plenty of references to other sheets.

So the my questions remain open: Does anyone here know a solution to speed this up? And does anyone know if I can in VBA somehow avoid that pesky clipboard warning popping up?
 
Upvote 0
The sorting goes wrong because of this issue: Redirecting - that is not something I can solve on my sheet, as there are plenty of references to other sheets.
I'm not sure that you are interpreting the issue correctly (or else I may be not understanding what you are saying).
It is not references to other sheets that cause the problem, but references (using the sheet name) to the sheet the formula is actually on.

Another simple demo is given here. The problem in this example is this formula on Sheet2 contains the Sheet2! reference
=SUMIF(Sheet1!$B$2:$B$7,Sheet2!A2,Sheet1!$C$2:$C$7)

If the formula is re-written without that sheet name ..
=SUMIF(Sheet1!$B$2:$B$7,A2,Sheet1!$C$2:$C$7)
.. then there is no problem with sorting.
 
Upvote 0
@Peter_SSs Give this a try, simple formulas that get messed up when I sort it, using the bit of code below (E/F switch). I copy-pasted the correct formulas in the part from row 21 for reference. I've simplified my actual example, as that has the data coming in from other sheets (and is protected, etc). So that's why I moved to cut-paste rows, but that is very, very slow and has some clipboard pop-ups showing (my first post).


VBA Code:
Sub DoSortNOw()

    'BUT MESSES UP FORMULAS!!!
    Set Sht = Worksheets("BS")
    OrderBy = "F" 'E Or F
    Sht.Range("A3:G15").Sort key1:=Range(OrderBy & "3:" & OrderBy & "15"), order1:=xlAscending, Header:=xlNo
    Set Sht = Nothing

End Sub



sort_cut_paste.xlsm
ABCDEF
1BALANCE SHEET for Company Z202020212022
2
3Row 313551
4Row 424662
5Total 3+43253373
684
7Current deferred tax assets46615
8Other current assets; operating57726
9Operating current assets24314337
1048
11Total current assets27567699
121010
13Non-operating current assets1012121111
14Total current assets1518181212
15EXTRA DATATESTORIGINAL
16Outside range202122
17
18
19
20
21BALANCE SHEET for Company Z202020212022
22
23Row 3135
24Row 4246
25Total 3+432533
26
27Current deferred tax assets466
28Other current assets; operating577
29Operating current assets243143
30
31Total current assets275676
32
33Non-operating current assets101212
34Total current assets151818
35EXTRA DATA
36Outside range202122
BS
Cell Formulas
RangeFormula
B5,B25B5=SUM(B3:B4)
C5,C25C5=SUM(C3:C4)+C14
D5,D25D5=SUM(D3:D4)+D16
B9:C9,B29:C29B9=B7+B8+B14
D9,D29D9=D7+D8+D14+D13
B11:D11,B31:D31B11=B5+B9
 
Upvote 0
Okay, I understand now what you are saying. I'm not sure if this will help with your actual data, but with that example, take the formula in C5. It is saying to add the values in the two rows above plus the value in the cell 9 rows below. With this particular sort (sorting on column E) you are keeping original rows 3,4,5 together and they become rows 7,8,9. The formula that was in C5, now in C9, is still (quite correctly) adding the the values in the two rows above plus the value 9 rows below (C18).

If you are going to be changing the relative positions but some values are retaining the original positions then you could anchor the fixed values with with absolute references. For example the original C5 formula could be ..
=SUM(C3:C4)+C$14
.. then that formula when moved by sorting to C9 would be ..
=SUM(C7:C8)+C$14
.. still returning the correct result.

I understand that may not be very easy to implement or very robust. A better option might be to use SIMIF(S) using the text in column A but in this particular example not easy because row 14 is "Total current assets", but so is row 11. (Having two rows with identical labels but different values seems very confusing to me)

The upshot for me is I don't see an easy and robust way that is any quicker than your existing Cut/Paste method
 
Upvote 0
Solution
Hi Peter, thanks for those insights. The thought direction is a good one, but my actual setup is much more complex and keeps throwing circle errors when I sort (even after adding a bunch of $ signs). I'll stick with the cut-paste for now, but my next version will have some "extract the formula references, hardcopy that, sort, use the hardcopy to correct formulas", that's probably faster VBA code than the current 3 cut-paste actions (2 seconds total).
 
Upvote 0

Forum statistics

Threads
1,226,837
Messages
6,193,256
Members
453,784
Latest member
Chandni

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