Excel - Blank Cells? - excel

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,"")

Related

Convert numbers to non-numbers in Excel

I notice that numeric values like 123456 can be considered as numbers or non-numbers in Excel. Mixing numbers and non-numbers may result in unexpected results of = or XLOOKUP.
For instance, in the following worksheet, the formula of D3 is =ISNUMBER(C3) and the formula of D4 is =ISNUMBER(C4). Their values are not the same. Then =C3=C4 in another cell will return FALSE; =XLOOKUP(C3,C4,C4) will return #N/A.
So one solution to avoid such surprises is that I would like to convert all these numeric values from numbers to non-numbers, before applying formulas on them.
Does anyone know if it is possible to undertake this conversion by manual operations (select the range, then...)?
Does anyone know how to achieve this conversion by a subroutine in VBA (select the range, then run the VBA subroutine, then the selected range will be converted)?
If you firstly write numbers in a range, let us say "C:C", formatted as General, any such a cell will return TRUE when you try =ISNUMBER(C4).
If you preliminary format the range as Text and after that write a number, this will be seen by Excel as a String (non-numbers, as you say...) and =ISNUMBER(C4) will return False.
Now, if you will try formatting the range as Text after writing the numbers these cells will not be changed in a way to make =ISNUMBER(C4) returning FALSE. In order to do that, you can use TextToColumns, as in the next example:
Private Sub testTextToCol()
Dim sh As Worksheet, rng As Range
Set sh = ActiveSheet
Set rng = sh.Range("C:C")
rng.TextToColumns Destination:=rng, FieldInfo:=Array(1, 2)
End Sub
It will make the existing =ISNUMBER(C4), initially returning TRUE, to return FALSE...
Of course you cannot compare apples to oranges, thus strings are not comparable to integers/longs/numbers. Make sure that all you compare are apples.
In a routine this would be s.th. like
Option Explicit
Sub changeFormat():
' Declare variables
Dim Number As Variant
Dim check As Boolean
'Converts the format of cells D3 and D4 to "Text"
Range("D3:D4").NumberFormat = "#"
'Assign cell to be evaluated
Number = Range("D3")
Debug.Print Number 'Prints '123'
check = WorksheetFunction.IsText(Trim(Sheets("Tabelle1").Cells(4, 3)))
Debug.Print check 'Prints True
'Converts the format of cells D3 and D4 to "Numbers"
Range("D3:D4").NumberFormat = "0.00"
'Compare Cells
If Range("D3").NumberFormat = Range("D4").NumberFormat Then Range("D5").Value = "Same Format"
End Sub
Also see the docs

How to add modification to all formulas from a selected range in excel

I would like to limit all of the formulas in a selected range of cells with an additional if statement.
Is it doable with the use of the VBA?
Let's say:
cell A1 contains =sum(B1;C1) but it can be any formula that a selected cell contains
after applying macro updated formula would be =if(D100>0;sum(B1;C1);0)
=if(D100>0;any formula in a cell ;0)
You can achieve this by using code like this:
Sub ModifyFormulas()
Dim cl As Range, formulas_cells As Range
Const template = "=if(D100>0,#,0)" '# will be replaced by old formula without '='
On Error Resume Next ' handle an error if there are no formulas in the range
Set formulas_cells = Range("A1:A10").SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not formulas_cells Is Nothing Then
For Each cl In formulas_cells
cl.Formula = Replace(template, "#", Mid(cl.Formula, 2))
Next
End If
End Sub
Without vba, I use the following method:
edit / replace the "=" with "xyxy" - this stops excel recalculating
then edit / replace "sum" with "if(D100>0;sum"
then edit / replace ")" with ");0)"
lastly replace "xyxy" with "="
and then it all works, done this often with my sheets when I have to change some calculations...

VBA code for overriding blank looking cells in excel

There are occasions when we use an IF formula, something like this in B1 for example =IF(A1=1,5000,""), and the results seem to be like either 5000 or a blank cells. But the blank cell is not actually blank as it contains a formula which has returned "" - that's why the cell looks blank.
When we drag the formula from B1 to B10 (say), then 10 cells are selected. And then I have written the below code to make the cells which have returned as "" to be empty.
Option Explicit
Sub delblanks()
For Each cell In Selection
If cell.Value = "" Then cell.Value = ""
Next
End Sub
...it triggers the error
variable not defined
How to fix this???
This here should fix it. Option Explicit gives an error when you do not declare your variables.
Option Explicit
Sub delblanks()
Dim cell As Range
For Each cell In Selection
If cell.Value = "" Then cell.Value = ""
Next
End Sub
Why not put an actual variable instead of just "" and delete it using your code?

Find and Replace values in cells containing formulae for Excel 2016

I want to use the Find and Replace in Excel to remove all zeroes from a column that holds say the values of column A-column C. So A2-C2, A3-C3 etc. But because its a formula it just wont work even though I have tried the 'match case/Match Entire contents' as well as other combinations in Options. This is a simple question but hard finding an answer as all questions seem to be directed at finding a part of a formula and replacing with something else.
If you do not want to see a "0" or "#VALUE!" in these columns, I would rewrite the formula such that it excludes these values as outputs (meaning any sum or avg function would exclude these cells). Try using either "iferror" or "if" formulas to exclude see examples below.
Excluding 0 using if statement: =IF(H8*G8 = 0,"",H8*G8)
Excluding #Value! using iferror: =IF(H8*G8 = 0,"",H8*G8)
Excluding 0 and #value using combined iferror/if statement: =IFERROR(IF(G20*H20 = 0," ",G20*H20),"")
If you want to exclude both 0s and #value! you can combine the formulas:
enter image description here
Assuming you do not want to rewrite the whole range of formulas, you dan do it via VBA (hit Alt+F11).Just paste that code somewhere in there. Now select the range you want to empty out of 0's and #Errors and run the code:
Sub RemZeroErr()
Dim rng As Range
For Each rng In Intersect(Selection, Selection.Parent.UsedRange).Cells
If IsError(rng) Then
rng.Value = ""
ElseIf rng.Value = 0 Then
rng.Value = ""
End If
Next
End Sub

Cell is empty but IsEmpty is not working

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

Resources