Hi Folks,
I have been using a loop to calculate the results of a formula and past those results into consecutive rows in the same sheet.
Things generally work fine, but at a point something isn't quite working and I get SPILL issues and some charts lose their data as cells move...even though the range referred to is still correct.
Here is what I was using and I was hoping someone could improve on it so that it can:
Idea is, in the macro (not in consecutive rows on the sheet), loop through values of m (-5 to 5 step 1).
Record each result from each loop iteration below starting in B7 (it 1), then B8 (it 2), etc.
This is what I was doing...and it worked for a while, until it didn't...my vba skills feel like banging pots and pans at the orchestra. Thank you!
I have been using a loop to calculate the results of a formula and past those results into consecutive rows in the same sheet.
Things generally work fine, but at a point something isn't quite working and I get SPILL issues and some charts lose their data as cells move...even though the range referred to is still correct.
Here is what I was using and I was hoping someone could improve on it so that it can:
- Avoid using the Selection.Insert to make a new row, but instead simply increment the paste range or cell. I tried using cells, but that seemed to break things.
- For this example, iterate through values of M in a Sub and report those values consecutively in rows below.
Idea is, in the macro (not in consecutive rows on the sheet), loop through values of m (-5 to 5 step 1).
Record each result from each loop iteration below starting in B7 (it 1), then B8 (it 2), etc.
Book1 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | y=mx+b | ||||||
2 | y | m | x | b | |||
3 | 9.2 | 0.1 | 12 | 8 | |||
4 | |||||||
5 | Desired Result | ||||||
6 | # | y | m | x | b | ||
7 | 1 | -52 | -5 | 12 | 8 | ||
8 | 2 | -40 | -4 | 12 | 8 | ||
9 | 3 | -28 | -3 | 12 | 8 | ||
10 | 4 | -16 | -2 | 12 | 8 | ||
11 | 5 | -4 | -1 | 12 | 8 | ||
12 | 6 | 8 | 0 | 12 | 8 | ||
13 | 7 | 20 | 1 | 12 | 8 | ||
14 | 8 | 32 | 2 | 12 | 8 | ||
15 | 9 | 44 | 3 | 12 | 8 | ||
16 | 10 | 56 | 4 | 12 | 8 | ||
17 | 11 | 68 | 5 | 12 | 8 | ||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B3,B7:B17 | B3 | =(C3*D3)+E3 |
This is what I was doing...and it worked for a while, until it didn't...my vba skills feel like banging pots and pans at the orchestra. Thank you!
VBA Code:
Sub Iterate()
Application.ScreenUpdating = False
Sheet4.Activate
Sheet4.Range("B7:E17").ClearContents
Dim m As Long
For m = -5 To 5 Step 1
Sheet1.Range("B3") = m
Sheet1.Range("B7") = m
Sheet1.Range("B3:E3").Copy
Sheet1.Range(B7:E7).PasteSpecial Paste:=xlPasteValues, Paste:=xlPasteFormats
Sheet1.Rows("7:7").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
DoEvents
Next m
Sheet1.Rows(7).EntireRow.Delete
Application.ScreenUpdating = True
End Sub