Checking if cell is empty with VBA - excel

Nom_1 is the name of a cell in my worksheet, and is used in the following code.
Someimes that cell is left empty and I don't want the NormInv function to get called because it will return an error if the inputs are empty.
I get the error: Unable to get the NormInv property of the WorksheetFunction class
This leads me to believe my if statement is incorrect and it is allowing for the code to enter and execute even if Nom_1 is empty.
If Not IsEmpty(Nom_1) Then
internal_1 = Application.WorksheetFunction.NormInv(Rnd(), N_1, Std_1)
End If
Am I correctly checking if the cell is empty?

Just Expanding on Barranka's comment,
You need to make sure all cells with the names Nom_1 ,N_1,Std_1,internal_1 exist in the workbook
then use Range("Nom_1") instead of Nom_1
Sub MyNormInv()
If Not IsEmpty(Range("Nom_1")) Then
Range("internal_1") = Application.WorksheetFunction.NormInv(Rnd(), Range("N_1"), Range("Std_1"))
End If
End Sub

You could use
If Range(Nom_1).Value <> vbNullString Then
internal_1 = Application.WorksheetFunction.NormInv(Rnd(), N_1, Std_1)
End If
However, know that this will return False if you have a formula returning a zero length string in the cell. Even though the cell is not "empty" it will still be seen as blank because the value is "".

Related

VBA Code to look up function in cell, store in variable and copy somewhere else

I'm trying to come up with a script that looks up a function in named range "Hard_Constr_Cost". Saves the function in variable "HC_Baseline" and then copies it to another range.
The reason why I don't just set one range equal to another is that after the function is stored I want to run a separate loop overwriting the value in range "Hard_Constr_Cost". At the end I want to overwrite with the original function stored in the variable "HC_Baseline".
Below is the script I created so far but I'm getting an out-of-range error. Does anyone know how to fix this? Any help is appreciated.
Sub HC_Sensitivity()
Dim HC_Baseline As String
HC_Baseline = Sheets("SU").Range("Hard_Constr_Cost").Formula
Sheets("SU").Range("T5") = HC_Baseline
End Sub

Excel VLookup VBA cant refer to another worksheet

Public Function Lohn(ID)
Lohn = Application.WorksheetFunction.VLookup(ID, ThisWorkbook.Worksheets("Arbeiter").Range("A:D"), 2, 0)
End Function
I cant access the Worksheet "Arbeiter" its just doesn't give me a Value back can some one help me
There is nothing more code in the Section if anyone wants to know
ID is numeric
I got it to work after changing from Application.WorksheetFunction.VLookup to Application.VLookup
Be careful to load in an ID with the same data type as the value in the cell. It gave me error 2042 when i used a string as the ID but was able to work properly when I put in an integer.
Here is your function after my changes:
Public Function Lohn(ID)
Lohn = Application.VLookup(ID, ThisWorkbook.Worksheets("Arbeiter").Range("A:D"), 2, False)
End Function

Identify all cells that have been conditionally formatted

I am trying to remove duplicate values except the first one.
My solution to this was to conditionally format all duplicates, then, working backwards, clear contents of the formatted cells. This would mean that the first one will stop being formatted once all duplicates are removed.
What I have been trying:
For i = LaC To 5 Step -1
LR = ws.Cells(Rows.Count, LaC).End(xlUp).Row
For j = 2 To LR
cond = (ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value)
If cond = 22 Then
ws.Cells(j, i).ClearContents
End If
Next
Next
Basically, if I try ws.Cells(j, i).DisplayFormat.Interior.ColourIndex in the immediate window, it returns 22.
However, if I try this code, I get error:
Object doesn't support this property or method (Error 438)
Any assistance would be greatly appreciated.
If it is not necessary to use VBA you can get rid of duplicates in the GUI via “Data” → “Data Tools” → “Remove Duplicates”. Or, in current versions of Excel O365, you can use the UNIQUE-function like this
UNIQUE(A:A)
assuming your source data is in row A. This will keep up with changing data.
Check this line more carefully:
cond = (ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value)
ColorIndex is a Property, not an Object. As such, ColorIndex.Value will return an error. Note that you haven't included the .Value when testing in the Immediate Window, and it works as expected:
?ws.Cells(j, i).DisplayFormat.Interior.ColourIndex 'This will work
?ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value 'This will err
Also, there is no need for you to put it in brackets, either:
cond = ws.Cells(j, i).DisplayFormat.Interior.ColourIndex
Ans: I was writing Colour instead of Color.

Handling errors

I am trying to perform some operations over some data, but they are not working and I need to find a way to ignore those cells which don´t meet all the requirements.
Basically, I have a column where some cells have text + numbers in their content and other have only text. I search inside all of them and split TEXT in one column and NUMBER in another one. Then, I run a macro to find the matching text to each one in other column.
But when I try to split TEXT from NUMBERS, I search for the first "(", cause my number format is (10.10.10), but if it is not found, the cell value appears: #VALUE! (ok, it is expected cause the character was not found). Here is the problem: if I run my macro over "#VALUE! it crashs and don´t finish its execution.
I have tried to use
On Error GoTo
in my macro code, but for some reason it doesn´t not handle the "Run time error: Type Mismatch".
For contadorOr = 2 To colO
For contadorDes = 2 To colA
On Error GoTo cont
If InStr(1, Cells(contadorDes, colunaDestino).Value, Cells(contadorOr, colunaOrigem).Value) Then
If InStr(1, Cells(contadorOr, colunaOrigem + 4).Value, Cells(contadorDes, colunaDestino + 1).Value) Then
Cells(contadorOr, colunaOrigem + 5).Value = "Mesma versão"
End If
Exit For
End If
Next contadorDes
cont: Next contadorOr
Any suggestions? I can think of ignoring this error (when it´s happen, my variable contadorOr is incremented and go to no next value) or any way to avoid #VALUE! returned by my functions, but haven´t had success doing that.
Thanks in advance.
Instead of using error handling options you could check if cell which you are going to check/process doesn't return error. This is quite simple as presented below:
If IsError(Cells(contadorDes, colunaDestino).Value) Then
'to do anything if there is error
'usually...do nothing
Else
'do what you want if there is no error
End if

VBA function "Object variable missing" error. Number 91

Excel keeps lighting it up with an "Object variable missing" error. Number 91.
Function GetMonthRange(sheetMonth) As Range
GetMonthRange = ActiveCell.Range("A1:AB1")
End Function
I'm pretty sure that Excel is maintaining its own clipboard.
Here's the link to the whole file.
https://github.com/okamura1967/Directors_project_sheet/blob/master/project-sheet-for-directors.vbs
There are several things wrong with your function.
1. If you want to return a range you have to use Set because Range is an object.
2. The parameter sheetMonth is not used
3. The function will return different results depending on whatever the activecell happens to be when the function is executed.
4. If this is a UDF it will not recalculate whenever anything in A1:B1 changes, because the A1:B1 is not a parameter.
What are you actually trying to do?
I changed your function to:
Function GetMonthRange() As Range
Set GetMonthRange = ActiveSheet.Range("A1:AB1")
End Function
This seems to work for me now.

Resources