VBA Code for User-Defined IFS Function in Excel - 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

Related

SHA512 hash in Excel

i have a code for generating SHA512 hash of a string in excel, but on some notebook, it runs into error thanks to a net framework related problem. I have came accross a solution recently; when I open the "Me" expression in the locals window, the code can run without any error.
Locals:
Is there any chance to insert an additional code which can expand the "Me" expression in locals when i start running my VBA code in excel?
Sub sha512_kodolas()
Sheets("a").Range("a1").Value = h512(Sheets("b").Range("c1").Value)
End Sub
Function h512(ByVal S As String) As String
'https://msdn.microsoft.com/en-us/library/system.security.cryptography.sha512managed.aspx
Static UTF8 As Object, SHA As Object
Dim Data, Temp, i As Long
If SHA Is Nothing Then
Set UTF8 = CreateObject("System.Text.UTF8Encoding")
Set SHA = CreateObject("System.Security.Cryptography.SHA512Managed")
End If
Data = SHA.ComputeHash_2(UTF8.GetBytes_4(S))
ReDim Temp(LBound(Data) To UBound(Data)) As String
For i = LBound(Data) To UBound(Data)
Temp(i) = Right$("0" & Hex(Data(i)), 2)
Next
h512 = Join(Temp, "")
End Function
could you try this code: https://github.com/krijnsent/crypto_vba -> download the excel and try the ComputeHash_C function? It's also carrying out the SHA512 encryption, but in a slightly different way from yours, so I'm curious to know if it also crashes?
I tried the suggestion from the author above and it works like a charm: "could you try this code: https://github.com/krijnsent/crypto_vba -> download the excel and try the ComputeHash_C function? It's also carrying out the SHA512 encryption, but in a slightly different way from yours, so I'm curious to know if it also crashes?".
Simply clone the Github project, copy the following functions into your Excel workbook:
Function ComputeHash_C(Meth As String, ByVal clearText As String, ByVal key As String, Optional OutType As String) As Variant
Function ConvToBase64String(vIn As Variant) As Variant
Function ConvToHexString(vIn As Variant) As Variant
Function Base64Decode(ByVal base64String)
Function Base64Encode(inData)
It works perfectly well! Thanks for sharing this code!
Using the Excel file from https://github.com/krijnsent/crypto_vba, it works for me if I use commas rather than semicolons in the formula: =ComputeHash_C("SHA512",A3,"","STRHEX"). This is just a small variation on what Luc Krolls has posted.

#NAME! Error with UDF functions previously working

I have an Excel workbook macro template (.xltm), with a number of VBA UDF used througout the sheets; nothing big or complex. The functions are declared Public and are saved in a module in the same workbook.
The template has been used for years, creating workbooks (.xlsm) in which the functions have always worked perfectly.
Now, opening the template to make some upgrades, I found ugly #NAME! errors in every cell containing one of my UDFs; if I try to insert a function in a cell, I do not find my functions listed with the Excel ones anymore.
The only thing changed in my Excel is the version, now 2019 64 bit; the template was created with the 2016 version (32 bit but worked with 64 bit also) and, I repeat, all things have always worked perfectly.
Maybe an Excel Guru can give me a hint to resolve this strange problem?
Edit an example as requested:
As I said, my functions are very simple and don't call any API code: for example, I have these two:
Const MinValue = 0.00001
Const NullValue = -999
Public Function IsZero(r As Range) As Boolean
'check the cell value, and returns True if the value is <= MinValue
If IsEmpty(r.Cells(1, 1)) Then
IsZero = True
ElseIf IsNumeric(r.Cells(1, 1)) Then
IsZero = (r.Cells(1, 1) <= Zero)
End If
End Function
Public Function Zero() As Double
' Simply returns the MinValue value.
Zero = MinValue
End Function
Excel sometimes has an issue 'remembering' the dependency chains for its calculations. A symptom of this is when a known functioning file has strange errors in cells that you know should have values.
A quick way to check if this is the case is to do a full rebuild of the dependency chain. From the keyboard this is CTRL+ALT+SHIFT+F9. If you find this is happening on a regular basis, you can put the following line into your Workbook_Open event:
Application.CalculationFullRebuild
Further suggested readings are:
https://learn.microsoft.com/en-us/office/client-developer/excel/excel-recalculation
https://learn.microsoft.com/en-us/office/vba/api/excel.application.calculatefullrebuild

Excel UDF not appearing in drop down menu

I wrote a User Defined Fucntion in Excel. It works great with no issues. I even wrote a description for it under the object properties menu.
The problem is, my UDF never shows up in the Excel drop down menu that appears when I start to type a function. I want the user to be able to see my UDF, named removeNumbers, when they go into a cell and start to type out a function.
I would also like them to be able to see the description which I wrote, just like the standard Excel functions.
And finally, is there a way that I can provide a description for each argument which my function takes as input?
Here is the actual code, although I don't think it will be necessary to answer my questions.
Function removeNumbers(sInput As String, sChoice As Boolean) As String
Dim sSpecialChars As String
Dim i As Long
If (sChoice = True) Then 'if true is selected, will remove all number including 0
sSpecialChars = "0123456789" 'This is your list of characters to be removed
For i = 1 To Len(sSpecialChars)
sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
Next
End If
If (sChoice = False) Then 'if false is selected, will remove all numbers excluding zero
sSpecialChars = "123456789" 'This is your list of characters to be removed
For i = 1 To Len(sSpecialChars)
sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
Next
End If
removeNumbers = sInput
End Function
To make the function appear in the drop-down you must place it in a standard module rather than the worksheet code area.
Another poster has already covered the need for the code to be in a standard module. With regards the argument descriptions, you should look at the MacroOptions code in this answer - although it only works in Excel 2010 or later.
For Excel 2007 and earlier, the only solution I have seen is in an article by JK Pieterse. This involves using the ExecuteExcel4Macro and looks a bit complicated.

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)

iserror formulae in excel

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

Resources