VBA Insert formula with offset in cell and loop - vba

I've got hourly data. I need to have a cell that computes say the R2 for days 1 and 7 between two different variables (say column A and B) and then loop this so next time it computes the R2 for days 2-8 and then for days 3-9 etc etc.
I must say that the cell that has the R2 formula is used in Solver to solve some code/formulas for max R2, so the cell that contains the R2 needs to be a formula and not a value.. (I have been able to offset this as a value but not as a formula). Part of the current code is the following:
i = 0
ActiveSheet.Cells(24,13).Value = Application.WorksheetFunction.RSq(RangeA.offset(i,0), RangeB.offset(i,0)
i = i + 24
Many many thanks in advance for any helpful tips
Cheers

Assuming you have named ranges RangeA and RangeB, you could use:
ActiveSheet.Cells(24,13).Formula = "=RSQ(RangeA,RangeB)"
Or, you can use relative references in R1C1 notation like so:
ActiveSheet.Cells(24,13).Formula = "=RSQ(RC[-2],RC[-1])"
or, for multi-celled ranges:
ActiveSheet.Cells(24,13).Formula = "=RSQ(RC[-2]:R[2]C[-2],RC[-1]:R[2]C[-1])"

Related

Dynamically select a column using month and year, then sumif the values based on criteria in another column

Here is an image of what I'm trying to achieve:
I am not well-versed in creating my own VBA script, but can understand code written by someone else if given time to dissect it. If this objective can be resolved without VBA, that would be my preference. I've explored using INDEX and MATCH but have gotten nowhere. Any suggestions?
I'm using Excel 2013.
I would "cheat"
I have my months list was in AI1:AI12, and year list in AJ1:AJ2 (for the dropdowns in A1 and B1 respectively). Move them elsewhere for more years.
Then in A2:
=MATCH(A1,AI1:AI12,0)
In B2:
=IF(MATCH(B1,AJ1:AJ2,0)=1,0,12 *( MATCH(B1,AJ1:AJ2,0)-1))
In D1O drag across to AA10:
=SUMIF($C$3:$C$8,"Plan",D3:D8)
Note: You could remove hardcoded "Plan" and reference a cell instead.
Result in C1 with
=OFFSET(D10,,B2+A2-1,1,1)
You are basically using a formula to determine how far to offset from month 1, year1 to get the current month and year conditional sum in row 10.
Example of finding the correct column, as described in my comment (untested code):
arr = array("Jan","Feb") 'you can do the rest
x = 2018 'first year in question, to make the math easier
s = 4 'column your first month is in
y = cells(1,2).value - x
z = application.match(left(cells(1,1).value,3),arr,0)
'all that gets you to:
c = s+(y*12)+z
You can then use the final column number, c, to do your Application.Sumifs() where you specify actual/plan as one of your criteria.

replacing .End(xlUp) with a set value

I am using some code I found on this site to transpose a section of a table
Copy Partial Table and Insert N-times, then Transpose second part of table
however I have found that it is not transposing the data beyond the last filled cell in the range being transposed, resulting in mis-alignments of the data in the resulting table. I believe it is due to the .End(xlUp) reference in the last line of the code.
tws.Cells(tws.Rows.Count, dataClmStrt + 2).End(xlUp).Offset(1).Resize(38, 1).Value = _
Application.Transpose(rng.Offset(, dataClmStrt).Resize(, 38))
Is this the cause of the issue and if so can this term be replaced with a set value?
EDIT---------------------------------------------------
To add some more information I have a table with 4 reference columns and 38 data column
Columns A-D contain identifiers to make each line item unique, columns 1-38 contain data in accordance to the heading category of the column.
A B C D | 1 2 3 ... 38
Not all of the 38 category columns contain data, some are empty cells
I am using the code from the reference link verbatim, except have changed to 12s to 38s to reflect the number of columns and expanded the main Arr to 38.
The code will copy the data down the rows correctly, and add the main Arr IDs correctly, but doesn't transpose the 38 columns of data correctly.
Once it reaches a cell with data, it will add this data to the tws sheet, the return to data column 1 and begin again, but in the next row. (i.e. if it finds data in column 12 in row 1 of the original table, it will add that data to row 12 of the new table, but the start adding data from column 1 row 2 of the original table to row 13 of the new table)
I have found by adding a checksum column (column 39) it will transpose correctly. Once the code has run, I use two other subs to remove the blanks and the check sums.
I would like to be able to run the code without needing the check sum column, or if possible eliminate the blanks automatically without additional subs.
Hope this is clearer
#Scott Craner's code is fine but it takes times to get familiar with the logic how Excel manages ranges. I suggest an approach to split Scott's highly advanced expression into smaller pieces that make a single operation, and analyze what happens:
Dim r1 As Range, r2 As Range, r3 As Range, r4 As Range
Dim r7 As Range, r8 As Range
Set r1 = tws.Cells(tws.Rows.Count, dataClmStrt + 2)
Debug.Print r1.Row, r1.Rows.Count, r1.Column, r1.Columns.Count
Set r2 = r1.End(xlUp)
Debug.Print r2.Row, r2.Rows.Count, r2.Column, r2.Columns.Count
Set r3 = r2.Offset(1)
Debug.Print r3.Row, r3.Rows.Count, r3.Column, r3.Columns.Count
Set r4 = r3.Resize(38, 1)
Debug.Print r4.Row, r4.Rows.Count, r4.Column, r4.Columns.Count
Set r7 = rng.Offset(, dataClmStrt)
Debug.Print r7.Row, r7.Rows.Count, r7.Column, r7.Columns.Count
Set r8 = r7.Resize(, 38)
Debug.Print r8.Row, r8.Rows.Count, r8.Column, r8.Columns.Count
r4.Value = Application.Transpose(r8)

Return values from other workbook

Have a question about formula which will resolve my issue.
In my main workbook I need to compare data from two sources.
One of the columns must retrieve data(amounts) from other workbook.
I want formula which will search for all amounts in column G and will skip all blank cells. Tried to use VLOOKUP, INDEX and SMALL functions but no effect.
Each day amounts are different and I need to match them in main file and find exeptions.
Any ideas?
How about an array formula such as the following?
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-1)-ROW($G$2:$G$20)+1)
The formula would have to be placed into cell I2 as an array formula (which must be entered pressing Strg + Shift + Enter). Then you can drag down the formula to get all the other values.
It doesn't have to be in column I but it has to be in row 2 because this formula get's the n-th Number from the list which is not = 0. The n-th place is (in this formula) row()-1. So for row 2 it will be 2-1=1 and thus the 1st number. By dragging down the formula you get the 2nd, 3rd, etc. number. If you start with the formula in cell I5 instead then it would have to be adjusted to be as follows:
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-4)-ROW($G$2:$G$20)+1)
You could loop through the column and store each value >0 in an array and then compare or you loop through the column and compare directly...
something like:
Dim i as Integer = 0
Foreach value in Maintable
Do
If otherworkbook.cells(i,7) = value Then '7 for G
do your stuff
End If
i = i + 1
While i < otherworkbook.rows.count
Next
I think that could be the right approach

