Check if cell if empty - vb.net

I am trying to check if the cell (A5) in sheet tstDash is empty. Currently the cell contains a formula but sometimes it the result is blank and sometimes is a number.
If it is blank, I want a message box. Otherwise perform other function.
I have this code but it is completely ignoring the line and giving me fits.
If chk25thPercentile.Checked = True And Globals.tsdDash.Range("A5").Value Is DBNull.Value

Try this ..
If chk25thPercentile.Checked And IsDBNull(Globals.tsdDash.Range("A5").Value)

What language are you writing in ? VBA or VB.Net ? VBA won't accept IsDBNull. If you are in VBA you want to be testing for the blank result of the formula, which is probably just an empty string ""
I expect you need something like
If chk25thPercentile.Checked And Globals.tsdDash.Range("A5").Value = ""
That is assuming that the result of your formula is an empty string, if the cell is actually blank, ie, without a formula you might want to look at ISBLANK

Related

Data imported to Excel as "general", text started with "=", getting "#name?" Is there a way to get it back to text?

On of our offices started their text data with an equals sign, which, when imported into the spreadsheet, gave the #NAME? error.
Now, I know I can change the cell format to text and then click in the formula bar, and excel recognizes the cell as text, but is there a way to do this through VBA?
EXAMPLE:
Cell display value: #NAME?
cell text =-Notice, ABC
Desired OUTPUT: Notice, ABC
now, normally, I'd do something like
sub convert_it()
if left(cells(1,1).value,2) ="=-" then cells(1,1).value = _
right(cells(1,1).value, len(cells(1,1).value) -2)
end sub
but that returns a type mismatch error.
Is there some way I can get the text in the cell without manually clicking?
I can click a few thousand times and then Excel recognizes it as text again, and not as an error, but I'd rather avoid that.
You can do this with VB, or just highlight the whole sheet, hit CTRL-H, type = in the Find and '= in the Replace, then click Replace All. This will make Excel stop thinking that these are formulas.
Instead of using .Value you have to use .Formula, since it's being interpreted as such here:
sub convert_it()
if left(cells(1,1).formula,2) ="=-" then cells(1,1).formula = _
right(cells(1,1).formula, len(cells(1,1).formula) -2)
end sub
Naturally you would place your code within a loop for the column. But I agree that it might be easier to just do a large find/replace.

Get value from cell without transform in vba macros

I have an issue with vba macros. Cell contains value 941144280284022000000. But when try to get this value in macros, variable is equal
9.41144280284022E+20. Is it possible to get "real" value?
Thanks.
Here is an example:
Sub dural()
MsgBox ActiveCell.Text
End Sub
This will insure that long strings of numerals are not converted to numbers.
EDIT#1:
This assumes that the cell actually displays the long string of numerals. If the cell displays ####, that is what the sub will pick-up. If the cell displays 9.41E+20, then that is what my sub will pick-up.
My sub will not necessarily pick-up the contents of the Formula Bar.
Try a simple :
Dim myVar as Variant
myVar = CDec(Range("A1").Value)
If you wish to store a value that looks like a number (or is a number) but has more than 15 digits of precision, then you must either format the cell as text before entering the value, or prepend the value with a single apostrophe to indicate to Excel that the value is to be treated as text and not a number.
If you don't do this, then as soon as the value is entered, there's a good chance it will be altered by Excel. The trailing zeros in the example value mean this does not happen in this specific case, but try changing that last 0 to 1 and you'll see what I mean.
You enter: 941144280284022000001
Excel converts this to: 941144280284022000000
More reading: https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel

Calculating Formula After Text Paste

