Repetitive VBA code

Copper Contributor

Excuse my VBA ignorance, I started recording a macro which is very repetitive.  I need to repeat this code (pasted below) changing the column each time starting with column AG all the way to DY.  

 

I have created a table of results which spits out a different set of results for a change in the main variable (Row 27).  The code pastes increments of the main variable into a key cell in my model and then cuts and pasts the results from row 28. 

 

I already did this via record macro and it took an hour and broke my worksheet... 

 

I'm guessing there must be a neater way rather than pasting this code over and over again changing the column letter each time... 

 

Any points would be humbly and gratefully received!  

 

Range("Ag27").Select
Application.CutCopyMode = False
Selection.Copy
Range("AE12").Select
ActiveSheet.Paste
Range("AD12").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Range("Ag28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

3 Replies

@Spacer7 

Can you share a screenshot or sample workbook with no confidential information? At the moment, the VBA doesn't provide enough detail on the copy and paste

Hi @adversi thanks for the reply.  I can't paste in any more detail as its sensitive. But essentially the only think that is changing is the letters 'AG', i.e. the column label, and they are increasing by 1, about 80 times, repeating the rest of the code over and over again.  The Macro works, but take a couple of minutes to process, and I'm sure there must be cleaner way of coding than repeating the whole section of code over and over again.  

 

Appreciate if that's still not enough information! 

@Spacer7 

To improve the macro, you can adjust the Range portion of the selection to expand the number of cells being copied.

 

In this case, if you want to copy cells from Row 27 to Row 28, starting with column AG to (hypothetical) AZ, the copy and paste macro would be:

Range("AG27:AZ27").Copy
Range("AG28:AZ28").PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

 

There are more ways to optimize this code, but this elimination of individual cells being copied one at time should improve the performance