I'd like some advice on creating a macro in VBA which will loop through an array of values in one worksheet, copying the values of two fields, pasting those values into another worksheet, then copying and pasting the output of a calculation made in those values back into the orignal worksheet next to the original rows.
In the (very trimmed down) example, I have 2 worksheets in a single workbook
Worksheet: Values
Contains 4 columns. (A,B,C &D).
Columns A and B each contain a list of numbers
Columns C and D are empty, waiting to be populated based on a
calculation made from columns A and B (calculation takes place in a seperate worksheet).
Worksheet: Formula
Contains 2 fields to enter data (pasted from VALUES:colums A & B)
Also contains 2 calculation fields which produce the output.
This output then needs to be pasted back into "VALUES" at the respective rows in the blank columns.
Values Workseet Below:
Formula Workseet Below:
The process I've explained above in VBA goes something like this:
Sub value_paster()
'
' value_paster Macro
'
'
Sheets("Values").Select
Range("A2:B2").Select
Selection.Copy
Sheets("Formula").Select
Range("A2").Select
ActiveSheet.Paste
Range("C2:D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Values").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
My next job is to wrap the above code into something useful which will repeat the same processes made in the "Forumla" worksheet while working its way down rows in the "Values" worksheet.
I've found numerous examples on how to loop/iterate through rows but nothing much on how to do it while jumping between worksheets and copying/pasting from one to the other etc.
Note: The real calculations made on the data in the worksheet are complex and can't be incorporated into the code.
Any advice gratefully appreciated.
EDIT: To clarify, I don't need any additional rows created in the Formulas
worksheet - this worksheet is solely used to perform calculations on the data pasted from Values
. The ouput generated in Formulas
then needs to be pasted back into the Values
worksheet in the 2 output columns - this is where the looping through rows needs to occur.
EDIT 2: I've created a gif demonstrating the manual process I'd like to replicate using VBA
Note this isn't the actual workbook I'll be using, it's just a quick demo for the purposes of this question)
Values
should always be pasted intoA2:B2
range fromFormula
, or should they also be pasted moving down the rows along withValues
. You need to copy/paste just the values? – Victor Moraes