I have a formula that constructs a vlookup function while drawing the file path from a date input from the user, next I have a macro that takes the results of this formula and pastes them in Cell C6.
=("=" & "IF(ISNA(VLOOKUP($A6," & "'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE)),"""",VLOOKUP($A6,"&"'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE))")
The trouble I am having is finding a way for VBA to activate the formula instead of having it sit there in a text format as:
=IF(ISNA(VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE)),"",VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE))
Afterward I just have a macro autofill it down for the other names between column A6:A37.
If anyone can help me with VBA activating the cell, it would be much appreciated.
If you want to get the value of a cell (which might contain a text string) you use something like:
someVariableEtc = Range("A1").Value
If you want to set a Cell's formula using VBA, you would use something like:
Range("C6").Formula = "some text string"
Combining the two, you could come up with something like:
Range("C6").Formula = Range("A1").Value
Look this up. The evaluate function might be what you're looking for. The simplest way to use it is wrapping an expression in square brackets [].

How to VBA change cell's value (display text) without changing its formula?

I've a problem with this VBA macro.
Sub demoChangeText()
Application.Range("B2").Value = "Overidden text"
End Sub
My test is here. To run this macro, open it in Excel, press Ctrl+F8 and choose demoChangeText.
As the result, this macro changes the value of cell B2 (the text displayed to us) but clear its formula. I need to change B2's value BUT also need the formula to be remained.
So my question is How to change the display text of cell without changing its formula?
UPDATE
I ask this question because I'm trying to solve this problem
I'm not sure if this will help, as it is a bit hard to tell what your underlying requirement is, but here goes anyway:
Several things affect the display of a cell:
the entered value, if its a constant
the result of a calculation, if its a formula
the format of the cell
the conditional format(s) of the cell, if any
In the example sheet provided you have a formula of =ROW()&COLUMN() which returns a string result of 22
You can make this display something else by applying a cell format,
eg a format of 0;0;0;Ov\e\r\ri\d\d\e\n t\ext will display any string value as Overridden text
This can by applied with VBA with
Range("B2").NumberFormat = "0;0;0;Ov\e\r\ri\d\d\e\n t\ext\s"
or
Range("B2").NumberFormat = "0;0;0;""Overridden texts"""

Excel VBA - Interpret "N/A" Values

I am traversing a spreadsheet which contains a column of prices, in the form of double types. I am trying to locate a missing value which is shown on the spreadsheet as "n/a", but it is not letting me interpret this as a string type.
The cell containing "n/a" seems to be an integer type; how can I read this?
If all you want to do is to check for the error value then:
Application.WorksheetFunction.IsNA(rngToCheck.Value)
where rngToCheck is the cell which you want to check for the #N/A error value
(There's a list of the worksheet functions which can be called from Excel VBA here)
You could also examine rngToCheck.Text as this will contain the string "#N/A"
If instead, you want to read the formula in the cell which generated the #N/A then rngToCheck.Formula would do that
A cell containing #N/A is retrieved by VBA as a variant containing an error code
In general its usually best to assign Excel cells to Variants because a cell can contain a number(double), logical, string or error and you cannot tell in advance what the cell wil contain.
You can prepare the spreadsheet you like to check as described below and evaluate the special cells containing the IS Functions, it is easy to check them for True or False in VBA. Alternatively, you can write your own VBA function as shown below.
There are Excel functions which check cells for special values, for example:
=ISNA(C1)
(assumed that C1 is the cell to check). This will return True if the cell is #N/A, otherwise False.
If you want to show whether a range of cells (say "C1:C17") has any cell containing #N/A or not, it might look sensible to use:
=if(ISNA(C1:C17); "There are #N/A's in one of the cells"; "")
Sadly, this is not the case, it will not work as expected. You can only evaluate a single cell.
However, you can do it indirectly using:
=if(COUNTIF(E1:E17;TRUE)>0; "There are #N/A's in one of the cells"; "")
assuming that each of the cells E1 through E17 contains the ISNA formulas for each cell to check:
=ISNA(C1)
=ISNA(C2)
...
=ISNA(C17)
You can hide column E by right-clicking on the column and selecting Hide in Excel's context menu so the user of your spreadsheet cannot see this column. They can still be accessed and evaluated, even if they are hidden.
In VBA you can pass a range object as RANGE parameter and evaluate the values individually by using a FOR loop:
Public Function checkCells(Rg As Range) As Boolean
Dim result As Boolean
result = False
For Each r In Rg
If Application.WorksheetFunction.IsNA(r) Then
result = True
Exit For
End If
Next
checkCells = result
End Function
This function uses the IsNA() function internally. It must be placed inside a module, and can then be used inside a spreadsheet like:
=checkCells(A1:E5)
It returns True, if any cell is #N/A, otherwise False. You must save the workbook as macro-enabled workbook (extension XLSM), and ensure that macros are not disabled.
Excel provides more functions like the above:
ISERROR(), ISERR(), ISBLANK(), ISEVEN(), ISODD(), ISLOGICAL(),
ISNONTEXT(), ISNUMBER(), ISREF(), ISTEXT(), ISPMT()
For example, ISERR() checks for all cell errors except #N/A and is useful to detect calculation errors.
All of these functions are described in the built in help of Excel (press F1 and then enter "IS Functions" as search text for an explanation). Some of them can be used inside VBA, some can only be used as a cell macro function.