PDA

View Full Version : Sleeper: Loop for copying and pasting tables



Lyrad39
06-14-2023, 08:26 PM
Good day community,

I am intending to loop and copy the tables (K4:T20) as well as other tables on the right till the end, pasting them below row 20 and so on. I am stuck with an error whereby the last column for "Sd" results are pasted on column A instead of column K as shown in the 2nd photo. Also, I am unable to paste the codes starting from K4 onwards for Sd results. Here are my codes and any help would be greatly appreciated!
3086130862

Sub MoveTables()
Dim Lastcol As Long, c As Long
Lastcol = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
For c = 11 To Lastcol Step 10
Cells(4, c).Resize(17, 10).Cut Destination:=Range("C" & Rows.Count).End(xlUp).Offset(1, -2)
Next c
End Sub

Lyrad39
06-14-2023, 08:26 PM
Here are the codes thank you:


Sub MoveTables()
Dim Lastcol As Long, c As Long

Lastcol = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
For c = 11 To Lastcol Step 10
Cells(4, c).Resize(17, 10).Cut Destination:=Range("C" & Rows.Count).End(xlUp).Offset(1, -2)
Next c
End Sub

Aussiebear
06-14-2023, 11:10 PM
Welcome to VBAX Lyrad39. I cleaned up your initial post for you.

p45cal
06-15-2023, 02:52 AM
Not at a computer at the moment but try using .Copy instead of .Cut, then at the end delete the original ranges.
When you cut a range Excel moves cells either from the right or from below to replace the cut cells and this is likely to play havoc with your For loop.
If you really want to .Cut it would probably work if you worked from right to left, but then your final table might not be in the order you want (although that too can be worked around).

Edit1:Scrub the above, I was wrong, now that I'm at a PC. It seems to work fine here, looking into it a bit more…
Could you attach a workbook with just this sheet in it?

Edit2: I'm beginning to think it might be to do with formulae that you're cutting and pasting.

Edit3: I note from your pictures that the second picture does not have data (first table) in the same place as in the first picture; the tables start a row higher and 1 column to the right.

georgiboy
06-15-2023, 03:12 AM
If you are using excel 365 and you don't need to carry over the formatting then you could also use the VSTACK function to do this.