Cell is empty but IsEmpty is not working - excel

I am writing an IF statement that uses the IsEmpty function to determine if True or False.
I tried it both on a cell with a value (e.g., PRB2039) and on a blank cell to test my code, and the result is the same.
I removed formatting, and tried it on a new worksheet. I don't know what I'm doing wrong.

I prefer using
If Len(Trim(Cells(i, 1).Value))=0 Then Msgbox "Empty"

As suggested by #PatricK you may consider using ISBLANK function instead of IsEmpty function. ISBLANK function Returns TRUE if the value is blank
Using VBA
Sub test()
For i = 1 To 4
MsgBox Evaluate("isblank(" & Cells(i, 1).Address & ")")
Next
End Sub

Related

Why the VBA UDF "Range.Formula =" don't work?

I want to copy the formula from one cell/cells to another cell/cells by Range.Formula = .
But it not work as expected.
If I run the VBA step by step, the function will ended at Range.Formula = without error.
Function test1(sOURCE As Range, tARGET As Range)
tARGET.Formula = sOURCE.Formula
test1 = tARGET.Formula
End Function
You are trying to change another cell's formula with a UDF. According to the Microsoft documentation this cannot be done, but Ryan Wells has actually found a nice workaround. See: How to change another cell with a VBA function UDF. Compare also: VBA: How to change the value of another cell via a function?
We need to use Evaluate on a "helper" sub. I have slightly adjusted the first example provided by Wells to suit your needs:
Function copyFormula(copyFrom As Range, copyTo As Range)
copyFrom.Parent.Evaluate "copyOver(" & copyFrom.Address() _
& "," & copyTo.Address() & ")"
copyFormula = "Formula " & copyFrom.Address() & " -> " & copyTo.Address()
End Function
Private Sub copyOver(copyFrom As Range, copyTo As Range)
copyTo.Formula = copyFrom.Formula
End Sub
This works, but please bear in mind that (intriguingly) the formula will not automatically calculate after insertion, even with calculation set to automatic. Implementation:
Result:
One way to overcome this problem is to include a Worksheet_Change sub for the worksheet where you are using the formula. E.g. simply:
Private Sub Worksheet_Change(ByVal Target As Range)
Calculate
End Sub
But any subsequent action in the sheet seems to trigger the calculation. Also, please be aware that you won't be able to overwrite the target cell while you have a copyFormula in use with this cell. Since it will just keep overwriting it immediately with the formula (with 0 as output, if the above trick isn't applied). This might be confusing to your users.
This is a restriction by design.
Because a UDF cannot change any other cells/formulas. A UDF can only return a value to the cell the UDF was used in. Therefore tARGET.Formula = sOURCE.Formula is not possible.
Also test1 = tARGET.Formula will return the formula of tARGET as text. It will not replace the formula used in the cell nor will it evaluate the formula.

Evaluate formula not working in VBA

What is the wrong with the following formula ?
matchformula = "{=MATCH(1, (G12= G:G) , 0)}"
x = MySheet.Evaluate(matchformula)
Whereas the code below yields a correct result.
matchformula = "=MATCH(G12, G:G , 0)"
x = MySheet.Evaluate(matchformula)
VBA should handle this automatically. Consider this array formula case:
In VBA the equivalent code gives the same results:
Sub qwerty()
MsgBox Evaluate("MIN(IF(A1:A3>2,A1:A3))")
End Sub
There is no need to tell VBA that the formula should be treated as an array formula.

Excel - Blank Cells?

I would like to return a blank cell from an if statement (Let's call this cell SHEET2!A1):
=IF(MAIN!E5=0,"",MAIN!E5)
However, if I call this:
=ISBLANK(SHEET2!A1)
The result is FALSE. I don't understand! I've tried resulting to NA() or just leaving the "" out of the formula, but to no effect. THE CELL IS NOT BLANK - Excel tells me so.
How do I result this formula to NOTHING?
BUZZYSIN
Yes you have to use VBA something like the below code
Sub delempty()
Dim Rng As Range
Set Rng = ActiveSheet.Range("A1:A10")
Dim i As Long
For i = 1 To 10
If Rng.Cells(i,1) = "" Then
Rng.Cells(i,1).ClearContents
End If
Next i
End Sub
If testing a cell containing a formula for the presence of "", you have to compare the result to "" rather than using ISBLANK:-
=A1=""
ISBLANK only works for cells which are completely empty.
You still need to wrap the isblank formula as an If statement or else by default it will only return true or false
Try syntax similar to this:
=if (istext(a1), a1,"")
Or
=if(isblank (a1), a1,"")

Applying VLOOKUP function with IF statement to a range

I'm trying to write a VBA code that would be the equivalent of this excel function for a definite range of cells.
=if(A1="", "", vlookup(A1, K1:M2000, 3, false))
I want to apply this for range(A1:A15) and insert the function in range (B1:B15) using VBA.
Not sure why you'd want to do this, but you can easily make this function in VBA using the below code. The above function will actually be faster and more efficient in the workbook rather than making the below, but it is possible.
Using the below function should now work in your file for cell B1. Then just copy and paste through B2:B15.
=customVLOOKUP(A1,$K$1:$M$2000,3,FALSE)
Note that the last two arguments are set to 3 and false by default, so =customVLOOKUP(A1,$K$1:$M$2000) will give the same result as above
Function customVLOOKUP(lookup As Range, lookupRng As Range, Optional lookupCol As Integer = 3, Optional lookupType As Boolean = False) As Variant
' check to see if lookup value is empty string
' if so return empty string to function and exit
' otherwise, evaluate normal vlookup function
If lookup.Value = "" Then
customVLOOKUP = ""
Exit Function
Else
customVLOOKUP = Application.WorksheetFunction.VLookup(lookup.Value2, lookupRng, lookupCol, lookupType)
End If
End Function
If you are looking to put that formula into B1:B15 and have all of the column A references adjust as the formula is filled down, you do not need to loop through the cells. Put the formula into the B1:B15 block of cells and the references to column A will adjust automatically.
with activesheet '<-reference this worksheet properly!
with .range("B1:B15")
' use this one
.formula = "=if(len(A1), iferror(vlookup(A1, K:M, 3, false), """"), """")"
' or this one
.formulaR1C1 = "=if(len(RC[-1]), iferror(vlookup(A1, C11:C13, 3, false), """"), """")"
end with
end with
Note that each of the pairs of double quotes from the original formula needs to be doubled up as they are quotes within a quoted string. I added an IFERROR function to handle non-matches and changes the check on A1 from "" to the LEN function to ease the double of quotes in the formula.

excel 2013 interior.color "#Value!" error

When i try to change color of a cell using a function like this:
Function abcd()
Worksheets("Sheet1").Cells(1, 1).Interior.ColorIndex = 3
End Function
by calling the function from a cell "=abcd()" it returns a "#Value!" error.
but if i use a sub and run it manually (by pressing the run button) it will work fine.
Sub abcd()
Worksheets("Sheet1").Cells(1, 1).Interior.ColorIndex = 3
End Sub
but i want call the function from a cell and i dont want to use conditional formatting.
what am i doing wrong?
A function can only return a value to a cell, it cannot change formats. Use Conditional Formatting

Resources