Fetch cell value with increment in row number every day

I have an excel sheet which contain dates in one column (Say column A) and some values corresponding to each date in another column (Say column E). I want to fetch the value from the cell at the intersection of today's date (in column A) and it's corresponding value (in column E).
The value fetched should be assigned to a different cell (say R1). The value should automatically update in R1 since we need to fetch based on today's date.
Please provide me a formula for cell R1.
Example:
-----A----------B---------C--------D--------E
5/8/2015-------------------------------------3
6/8/2015-------------------------------------3
7/8/2015-------------------------------------6
8/8/2015-------------------------------------10
9/8/2015-------------------------------------3
10/8/2015------------------------------------12
11/8/2015------------------------------------3
If today is 10/08/2015, then cell R1 should be filled with the value 12.
Tomorrow R1 should be filled with the value 3 automatically.
Thank you.
You should try this formula in your cell R1:
=VLOOKUP(NOW(), A1:E11, 5, TRUE)
One thing is that if needed, you should modify range A1:E11 as you like.
ADDED
If your R1 cell is in other sheet, use this formula:
=VLOOKUP(NOW(), sheetname!A5:E11, 5, TRUE)
Here, also has one point that you should modify sheetname to your data sheet name.
I'm assuming you want to solve this with a formula, so try this formula in cell R1
=INDEX(D:D;MATCH(TODAY();A:A;0))

Absolute reference with R1C1 using a variable to dictat row number (VBA)

I have a code that creates a number of separate lists under each other. For each of these lists I'm running a for loop to assign an equation to each row at the end column. This formula should multiply the neighbour cell (a relative reference) with cell at the top of that particular list (an absolute referance). The problem is that the lists are of arbitrary length and generated earlier in the code, so I can't assign the absolute referance beforhand.
I was thinking on saving the row number (row 1 = 1, row 2 = 2 etc) in a variable and then use the variable in the R1C1 notation (= "R(variable)C5*RC[-1]), but I cant seem to get this to work... The variable will be the same throughout the for loop given in the example below, but will change next time the same for loop is entered.
Is this even possible?
(I know the parantheses in the R1C1 are not the proper notation, but this is to show where I want my variable)
...
variable = 3
For i = 1 to count
last = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Cells(siste, "E").FormulaR1C1 = "=R(variable)C5*RC[-1]"
Next
Just one small change:
Cells(siste, "E").FormulaR1C1 = "=R" & variable & "C5*RC[-1]"