Not sure if my terminology is correct but the problem is that I have a vba module that I am using to analyze data(count unique values). The formula is fine and works when after I correct excels change.
VBA line to insert formula is
ws_MachineNumber.Range("D2").FormulaLocal = _
"=SUM(IF('Roh Daten_IA'!$B$2:$B$" & lastR_RDi & "= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$" & lastR_RDi & ",MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$" & lastR_RDi & ",'Roh Daten_IA'!$G$2:$G$" & lastR_RDi & ")),0))"
Which I am expecting to show up in the cell as
=SUM(IF('Roh Daten_IA'!$B$2:$B$296= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$296,MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$296,'Roh Daten_IA'!$G$2:$G$296)),0))
However for a reason unbeknownst to me excel keeps making it
=SUM(IF(#'Roh Daten_IA'!$B$2:$B$296= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$296,MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$296,#'Roh Daten_IA'!$G$2:$G$296)),0))
not sure why it keeps referencing it that way. Prior to this I didn't have the abs ranges and figured that was the problem but the # symbol is somehow causing the formula to operate incorrectly and it gives a result of 0 for every occurrence regardless of what the correct evaluation should give.
Thank you
Related
x = Application.GetOpenFilename(Title:="Please Select the required File")
lNewBracketLocation = InStrRev(x, Application.PathSeparator)
x = Left$(x, lNewBracketLocation) & "[" & Right$(x, Len(x) - lNewBracketLocation)
Sheets("T0").Range("AC2").FormulaR1C1 = "=IFERROR(VLOOKUP(LEFT(RC[-28],8),'" & x & "]SheetName'!C1:C3,3,0,)""Not Available"")"
I am asking the user to select the file which the user needs to perform the mapping task and for that I am using the above code, but it isn't working.
The error comes from a syntax error in your formula. So Excel (not VBA) complains about that error by raising an "application-defined" error to VBA, and VBA shows it to you. You simply cannot enter an invalid formula into a cell. If you try manually from Excel, you will get something like "There is an error with that formula".
Now, while it's hard to write a complicated formula in Excel, it's even harder to get it right from VBA. Following things can help to find the problem:
(1) Don't write the formula direct into the excel range. Use an intermediate variable:
Dim formula as string
formula = "=IFERROR(VLOOKUP(LEFT(RC[-28],8),'" & x & "]SheetName'!C1:C3,3,0,)""Not Available"")"
Debug.Print formula
Sheets("T0").Range("AC2").FormulaR1C1 = formula
(2) Write the formula into Excel manually, keep the cell active, switch to the VBA environment and type in the immediate window:
? activecell.FormulaR1C1
Compare the result with the content of the formula-variable. If they are not identically, you probably have found your problem.
In your case, you have a misaligned comma in the formula. Look at C1:C3,3,0,) - it should have the closing parenthesis before the comma: C1:C3,3,0),
"=IFERROR(VLOOKUP(LEFT(RC[-28],8),'" & x & "]SheetName'!C1:C3,3,0),""Not Available"")"
I cannot guarantee that this the only error in the formula - but I hope it can help to narrow down such problems.
I have a database dump of data I need to regularly format. I'm trying to make a macro for this and am about 90% there.... The data dump is in a CSV elsewhere. My macro is in a workbook (Toolwb) and contains a couple macros, a look up table sheet(LookUpSheet), and a sheet with some formulas on it(FormulaSheet).
I'm coming across an issue with my formula that I am copying from my FormulaSheet that uses VLOOKUP on my LookUpSheet. Since I am running the code on a different workbook, I need to copy the formula over with the reference to Toolwb in it... And from what I can tell, Toolwb does not like formulas that specify Toolwk in the formula. It just edits them out and then breaks when the vba copies it over to the data dump. So to get around this I changed the cell to be Text type instead of general. Perfect! Copies over great.... But... I can't seem to get the formula to "refresh" once its in data dump cell. I have to open the cell and hit enter for it to refresh and evaluate the data.
I've tried changing the cell General after pasting it and then set it to auto calculate. I've also tried the formula refresh buttons. What am I missing?
I would just as happily paste this from vba instead of a cell, but I'm having a hard time getting the quotes in syntax.
=RIGHT(VLOOKUP(VALUE(CONCATENATE(B2,".",IF(LEN(C2)<2,CONCATENATE("0",C2),C2))),'[Toolwb]LookUpSheet'!$C$2:$D$4419,2,TRUE),LEN(VLOOKUP(VALUE(CONCATENATE(B2,".",IF(LEN(C2)<2,CONCATENATE("0",C2),C2))),'[Toolwb.xlsm]LookUpSheet'!$C$2:$D$4419,2,TRUE))-6)
"." can be replaced with CHAR(46).
CONCATENATE(0,C2) is the same as CONCATENATE("0",C2). The result will still be a string with a leading zero.
.Address(External:=True) will produce a fully qualified workbook/worksheet/range reference as an address string including any necessary punctuation.
I'm a little unclear on why $C$2:$D$4419 needs to be specified instead of $C:$D.
IF(LEN(C2)<2,CONCATENATE("0",C2),C2) is simpler as RIGHT("00"&C2, 2)
Code:
dim Toolwb as workbook, Toolws as worksheet, Toolrng as range
set Toolwb = workbooks("some workbook")
set Toolws = Toolwb.worksheets("LookUpSheet")
set Toolrng = Toolws.Range("C2:D4419")
Range("A1").Formula = _
"=RIGHT(VLOOKUP(VALUE(CONCATENATE(B2, CHAR(46), RIGHT(""00""&C2, 2))), " & Toolrng.Address(External:=True) & ", 2, TRUE), LEN(VLOOKUP(VALUE(CONCATENATE(B2, CHAR(46), RIGHT(""00""&C2, 2))), " & Toolrng.Address(External:=True) & ", 2, TRUE))-6)"
I haven't tested that but it looks right.
You might also try this adaptation using REPLACE that removes the first 6 characters and effectively cuts your formula in half.
Range("A1").Formula = _
"=REPLACE(VLOOKUP(VALUE(CONCATENATE(B2, CHAR(46), RIGHT(""00""&C2, 2))), " & Toolrng.Address(External:=True) & ", 2, TRUE), 1, 6, """")"
Ricardo A solved this for me in a comment.
"Have you tried in VBA newWorkbook.Sheets("Sheet1").Range("A2").Formula = formulaWorkbook.Sheets("FormulaSheet").Formula"
Worked perfectly!
I'm trying to use Application.Evaluate method for evaluating a Sumif formula in which the criteria is ">="
Following is the line of code I'm trying to get an evaluation of
So I have Order Numbers 1 to 15 in Cell E1 to E15, and their Respective Amounts in Cell F1 to F15.
In J1 the user inputs his Order Number. Amount over and above that order number will be totaled and displayed using Sumif Function.
Now I want to find this answer using Application.Evaluate
MsgBox( Application.Evaluate("=SUMIF(E1:E15, ">=" & J1, F1:F15)"))
I get greeted with True or False Message Box.
Now I am guessing the inverted commas in Sumif Function ie. ">=" is causing this problem. Hence to fix this, I amended the function to
MsgBox( Application.Evaluate("=SUMIF(E1:E15, """">="""" & J1, F1:F15)"))
However now it returns 0, although there are values in it.
I would really appreciate if someone could help me understand a way around it
*Note: Please do not suggest to use any other function. I want to know it purely from Application.Evaluate perspective, as my further line of code depends on it. *
Thanks.
All quotes inside the string must be doubled:
">="
Should be
"">=""
Also:
Application.Evaluate
will work on the active sheet, thus if the wrong sheet is active the sumifs will be done on the wrong sheet since no sheet names are provided in the formula itself.
Use
Worksheets("Sheet1").Evaluate
Changing "Sheet1" to your sheet.
So in total:
MsgBox Worksheets("Sheet1").Evaluate("=SUMIF(E1:E15, "">="" & J1, F1:F15)")
I am trying to create a reference to a separate sheet using INDIRECT. I also want to check this for errors, so I preface the thing by using ISERROR.
H1 is a date value, formatted as "nn m.d". In this case, 42574 returns Sat 7.23
'Sat 7.23'.D2 equals 100
Let's say there's a tab named "Sat 7.23", and I would like to access cell D2. Using INDIRECT and converting the formatted date to text, I create this formula:
=INDIRECT("'" & TEXT(H1, "nn m.d") & "'.D2")
In other words, INDIRECT tells me to make the following reference:
='Sat 7.23'.D2
When the tab exists, this functions perfectly (it returns 100). But... what if the tab doesn't exist? INDIRECT returns #REF!, which is to be expected. So, I throw an ISERROR in front of it:
=ISERROR(INDIRECT("'" & TEXT(H1, "nn m.d") & "'.D2")
This returns nothing (or I guess FALSE), even though INDIRECT is generating a #REF! error and therefore should be TRUE. Should it not?
To go further:
=IF(ISERROR(INDIRECT("'" & TEXT(H1, "nn m.d") & "'.D2")),0,INDIRECT("'" & TEXT(H1, "nn m.d") & "'.D2"))
In this case, ISERROR is always true, so this IF always goes to it's "else" statement. Since the reference is invalid, the whole IF statement returns #REF!
I'm not sure what regional language uses nn to represent Sun - Sat in a format mask but ddd is used in an EN-US system and there is an exclamation mark between the worksheet and the cell address.
=IFERROR(INDIRECT("'"&TEXT(H1,"ddd m.d")&"'!D2"), 0)
This will return zero when copied to one cell above (#REF! on H0 as a cell address).
Some amends as proposed below should help you see through the issues:
Change the custom format of cell H1 which has the date value to "ddd m.dd" without the quotes. I see you have used "nn m.d" and it did not work for me.
Now, in cell I1 (adjacent to H1), let's try and pull the value of D2 from the sheet named 'Sat 7.23' by using the formula below:
=IF(ISERROR(INDIRECT("'" & TEXT(H1, "ddd m.d") & "'!D2")),"sheet doesn't exist, put your appropriate text message here",INDIRECT("'" & TEXT(H1, "ddd m.d") & "'!D2"))
To do a negative check, I recommend putting another date 'Sun 7.24" in H2 and use the same formula in I2 with a reference to the date in H2:
=IF(ISERROR(INDIRECT("'" & TEXT(H2, "ddd m.d") & "'!D2")),"sheet doesn't exist, put your appropriate text message here",INDIRECT("'" & TEXT(H2, "ddd m.d") & "'!D2"))
This is what it will look like in a completed worksheet:
I fixed similar problem by generating error
=IFERROR(QUERY(INDIRECT("'"&I2&"'!B3:E", TRUE), "select E where B = '"&I5&"'),"-")
Gives #REF!
=IFERROR(QUERY(INDIRECT("'"&I2&"'!B3:E", TRUE), "select E where B = '"&I5&"')*1,"-")
Works because multiplication of REF! with 1 gives an error
If you expect a number go with *1
If you expect string go with &''
I'm having problems inserting the value of a cell into a range. Right now I have:
=M3/SUM(M$3:M$13)
What I need is to be able to insert the value of another cell into that range, like:
=M3/SUM(M$A1:M$B1)
I have spent some time with the indirect function but I can't seem to get the syntax right. Does anyone know the correct syntax? Is indirect even the best/easiest/cleanest way to achieve my goal?
This will do what you want:
=M3/SUM(INDIRECT("M" & $A$1 & ":M" & $B$1))
Not sure if you want A1 and B1 absoluted so I did it anyway.