iserror formulae in excel - string

In excel if I have the following
=match("abc",A:A,0)
if it errors then it throws some thing like
#value
so i tide this up by saying
=iserror((match("abc",A:A,0),"Not found",match("abc",A:A,0) )
but this seems messy code.. running the same formula twice, can this be formated better to give the same result?
Cheers

Which version of Excel are you using? In Excel 2007 or later versions you can use IFERROR function to avoid repetition
=IFERROR(MATCH("abc",A:A,0),"Not found")
or in earlier versions you could employ COUNTIF
=IF(COUNTIF(A:A,"abc"),MATCH("abc",A:A,0),"Not found")

I'm not aware of a built-in way to do this, but you could write your own VBA function:
Function GracefulError(varValue As Variant, strMessage As String) As Variant
If IsError(varValue) Then
GracefulError = strMessage
Else
GracefulError = varValue
End If
End Function
Usage:
=GracefulError(match("abc",A:A,0), "Not found")

Related

implementation of excel function 'filter' in vba

I have filter function in excel and it works well in excel spreadsheet. I wrote this function in cell G7. Following is the function. Click here for reference image
=FILTER(A3:C19,ISNUMBER(SEARCH("nut",B3:B19)),"Not found")
I need to implement this in VBA. So I wrote following code in vba:
Sheets("Sheet1").Range("G7").Value = Filter(Sheets("Sheet1").Range("A3:C19"), WorksheetFunction.IsNumber(WorksheetFunction.Search("nut", Sheets("Sheet1").Range("B3:B19"))), "Not found")
However this gives me error "Run time error 13: type mismatch". What is my mistake? An expert advice is needed.
Thanks..
You should be using Application.Filter not Filter, Filter is a VBA function, and you should return the result to a variant variable.
Dim Res As Variant
With Application
Res = .Filter(Sheets("Sheet1").Range("A3:C19"), .IsNumber(.Search("nut", Sheets("Sheet1").Range("B3:B19"))), "Not found")
End With
If Not IsError(Res) Then
Sheets("Sheet1").Range("G7").Resize(UBound(Res, 1), 3).Value = Res
End If

FIND in reverse (from right to left, FINDrev) function Excel UDF

I spent a long time searching for a simple FIND-in-reverse function in Excel and found some formulas, which were way too long for my taste (e.g. How can I perform a reverse string search in Excel without using VBA?).
So I ended up creating my own simpler one, below, which gives you the last word in a string by finding the position of the last space (or the 1st one from right to left).
=RIGHT(A1,FINDrev(” “,A1))
If you run this formula on the sentence above it (put in cell A1), it will give you the result "left)." All you need is to define a 3-row VBA UDF FINDrev() and save it in a permanently available xlam add-in:
Public Function FINDrev(Find_text As String, Within_text As String)
FINDrev = Len(Within_text)-Len(Find_text)-InStrRev(Within_text, Find_text)+1
End Function
Has anyone found a simpler solution for FIND-in-reverse?
You could try the VBA build-in function InStrRev().
You can find document here, https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/instrrev-function .
I will make a UDF like this.
Function myRightRev(find_text As String, Within_text As String) As String
myRightRev = Mid(Within_text, InStrRev(Within_text, find_text) + 1)
End Function

VBA Code for User-Defined IFS Function in Excel

Our firm uses the packaged version of Excel 2016, and not the Office 365 version. I've recently learned of the IFS function present in the newer versions, and it seems incredibly useful. I've found the CONCATENATEIF function here on the site in VBA form, and I'm wondering if there's a way to replicate this formula in my older version in the same way. I'm not very experienced in VBA coding, so I'd appreciate the help!
The following reproduces the functionality of IFS(), using ParamArray to handle an arbitrary number of parameters:
Public Function UdfIfs(ParamArray args() As Variant) As Variant
Dim i As Integer
i = 0 ' or 1 if you're not using zero-based indexing
Do Until CBool(args(i)) Or (i >= UBound(args))
i = i + 2
Loop
If i < UBound(args) Then
UdfIfs = args(i + 1)
End If
End Function

MS Excel Formula Correction

I am Using below formula to Search some Keywords from Particular Columns. But This one is giving error in Excel may be Excel is limited to 7 Nested IF statements Only.
=IF(ISERROR(SEARCH("*SIPC*",J2,1)),IF(ISERROR(SEARCH("*HIU*",J2,1)),IF(ISERROR(SEARCH("*GMC*",J2,1)),IF(ISERROR(SEARCH("*CNS*",J2,1)),IF(ISERROR(SEARCH("*LCSM*",J2,1)),IF(ISERROR(SEARCH("*RoHC*",J2,1)),IF(ISERROR(SEARCH("*GL1*",J2,1)),IF(ISERROR(SEARCH("*GL3*",J2,1)),IF(ISERROR(SEARCH("*GL2*",J2,1)),IF(ISERROR(SEARCH("*URRC*",J2,1)),IF(ISERROR(SEARCH("*UPHY*",J2,1)),IF(ISERROR(SEARCH("*UHAL*",J2,1)),IF(ISERROR(SEARCH("*UMAC*",J2,1)),"","SIPC"),"HIU"),"GMC"),"CNS"),"LCSM"),"RoHC"),"GL1"),"GL3"),"GL2"),"URRC"),"UPHY"),"UHAL")
Can anyone Please suggest me any alternatives for this to get more than 10 Nested IF statements.
Thanks in Advance..:)
If it is acceptable to use Visual Basic for Applications (VBA), create a macro and define the following function in it:
public function GetCode(s as string) as string
GetCode = ""
codes = split( _
"SIPC|HIU|GMC|CNS|LCSM|RoHC|GL1|GL3|GL2|URRC|UPHY|UHAL|UMAC", "|")
for each code in codes
if InStr(s, code) > 0 then
GetCode = code
exit for
endif
next
end function
You should then be able to access the function in your formula as follows:
= GetCode(J2)

Excel VBA - Execute string as code

I am trying to execute the following string as code using Evaluate but get Error 2029. Anyone know why?
Help much appreciated.
Calculation = "Format(""21/08/2012"", ""MMM"")"
Value = Evaluate(Calculation)
Try instead
Calculation = "TEXT(""21/08/2012"", ""MMM"")"
EVALUATE converts formulas to results, and FORMAT is a VBA function. The formula equivalent is TEXT.
You can also skip the evaluate and use the FORMAT function on the date directly.
You can use most worksheet functions in VBA directly with Application.WorksheetFunction. - for example - try this out:
Sub DateExample()
Dim StringTest As String
StringTest = Application.WorksheetFunction.Text("12/08/2012", "MMM")
Cells(1, 1).Value = StringTest
End Sub
Good Luck

Resources