Excel- DNA C# Script – for adding a missing “0” in Danish Social Security Number

Since I wrote this UDF in VBA; I have had plans for making a version written purely in C#- simply to make a higher performing function for large volume data. Excel-DNA UDF makes faster calculations than Excel  – some estimates say 40% faster calculation rate  than excel VBA. I have to test that claim !

Here it comes – my first attempt which I admit could be more smooth.

[ExcelFunction(Description = “My first .NET function”)]

public static object CorrectCPR(object CCR,object CCRU)

{

String InterN;

String BN;

String BZ;

BZ = “0”;

InterN = Convert.ToString(CCR);

BN = Convert.ToString(CCR);

if (InterN.Length == 9)

{

BN = BZ + InterN;

}

else

if (InterN.Length == 10)

{

BN = InterN;

}

else

if (InterN.Length <9)

{

BN = “too short”;

}

else

if (InterN.Length == 8)

{

BN = “too short”;

}

else

if (InterN.Length <8)

{

BN = “too short”;

}

else

if (InterN.Length>10)

{

BN = “too long”;

}

else

if (InterN.Length ==0)

{

BN = “Empty field”;

}

CCRU = Convert.ToString(BN);

return CCRU;

}

}

}

 

For further reading https://excel-dna.net/