Not true, you have the workbooks.HansV wrote:Apparently there is no worksheet named Vix in the workbook you're testing the code in.
However there are a number of workbooks open and perhaps the "wrong" workbook is in focus.
Not true, you have the workbooks.HansV wrote:Apparently there is no worksheet named Vix in the workbook you're testing the code in.
I need a bit of time to think. I am getting a bit confused.. let me re read your last few posts !!bknight wrote:What do you guys think?
If your code is in the workbook with the Vix Worksheet in it them use thisbknight wrote:Not true, you have the workbooks.HansV wrote:Apparently there is no worksheet named Vix in the workbook you're testing the code in.
However there are a number of workbooks open and perhaps the "wrong" workbook is in focus.
No it is not.bknight wrote:...Alan's procedure is to find the formulas to "copy" to the next empty rows.
What you are focusing on ( Looking at ) ( Having Active ) is the active Workbook.bknight wrote:.... there are a number of workbooks open and perhaps the "wrong" workbook is in focus.
Code: Select all
Sub CopyCellsFormulas() ' http://www.eileenslounge.com/viewtopic.php?f=27&t=25344&start=60#p197499
Dim dteDateValue As Date
Dim I As Integer, J As Integer, intStartRow As Integer, R As Integer
Dim dbl9Value As Double, dbl21Value As Double
Const defName As String = "DataCol"
Const defNameOEX_High As String = "OEX_High"
Const defNameOEX_Low As String = "OEX_Low"
Const defNameDATE_Range As String = "DATE_Range"
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
'New Data Aquisitions Begins 10/21/2003 Put/Call
'01/02/2004 Row 53
'07/01/2004 Row 177
'01/03/2005 Row 305
'07/01/2005 Row 430
'01/03/2006 Row 557
'07/03/2006 Row 682
'01/02/2009 Row 1312
'01/03/2012 Row 2067
'07/02/2012 Row 2142
'01/02/2013 Row 2267
'07/01/2013 Row 2391
'01/02/2014 Row 2519
'07/01/2015 Row 3148
'07/01/2016 Row 3199
'intStartRow = 3199
'I = intStartRow
Dim ws As Worksheet: Set ws = Worksheets("Vix"): Set ws = ThisWorkbook.Worksheets("Vix")
Set ws = Workbooks("VIXData050824After.xls").Worksheets("Vix")
Worksheets("Vix").Activate
Dim Lr As Long: Let Lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
Dim Sr As Long: Let Sr = (ws.Cells(ws.Rows.Count, "F").End(xlUp).Row) + 1
'Do
'I = I + 1
'Range(Cells(I - 2, 6), Cells(I - 2, 9)).Copy Range(Cells(I - 1, 6), Cells(I - 1, 9))
'Loop Until Cells(I, 2) = ""
Rem Code section to help get Formulas: Put stop in left margin at Set rngVix = .. then run code and Hit Ctrl+G to display Immediate Window
Dim Temp() As Variant: Let Temp() = ws.Range("F" & (Sr - 1) & ":I" & (Sr - 1) & "").Formula
Dim strTemp As String: Let strTemp = ws.Range("F" & (Sr - 1) & ":F" & (Sr - 1) & "").Formula
Let strTemp = ws.Range("F" & (Sr - 1) & "").Formula
Debug.Print strTemp ' =(F3261*$I$3)+(E3262*$I$2)
Debug.Print Temp(1, 1) ' =(F3258*$I$3)+(E3259*$I$2)
Debug.Print Temp(1, 2) ' =G3258*$I$7+E3259*$I$6
Debug.Print Temp(1, 3) ' =F3259/G3259
Debug.Print Temp(1, 4) ' =AVERAGE(E3210:E3259)
Dim rngVix As Range ' variable to hold the entire Range you are currently considering to add formulers to
Set rngVix = Worksheets("Vix").Range("F" & Sr & ":I" & Lr & "")
Dim arrrngVix() As Variant ' We know the size of the Array, but must use Re Dim below as Dim only takes Numbers. We must use Variant as otherwise the final Paste Out to the Worksheet for Formula strings does not always work. http://www.mrexcel.com/forum/excel-questions/887822-formula-link-cell-array-instead-cell-value-based-cell-r-c-co-ordinates-2.html
ReDim arrrngVix(1 To rngVix.Rows.Count, 1 To 4) ' We must use Redim as this allows us to use the Rows Count Property
Dim Cntrw As Long
For Cntrw = 1 To (UBound(arrrngVix(), 1) - 0) ' at every "row" in the Array, (Up to and including the last), we ...
' ... Put in each string Formula
Let arrrngVix(Cntrw, 1) = "=(F" & Sr - 2 + Cntrw & "*$I$3)+(E" & Sr - 1 + Cntrw & "*$I$2)" '
Let arrrngVix(Cntrw, 2) = "=G" & Sr - 2 + Cntrw & "*$I$7+E" & Sr - 1 + Cntrw & "*$I$6" '
Let arrrngVix(Cntrw, 3) = "=F" & Sr - 1 + Cntrw & "/G" & Sr - 1 + Cntrw & "" '
Let arrrngVix(Cntrw, 4) = "=AVERAGE(E" & Sr - 50 + Cntrw & ":E" & Sr - 1 + Cntrw & ")" '
Next Cntrw
Let rngVix.Value = arrrngVix() ' This will paste out the Formula values to the Worksheet
End Sub
OK, as I said I will try to get back to you in a day or so... ( get me some files ( Before and After ) in the meantime so I do not get confused where we are with everything )bknight wrote:OK, you have mail.
For Debugging , Debug.Printing and the such that is probably the best ( or proper way )HansV wrote:1) Change all occurrences of Temp() to Temp
2) After the 3rd Let Temp = ..., refer to Temp instead of to Temp(1, 1).
When you know it has an Array of values in it then to get a single value ( as necessary, for example, for the Debug.Print line ) , then you still need to get at each value likeHansV wrote:….
2) After the 3rd Let Temp = ..., refer to Temp instead of to Temp(1, 1).
Code: Select all
Sub CopyCellsFormulasTest()
Dim dteDateValue As Date
Dim I As Integer, J As Integer, intStartRow As Integer, R As Integer
Dim Lr As Long, Sr As Long, Cntrw As Long
Dim dbl9Value As Double, dbl21Value As Double
Dim Temp() As Variant
Dim rngWs As Range
Dim arrrngWs() As Variant
Dim ws As Worksheet
Const defName As String = "DataCol"
Const defNameOEX_High As String = "OEX_High"
Const defNameOEX_Low As String = "OEX_Low"
Const defNameDATE_Range As String = "DATE_Range"
Set ws = Worksheets("Vix")
Worksheets("Vix").Activate
Let Lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
Let Temp() = Worksheets("Vix").Range("F" & (Sr - 1) & ":I" & (Sr - 1) & "").Formula
Debug.Print Temp(1, 1) ' =(F3258*$I$3)+(E3259*$I$2)
Debug.Print Temp(1, 2) ' =G3258*$I$7+E3259*$I$6
Debug.Print Temp(1, 3) ' =F3259/G3259
Debug.Print Temp(1, 4) ' =AVERAGE(E3210:E3259)-->This debug print on all sections to aid in code development, but will be deleted in the remaining three pieces
Set rngWs = Worksheets("Vix").Range("F" & Sr & ":I" & Lr & "")
ReDim arrrngWs(1 To rngWs.Rows.Count, 1 To 4)
For Cntrw = 1 To (UBound(arrrngWs(), 1) - 0)
Let arrrngWs(Cntrw, 1) = "=SUM(E" & Sr - 4 + Cntrw & ":E" & Sr - 1 + Cntrw & ")/4" '
Let arrrngWs(Cntrw, 2) = "=SUM(E" & Sr - 81 + Cntrw & ":E" & Sr - 1 + Cntrw & ")/81"
Let arrrngWs(Cntrw, 3) = "=F" & Sr - 1 + Cntrw & "/G" & Sr - 1 + Cntrw & ""
Let arrrngWs(Cntrw, 4) = "=AVERAGE(E" & Sr - 50 + Cntrw & ":E" & Sr - 1 + Cntrw & ")"
Next Cntrw
Let rngWs.Value = arrrngWs()
Set ws = Worksheets("PutCall Ratio")
Worksheets("PutCall Ratio").Activate
Let Lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
Let Sr = (ws.Cells(ws.Rows.Count, "F").End(xlUp).Row) + 1
Set rngWs = Worksheets("PutCall Ratio").Range("F" & Sr & ":H" & Lr & "")
ReDim arrrngWs(1 To rngWs.Rows.Count, 1 To 3)
For Cntrw = 1 To (UBound(arrrngWs(), 1) - 0)
Let arrrngWs(Cntrw, 1) = "=(F" & Sr - 2 + Cntrw & "*$I$3)+(E" & Sr - 1 + Cntrw & "*$I$2)" '
Let arrrngWs(Cntrw, 2) = "=SUM(E" & Sr - 81 + Cntrw & ":E" & Sr - 1 + Cntrw & ")/81" '
Let arrrngWs(Cntrw, 3) = "=F" & Sr - 1 + Cntrw & "/G" & Sr - 1 + Cntrw & "" '
Next Cntrw
Let rngWs.Value = arrrngWs()
I = Lr - 23
Do
dbl9Value = 0: dbl21Value = 0
For J = I To (I - 20) Step -1
dbl21Value = Cells(J, 5) + dbl21Value
Next J
Cells(I, 12) = dbl21Value / 21
For J = I To (I - 8) Step -1
dbl9Value = Cells(J, 5) + dbl9Value
Next J
Cells(I, 11) = dbl9Value / 9
Cells(I, 11).NumberFormat = "0.00": Cells(I, 12).NumberFormat = "0.00"
I = I + 1
Loop Until Cells(I, 1) > Now() Or Cells(I, 2) = ""
Set ws = Worksheets("OEX")
Worksheets("OEX").Activate
Let Lr = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Let Sr = (ws.Cells(ws.Rows.Count, "G").End(xlUp).Row) + 1
Rem Code section to help get Formulas: Put stop in left margin at Set rngVix = .. then run code and Hit Ctrl+G to display Immediate Window
Set rngWs = Worksheets("OEX").Range("G" & Sr & ":H" & Lr & "")
ReDim arrrngWs(1 To rngWs.Rows.Count, 1 To 2)
For Cntrw = 1 To (UBound(arrrngWs(), 1) - 0)
Let arrrngWs(Cntrw, 2) = "=SUM(F" & Sr - 161 + Cntrw & ":F" & Sr - 2 + Cntrw & ")/160"
Next Cntrw
Let rngWs.Value = arrrngWs() ' This will paste out the Formula values to the Worksheet
Set ws = Worksheets("Calc")
Worksheets("Calc").Activate
Let Lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
Let Sr = (ws.Cells(ws.Rows.Count, "C").End(xlUp).Row) + 1
Set rngWs = Worksheets("Calc").Range("A" & Sr & ":H" & Lr & "")
ReDim arrrngWs(1 To rngWs.Rows.Count, 1 To 7)
For Cntrw = 1 To (UBound(arrrngWs(), 1) - 0)
Let arrrngWs(Cntrw, 1) = "='PutCall Ratio'" & "!A" & Sr + 130 + Cntrw
Let arrrngWs(Cntrw, 2) = "=((C" & Sr - 1 + Cntrw & "+D" & Sr - 1 + Cntrw & ")/2)*100"
Let arrrngWs(Cntrw, 3) = "='PutCall Ratio'" & "!H" & Sr + 130 + Cntrw
Let arrrngWs(Cntrw, 4) = "=Vix!H" & Sr + 79 + Cntrw
Let arrrngWs(Cntrw, 5) = "=OEX!A" & Sr + 366 + Cntrw
Let arrrngWs(Cntrw, 6) = "=OEX!F" & Sr + 366 + Cntrw
Let arrrngWs(Cntrw, 7) = "=OEX!H" & Sr + 366 + Cntrw
Next Cntrw
Let rngWs.Value = arrrngWs() ' This will paste out the Formula values to the Worksheet
End Sub
Code: Select all
Sub PointedTesties()
Dim Lr As Long
If Lr <> 2 Then MsgBox prompt:="""Lr"" is Not equal to 2, " & vbCrLf & "It should be a ""empty"" Long which is zero, like this " & Lr & ""
If Not (Lr = 2) Then MsgBox prompt:="""Lr"" is Not equal to 2, " & vbCrLf & "It should be a ""empty"" Long which is zero, like this " & Lr & ""
End Sub
Code: Select all
Let Temp() = Worksheets("Vix").Range("F" & (Sr - 1) & ":I" & (Sr - 1) & "").Formula
Code: Select all
Let Temp() = Worksheets("Vix").Range("F" & Sr - 1 & ":I" & Sr - 1 & "").Formula
Code: Select all
Sr = (ws.Cells(ws.Rows.Count, "F").End(xlUp).Row) + 1
OK, I used the ---> to emphasize what comments that were deleted to fit into the size limitationsDoc.AElstein wrote:Hi
Just some quick very minor initial points, - ( I have not had time to look in too much detail fully yet: )
_(i) Avoid using the pointed brackets ( > < ) in ‘comments ( and codes ) as much as possible. You may have to use them in codes – If so, fair enough, - do it.
I just mention it in passing as although I have not had a problem here at Eileen’s Lounge yet, I do find that posting any code generally around the internet can sometimes causes problems when a pointy bracket is mistaken as a “HTML Code Tag thingy”
I use the extra brackets to ensure the calculation meets what math I intend, but I understand your basic theme in usage of brackets.....
_ (ii) Avoid the extra brackets like in (Lr = 2) above. It is a bad habit of mine which in some situations may catch you out as it may do something you did not want.
I do often use extra brackets. Just a personal preference. A bad Habit. I have them. :)
One reason for having a syntax in a code ( or an order of mathematical operations ) is to avoid a periphery of brackets.
This code line_...
...
I like a periphery of brackets and the such. I do not know why. Maybe I am something of a anarchist programmer. I guess VBA is a anarchistic programming language actually. So it suits ? ( VBA ( so experts tell me ) rejects sometimes the Object Orientated Programming language hierarchy, which we are continually told it adheres to )
Ok, I will add it to the code, the after sheet that was linked wasn't run with code, just auto fill of the formulas, but I will test it today....
_(iv) In your code from the last post you forgot your Sr calculation code line for Vix worksheetCode: Select all
Sr = (ws.Cells(ws.Rows.Count, "F").End(xlUp).Row) + 1
I still don't fully understand the scheme behind your coding structure, but it works and that was the main goal in the initial question....
Finally for now: ( Just to clarify once again for anyone else following this Thread):
I am just for convenience taking that formula from the row just before the row we want to start filling in formulas of that type.
I just do that through copying a Debug.Printed formula to get the approximate form of the English version of that formula.
I do not use that formula at all in actually achieving the main requirement of this Thread, which is filling in some formulas over more rows ( Filling the spreadsheet up with formulas down a few more rows than they already are ).
I am not getting the formula to copy / paste into the following rows.
...
The Current code does:…
The basic formula is changed into a dynamic form , where the row number is dynamic.
In an internal VBA code Loop, is built up a large “internal VBA Array” of the entire formulas as Strings ( held in Variant type member Elements of that Array ) . These are then later, ( after the entire internal Array is filled ), pasted out in one go over the Range we want to fit in.
In the demo screenshot above we only have one row to fill in. That is just for demonstration purposes. In the practice the rows to be filled in may be much more. In the very original codes at the start of the Thread, that last formula was copied in order to use in a copy / paste type action: That copied formula was then pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down.
Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down.Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down. Then that just pasted formula was copied and pasted one row down.……. :)
We are looking at other ways to fill in the formulas.
:)
Alan
_..............................bknight wrote:..it works and that was the main goal in the initial question.
I think when you have time if you re read all I have written again, then it will suddenly become obvious. It’s one of these things that once you know is ridiculously obvious, but it just takes a while to suddenly twig.bknight wrote:…I still don't fully understand the scheme behind your coding structure...
Code: Select all
Let arrrngWs(Cntrw, 4) = "=AVERAGE(E" & Sr - 50 + Cntrw & ":E" & Sr - 1 + Cntrw & ")"
Next Cntrw
' Go to a Party at this point. Take arrrngWs with you if you like. It might be a novel ice-breaker ;)
Let rngWs.Value = arrrngWs() ' Fill up the worksheet in one go if you are still sober wenn you get back, by chucking arrrngWs with all the formulas in it at the screen
Yes you are correct cells "OEX!H3551" should be blank. All sheets should have data added for 12/15/2016, without calculations. Remember all cells on "Calc" are formula links to the other sheets, so they should be blank until the macro is executed.Doc.AElstein wrote:Hi
...
_Your Before and After are the same for Worksheet OEX. I expect you may have just not given the correct After there? And/ or you are not using the correct columns for the Lr and Sr calculation? But I may have missed exactly what you are wanting to do there. I think that is up to you to check or sort out. Possibly it is what you want.
_ For the Before and After for Calc –… the After has a complete new row of data compared with the Before.
The code gives some strange results ( as expected ) in that case. This is because the code was written such that Lr is where the range to be added ends, and Sr is where it starts. If you think about it then that idea will not work if the whole row is filled in. But that is up to you to check or sort out.
...
I expect then the OEX and Calc sections you need to check , and / or your before and After for those worksheets. But I will not look further there. If I take a look possibly, out of interest at a different solution idea, then I will just do the Vix Worksheet Snippet again as an example…. _..
_..But anyway I think it is probably solved enough now for you..bknight wrote:..it works and that was the main goal in the initial question.
I still haven't run the macro, doing other normal tasks on Saturday morning, but soon._..............................
I think when you have time if you re read all I have written again, then it will suddenly become obvious. It’s one of these things that once you know is ridiculously obvious, but it just takes a while to suddenly twig.bknight wrote:…I still don't fully understand the scheme behind your coding structure...
...
This layman’s explanation will do:
Each Loop in the code puts very quickly all the formulas that you want for a range to be filled in , in your hand.
Think of your hand as that internal bit of memory space.
If you like, think of your hand as looking a bit like a spreadsheet but a small spreadsheet with only just enough “rows” and “columns” for all the formulas you want, so it is only just big enough to hold all those formulas in it.
The Loop makes and puts those formulas into that “hand”. It puts them in a “row” and “column” order that is just the same as the actual row and column order that you finally want in your actual spreadsheet.
The Loop does nothing at all to the actual worksheet. - Your hand with all the final formulas in it is totally independent to the Worksheet.
Now, Say you stopped the code just after the Loop and just before this line
Let rngWs.Value = arrrngWs()
You then left your computer at that point and left the worksheet on your screen where it was and you still had all the formulas in your hand last night when you went to the party. You still had the formulas in your hand when you got back.
When you got back , the Worksheet was still showing on your screen. You were just about sober enough to hit the correc keyboard keys, so then you let the code go further such that this line_...
Let rngWs.Value = arrrngWs()
_....was done. That line is doing like you chucking all those formulas in your hand at your screen such that they all land at once in the correct cells.
Looking at the hand idea for your very original code. That was like you sort of copying manually each row of formulas , adjusting them for the next row offset, then chucking it back at the next row down. Then you sort of copying manually that new row, changing that for the next offset row and chucking it back at the screen. Then you sort of copying manually that new row, changing that for the next offset row and chucking it back at the screen. Then you sort of copying manually that new row, changing that for the next offset row and chucking it back at the screen. Then you sort of copying manually that new row, changing that for the next offset row and chucking it back at the screen. Then you sort of copying manually that new row, changing that for the next offset row and chucking it back at the screen.
My last attempt, just a bit of fun..: :xsmile:
My Code idea:
Go to the corner shop. Fill up a box of sweets, ( marking each one with a kids name on it ) for a kids party at your home. Go home chuck all the sweets out and all the kids catch them willingly in one go.
Original Code:
Go to a big supermarket somewhere in a big shopping center. Buy a sweet. Put a kids name on it . Go home to a kids party and give the sweet to a kid. Go to a big supermarket somewhere in a big shopping center. . Buy a sweet. Put a kids name on it. Go home to a kids party and give the sweet to a kid. Go to a big supermarket somewhere in a big shopping center. . Buy a sweet. Put a kids name on it . Go home to a kids party and give the sweet to a kid. Go to a big supermarket somewhere in a big shopping center. . Buy a sweet. Put a kids name on it. Go home to a kids party and give the sweet to a kid. Go to a big supermarket somewhere in a big shopping center. . Buy a sweet. Put a kids name on it. Go home to a kids party and give the sweet to a kid.
You will kick yourself when you finally realize how simple that is to understand. Just maybe you need to get clued up a bit on VBA Arrays.
Code: Select all
Sr = (ws.Cells(ws.Rows.Count, "F").End(xlUp).Row) + 1
Code: Select all
Sub NumToDate()
v = Cells(Sr, 1)
' convert it to a string
s = CStr(l)
' can now use string functions to parse it (assumes yyyymmdd)
d = DateSerial(CInt(Left(s, 4)), CInt(Mid(s, 5, 2)), CInt(Right(s, 2)))
' write it back to the sheet
Cells(Sr, 1) = d
End Sub