Dear All,
After a lot of effort (I am a brand new in VBA) I have managed to create a macro to do the following:
1. Copy time vs. Volume data (with variable lengths). A18:B18
2. Calculate deltaT and deltaVolume. (A19:B19).
3. For a constant DR located in cell C6 in the same w/s the following is calculated for the entire set of data:
a) From E19 down to last row: Ei = Di-($DR$*Ci)
b) From E19 down to last row: Fi = IF((Fi-1+Ei)<0,0, Fi-1+Ei).
c) and d) other formulas.
4. The block of columns from E to I will be replicated according to the number of constants DR I have.
The problem is that, when I move the formulas across, the formula in Colum E should refer to columns C and D always. At the moment, due to the R1C1 formula, when the block of cells is replicated, the formula is wrong as it will refer to columns H and I.
This is the macro I have crated:
Range("C19").Select
ActiveCell.FormulaR1C1 = "=RC[-2]-R[-1]C[-2]" 'Delta T
Range("D19").Select
ActiveCell.FormulaR1C1 = "=RC[-2]-R[-1]C[-2]" 'Delta V
Range("C19:D19").Select
Selection.AutoFill Destination:=Range("C19:D" & lastrow + 16)
Range("C15:D" & lastrow + 16).Select
' calculation for each DR
Dim myColumn As Integer
Dim Counter As Integer
Counter = 0
myColumn = 1
Do Until (Cells(6, myColumn) = "")
Cells(16, myColumn + 4).Select
Selection.Value = "DeltaV-DR [m3]"
Range(Cells(16, myColumn + 4), Cells(17, myColumn + 4)).Select
Cells(16, myColumn + 5).Select
Selection.Value = "SUM(DeltaV-DR) [m3]"
Range(Cells(16, myColumn + 5), Cells(17, myColumn + 5)).Select
Cells(16, myColumn + 6).Select
Selection.Value = "DeltSurge/DeltT [m3/h]"
Range(Cells(16, myColumn + 6), Cells(17, myColumn + 6)).Select
Cells(16, myColumn + 7).Select
Selection.Value = "Slug Vol. [m3]"
Range(Cells(16, myColumn + 7), Cells(17, myColumn + 7)).Select
Cells(16, myColumn + 8).Select
Selection.Value = "Slug Duration [h]"
Range(Cells(16, myColumn + 8), Cells(17, myColumn + 8)).Select
' DeltaV-Drain Rate = accumulated volume in the separator
Cells(19, myColumn + 4).Select
Selection.FormulaR1C1 = "=RC[-1]-(R6C3*RC[-2])"
Selection.AutoFill Destination:=Range("E19:E" & lastrow + 16).Offset(, 5 * Counter)
'Accumulated volume in the separator (only positive values)
Cells(19, myColumn + 5).Select
Selection.FormulaR1C1 = "=IF((R[-1]C+RC[-1])<0, 0, (R[-1]C+RC[-1]))"
Selection.AutoFill Destination:=Range("F19:F" & lastrow + 16).Offset(, 5 * Counter)
'Delta Surge / Delta time
Cells(19, myColumn + 6).Select
Selection.FormulaR1C1 = "=(RC[-1]-R[-1]C[-1])/RC[-4]"
Selection.AutoFill Destination:=Range("G19:G" & lastrow + 16).Offset(, 5 * Counter)
' Slug Volume
Cells(19, myColumn + 7).Select
Selection.FormulaR1C1 = "=IF(AND(RC[-2]>0, RC[-1]>0), R[-1]C+RC[-4],0)"
Selection.AutoFill Destination:=Range("H19:H" & lastrow + 16).Offset(, 5 * Counter)
'Slug Duration
Cells(19, myColumn + 8).Select
Selection.FormulaR1C1 = "=IF(RC[-1]=0,0, R[-1]C+RC[-6])"
Selection.AutoFill Destination:=Range("I19:I" & lastrow + 16).Offset(, 5 * Counter)
myColumn = myColumn + 5
Counter = Counter + 1
Loop
End Sub
Can anyone please let me know how can I change the formula
FormulaR1C1 = "=RC[-1]-(R6C3*RC[-2])"
so when the loop copies across the set of columns:
a) this formula still refers to columns C and D. And
b) it takes the correct constant that will be now in D6 instead of C6 (for loop 2 in E6 and so).
Thank you very much for your help.
E
After a lot of effort (I am a brand new in VBA) I have managed to create a macro to do the following:
1. Copy time vs. Volume data (with variable lengths). A18:B18
2. Calculate deltaT and deltaVolume. (A19:B19).
3. For a constant DR located in cell C6 in the same w/s the following is calculated for the entire set of data:
a) From E19 down to last row: Ei = Di-($DR$*Ci)
b) From E19 down to last row: Fi = IF((Fi-1+Ei)<0,0, Fi-1+Ei).
c) and d) other formulas.
4. The block of columns from E to I will be replicated according to the number of constants DR I have.
The problem is that, when I move the formulas across, the formula in Colum E should refer to columns C and D always. At the moment, due to the R1C1 formula, when the block of cells is replicated, the formula is wrong as it will refer to columns H and I.
This is the macro I have crated:
Range("C19").Select
ActiveCell.FormulaR1C1 = "=RC[-2]-R[-1]C[-2]" 'Delta T
Range("D19").Select
ActiveCell.FormulaR1C1 = "=RC[-2]-R[-1]C[-2]" 'Delta V
Range("C19:D19").Select
Selection.AutoFill Destination:=Range("C19:D" & lastrow + 16)
Range("C15:D" & lastrow + 16).Select
' calculation for each DR
Dim myColumn As Integer
Dim Counter As Integer
Counter = 0
myColumn = 1
Do Until (Cells(6, myColumn) = "")
Cells(16, myColumn + 4).Select
Selection.Value = "DeltaV-DR [m3]"
Range(Cells(16, myColumn + 4), Cells(17, myColumn + 4)).Select
Cells(16, myColumn + 5).Select
Selection.Value = "SUM(DeltaV-DR) [m3]"
Range(Cells(16, myColumn + 5), Cells(17, myColumn + 5)).Select
Cells(16, myColumn + 6).Select
Selection.Value = "DeltSurge/DeltT [m3/h]"
Range(Cells(16, myColumn + 6), Cells(17, myColumn + 6)).Select
Cells(16, myColumn + 7).Select
Selection.Value = "Slug Vol. [m3]"
Range(Cells(16, myColumn + 7), Cells(17, myColumn + 7)).Select
Cells(16, myColumn + 8).Select
Selection.Value = "Slug Duration [h]"
Range(Cells(16, myColumn + 8), Cells(17, myColumn + 8)).Select
' DeltaV-Drain Rate = accumulated volume in the separator
Cells(19, myColumn + 4).Select
Selection.FormulaR1C1 = "=RC[-1]-(R6C3*RC[-2])"
Selection.AutoFill Destination:=Range("E19:E" & lastrow + 16).Offset(, 5 * Counter)
'Accumulated volume in the separator (only positive values)
Cells(19, myColumn + 5).Select
Selection.FormulaR1C1 = "=IF((R[-1]C+RC[-1])<0, 0, (R[-1]C+RC[-1]))"
Selection.AutoFill Destination:=Range("F19:F" & lastrow + 16).Offset(, 5 * Counter)
'Delta Surge / Delta time
Cells(19, myColumn + 6).Select
Selection.FormulaR1C1 = "=(RC[-1]-R[-1]C[-1])/RC[-4]"
Selection.AutoFill Destination:=Range("G19:G" & lastrow + 16).Offset(, 5 * Counter)
' Slug Volume
Cells(19, myColumn + 7).Select
Selection.FormulaR1C1 = "=IF(AND(RC[-2]>0, RC[-1]>0), R[-1]C+RC[-4],0)"
Selection.AutoFill Destination:=Range("H19:H" & lastrow + 16).Offset(, 5 * Counter)
'Slug Duration
Cells(19, myColumn + 8).Select
Selection.FormulaR1C1 = "=IF(RC[-1]=0,0, R[-1]C+RC[-6])"
Selection.AutoFill Destination:=Range("I19:I" & lastrow + 16).Offset(, 5 * Counter)
myColumn = myColumn + 5
Counter = Counter + 1
Loop
End Sub
Can anyone please let me know how can I change the formula
FormulaR1C1 = "=RC[-1]-(R6C3*RC[-2])"
so when the loop copies across the set of columns:
a) this formula still refers to columns C and D. And
b) it takes the correct constant that will be now in D6 instead of C6 (for loop 2 in E6 and so).
Thank you very much for your help.
E