VBA - Place function in cell. Application-defined object-defined error - excel

Noob VBA user here, please excuse any lapses in correct terminology.
I have a macro that combines a number of spreadsheets, performing a number of calculations etc along the way.
As part of this I wish to place a function in a cell, that would update as data in the spreadsheet changes. (ie, the calculation needs to be performed within the worksheet, rather than in the macro code.)
The function is to check if a particular cell is blank, if so adding it to the sum function, if not it will just add zero to the sum function.
I used this line in the macro to accomplish this:
Range("C2:C" & 25).Value = "=SUM(IF(ISBLANK($F$1),E2,"")+(IF(ISBLANK($H$1),G2,"")))"
Note.. this works fine when entered directly into a cell as:
=SUM(IF(ISBLANK($F$1),E2,"")+(IF(ISBLANK($H$1),G2,"")))
However I get a application-defined/object-defined error when trying to run it. Any ideas on how I can make this work? Or is there another route entirely to get a function in a cell using VBA?
Cheers.

Related

vba macro excel - formula question after running a macro

i have a simple question but i can't find an answer in google.
i have the following simple code:
cells(1,1)= cells(1,2) + cells(1,3)
i want to be able to show the user the result in cells(1,1) but that he can also see how i got it (if he stands on the cell he should see that i did =B1+C1).
how can i do it?
moreover, i want him to be able to change the numbers in B1,C1 and get a new result in A1 like regular he could do in regular excel function
how can i do it?
thanks!
I think you may want to just add a formula to the cell and not put in a value.
Check this out.
VBA To Add Formula To Cell
Not exactly what you are asking but it shows you how to add a formula in your vba code.

Frustrating Excel-VBA Calculation Puzzle - function always returns = #N/A

I've got some formulas that, when an input value on a cell on another sheet in the same workbook is changed, display either #NA or #VALUE! until I open that sheet and manually trigger calculation, even though everything is set to Automatic Calculation and the VBA custom functions are set to Application.Volitile. When I run the Evaluate Formula routine in Excel on one typical cell and Evaluate down to nearly the last step I get an evaluation that reads as follows;
= IF(FALSE,#N/A,1076)
and expect the next step when I hit Evaluate to be
=1076
but instead it returns
= #N/A
and the only thing to do is manually calculate using Calculate Now or a key combination. Then all the cells return correct answers and there are no error messages.
What's going on??????

VLookup function in Excel VBA

I'm very new to VBA, yet I still find myself writing a UDF for excel and part of that process requires that a value be pulled from another sheet that is open in the same workbook. I don't know what I'm doing obviously, but I'm hoping someone else can point me in the right direction.
A sheet called "VIAL TYPES" has the data that I need in the 7th column.
For the purpose of this question, I would just like to print the value of VLookup in cell G9.
To run the function, I would manually write in the VialType in an adjacent cell. I expect the Vlookup function to recognize the type entered into cell (=VTEST(cellIwrotethevialtypein)), find it's match in the "VIAL TYPES" sheet and then print the value in the 7th column back in the active sheet. Basically, I want it to do exactly what VLOOKUP does, I just need this to work in vba so that I can execute additional things with the value.
I get #Value! in the cell now.
Function VTEST(VialType As String)
ActiveWorkbook.ActiveSheet.Range("G9").Value = Application.WorksheetFunction.VLookup(VialType, ActiveWorkbook.Worksheets("VIAL TYPES").Range("A1:A309"), 7, False)
End Function

EXCEL VBA - Select Case - Index / Match

I have a spreadsheet which contains a massive IF then Formula and it's getting a bit out of control, the spreadsheet is getting slower and becoming more difficult to maintain.
Is it possible to move the formulas from the cells to VBA? Would this have any improvement on performance? I was thinking about using a Select Case but need some help as to whether a select case would work. The select case will need to look at multiple cells i.e. If A1 = Yes AND A2 = "Cash" AND A3 = No" then call the Index Match function to retrieve the value. Is this possible?
Is there a big performance gain moving formula's out of the worksheet into VBA?
Thanks for any advice.
Brett
I don't think you find any performance improvement simply from moving the same code from the cell into VBA.
However it will be easier to read, and you may then see that not all of the conditionals are necessary, so you may be able to simplify it - which would speed it up.
"Select Case" will probably not be helpful though as it can only be used for a single comparison, not three like you mention.
I would suggest taking it one step at a time, move the formula into VBA, and convert it to a VBA "if" statement. Then you may be able to see ways to tidy it up.
Yes and No.
The main advantage to using VBA is that you could then also use for or while loops.
So instead of writing the same formula for each cell, you can develop your code to do these calculations in as many cells as you need/want.
Its not an easy question to answer, due to the little amount of information provided in the question.
Having the code in Excel formulas mean that it will be recalculated when any of its parameters changes i.e. you change the value of one Excel cell -> all Excel cells that use this value will be recalculated. This may significantly slow down Excel when you are making constant changes.
One way of handling this is to manually turn off Automatic calculations (Formulas->Calculation Options->Manual). Make your changes and run the calculations manually by hitting Formulas->Calculate Now for recalculating the whole workbook or Formulas->Calculate Sheet for a recalc of only the worksheet.
Another way is to move the formulas to a VBA procedure. This way you can execute the calculation manually while all the other calculations in the workbook are carried out automatically.
EDIT1:
To make an VBA procedure from an Excel formula create a procedure like this:
Sub CalculateFormula
Range("A1").Value = Evaluate("YOUR EXCEL FORMULA WITHOUT '='")
'Simple example below
Range("A1").Value = Evaluate("IF(A1=1,1,2)")
End Sub
EDIT2:
With looping:
Sub CalculateFormula
for i = 1 to 10
Range("A" & i).Value = Evaluate("IF(B" & i & "=""Hello"",2,1)")
Next i
End Sub

How to get the underlying value in an error cell in excel using VBA

I have this issue to solve purely by VBA, no manual intervention allowed.
In excel, if one puts a non-numerical string starting with minus sign, e.g "-SomeData", the cell will show as "#NAME?". However, if one selects this cell, in the formula bar it will show "=-SomeData".
Now I need to extract "-SomeData" and paste it on another sheet. I tried .value but it returned error, and .text but it returned "#NAME?".
I browsed through the entire list for Range object on MSDN without finding a viable solution.
Is there a way to do this purely by VBA? Asking because the user of the macro refused to do manual formatting for the error cells.
Thanks in advance.
Assuming the error value is in the active cell, do something like:
Worksheets(1).Range("A1") = "'" & Activecell.Formula

Resources