I'm trying to lookup an ID from column I, in column A.
Dim x As Long
lr = Worksheets("Risk Explorer greeks").Cells(Rows.Count, "I").End(xlUp).Row
Range("J2:J2" & lr).FormulaR1C1 = "=VLOOKUP(RC[-1], R1C1:R50000C1, 1, False)"
I have ~40,000 values in column J, however when I run this code it populates down to cell 237,000.
How can I look up column J where it has a value, and not lookup loads of blank cells?
Alternatively is there a faster way to do this lookup rather than the above formula?
You're appending the number 37000 to the string "J2:J2", which gives you "J2:J237000".
Replace Range("J2:J2" & lr) with Range("J2:J" & lr). You should be good to go.
Related
I am trying to rank values using column J and tie breaker in column K with the result populating in column N. Column J & K are values.
Somehow it only generates one value wherever my cell is pointed at, which means if I run the vba codes at cell C19, it will just populate value 1 in C19, not from N6 where I want the results to be.
Here are my codes,
Sub test()
Dim LR1 As Long
LR1 = Range("J" & Rows.Count).End(xlUp).Row
With Range("N6:N" & LR1)
ActiveCell.FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
End With
End Sub
I am not sure what went wrong. I tried to do it manually using the excel formula and its working fine but not my vba codes.
ActiveCell is your issue.
Change
ActiveCell.FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
To .FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
Remove that and it should do what you want.
You will want to fully qualify your Range references that way they aren't depending on the ActiveSheet. This will provide you with consistent behavior and results.
There are 2 things to note here. The first: I am using a For loop to step through each row. The second: I don't know which is the last column with data in each row and hence I am trying to use the Range.End function to find it. To Select the last column with data in row x, the code that works is:
Cells(x, Range("C" & x).End(xlToRight).Column).Select
As you can see from the above, I am starting from column C, row X and looking for the last cell with data in that row. I don't think it matters which column I start from as Column A, B, C, D and E will definitely have data.
Now I try to select a range from Cells(x, 3) (or column C row X) to the above last cell with data in row x. The following code doesn't work:
Range("Cells(x,3):Cells(x, Range("C" & x).End(xlToRight).Column)").Select
I don't know if I am allowed to use the range function like that. The error is a compile error stating "expected: List separator or )" and it highlights the C in Range("C" & x) I looked carefully but don't see anything missing.
I highly appreciate any assistance.
Your method using End(xlToRight) will work only if your range is continous.
If you have blank cells in the middle of the range (let's say columns M and N are empty) you will not get the cells with data in Columns "O:X".
To Select the last column with data in row x, use:
Cells(x, Cells(x, Columns.Count).End(xlToLeft).Column).Select
To Select the entire Range use:
Range(Cells(x, 3), Cells(x, Cells(x, Columns.Count).End(xlToLeft).Column)).Select
However, there is seldom a need to Select, if you "must" then it's better to first define a Range, like this:
Dim Rng As Range
Set Rng = Range(Cells(x, 3), Cells(x, Cells(x, Columns.Count).End(xlToLeft).Column))
And later on, if you need to Select it use:
Rng.Select
Use it like this:
Range(Cells(x,3), Cells(x, Range("C" & x).End(xlToRight).Column)).Select
I'm trying to sum a variable range of data starting at row 3. sortedRow is the row # of the end of the data. lColumn is the last column used as a number.
I'm trying to sum the data in row 1, column H - lColumn.
I need to have the formula calculate the sum of column G, then iterating on referenced column by the current iteration (which will be columns h through x)
I've taken Scott Craner's advice and edited my code as
Dim lColumn As Long
lColumn = ActiveSheet.Cells(3, Columns.Count).End(xlToLeft).Column
sortedRow = Cells(Rows.Count, "D").End(xlUp).Row
Dim i As Integer 'iterated column sum cell
For i = 8 To lColumn
Cells(1, i).Value =Application.WorksheetFunction.SumIfs(Range(Cells(3,7),
Cells(sortedRow, 7)), Range(Cells(3, i), Cells(sortedRow, i)), 1)
Next i
The cells are not being updated with their values though.
It feels like I'm missing something really obvious with actually applying the number to the cell. I thought the above would do it.
added lColumn declaration because of issue with the formula being applied
added sortedRow declaration.
basically this. But with variable column lengths and number of columns.
It didn't matter for Column H that there was "#VALUE" in the volume column. So I assume it doesn't matter for the other columns.
Any help would be appreciated!
You have problems here.
("$G3:$G" & sortedRow, i & "3:" & i & sortedRow, 1)
("$G3:$G" & sortedRow & "," & i & "3:" & i & sortedRow, 1)
But you still have issues in there. If i=4 and sortedRow=12 let's say, this is what you would have.
(G3:G12,43:412, 1)
So you need to fix your Criteria_range1. I'm not clear from your question what you want the criteria to be.
Here is info on the arguments. https://msdn.microsoft.com/en-us/library/office/ff193011.aspx
The issue was resolved after fixing #VALUE cells in the sumifs columns I was trying to add. I made a conditional statement that set all errors to equal zero. This allowed the loop to function.
I'm using INDEX and MATCH functions to pull data which is concatenated string of G2 and H2 from column D (sorry I don't have enough points to attach pic). Column D has INDEX(column A and column B) and columns A and B have values till 12th row. MATCH is working fine giving me the position as 6 on the worksheet. But when I use this in VBA code as shown below,INDEX is working in the VBA code (can be seen through MsgBox) but MATCH function which would allot value to the variable 'check' isn't working. I have been breaking my head for really long. Need help from experts here. Somebody please tell me where am I going wrong?
Sub testindex()
Dim check As Long
Set sh = Sheets("Sheet1")
For j = 1 To 11
'Index value is correctly shown
MsgBox "Index Value=" & Application.WorksheetFunction.Index(sh.Range("A2:B12"), j, 1) & Application.WorksheetFunction.Index(sh.Range("A2:B12"), j, 2)
'Cells(7, 4)=ISA737775 same as G2&H2
MsgBox "Cells(7,4)=" & Cells(7, 4)
check = Application.WorksheetFunction.Match(Cells(7, 4), Application.WorksheetFunction.Index(sh.Range("A2:B12"), j, 1) & Application.WorksheetFunction.Index(sh.Range("A2:B12"), j, 2), 0)
Next j
End Sub
Thanks
Match expects the second paramater to be in the form of a range. When you call match through VBA that range actually needs to be a range object, not just some string like "A1:A12" or whatever it is that your concatenated Index formulas output.
At any rate, you are iterating already, so why not just call those values directly instead of pulling their values through Index?
check = Application.WorksheetFunction.Match(Cells(7, 4), sh.Range("A" & 2 + j).value & sh.Range("B" & 2 + j), 0)
Which is writing the same exact thing but without having to use a taxing INDEX function in VBA to do it. Note that this still won't work because the second parameter of match is still just a string which is a concatenated value from Column A and Column B. You could convert to a range by sticking them in the range object with:
check = Application.WorksheetFunction.Match(Cells(7, 4), sh.Range(sh.Range("A" & 2 + j).value & sh.Range("B" & 2 + j)), 0)
I'm assuming that the values in A and B are actual cell names that when concatenated will make a range. Like when j=1 then the it would be like check=Match(Cells(7,4), sh.Range("G2:H50"), 0) or something...
I'm trying to calculate the sum of my columns (column I). From 1 to the last record in I it has. When I record a macro I get this as output, but this is gibberish to me.
ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-4]C)"
I found another topic and there they said
LastRow = .Range("I" & .rows.Count).End(xlUp).row
Range("I"&LastRow) = "SUM(I1:I...)"
Except in my case, I can't figure how to enter the lastrow of I in it.
All help is welcome :)
There are two ways of referencing a cell - 'R1C1' and 'A1'. The former works like co-ordinates, with a relative number of rows (R) and cells (C).
The other reference style refers to the cell name on the sheet - B6, F67 etc.
Let's say you want to put your Sum() in cell B1 and LastRow has a value of 6:
ActiveSheet.Range("B1") = "=Sum(I1:I" & LastRow & ")"
Would insert the following function in cell B1:
=SUM(I1:I6)