[FONT="]This is excel 2019 being extended with vba. Trying some simple, nonsense first.... and then.... FRUSTRATION
the following code snippet is used as follows[/FONT]
[FONT="] [/FONT]
[FONT="]Cell #f21 has a value of tograms(a21,b21,16)[/FONT]
[FONT="]A21 is typed as a numeric and has a value of 16[/FONT]
[FONT="]B21 is typed as text and has a value of “oz”[/FONT]
[FONT="]16 is a number[/FONT]
[FONT="]The issue is:[/FONT]
[FONT="]The first test should fail because the arg = “oz”, not “g”, but the if test always passes[/FONT]
[FONT="]So that means i am doing something stupid (or rather not doing something i should be)[/FONT]
[FONT="]I can’t believe i am stuck on such an elementary thing…[/FONT]
[FONT="]FRUSTRATED[/FONT]
[FONT="] [/FONT]
[FONT="] [/FONT]
[FONT="]Function ToGrams(CellQty As Double, CellUoM As String, CnvFactor As Double) As Double[/FONT]
[FONT="]‘ This function converts cell contents to grams, as appropriate[/FONT]
[FONT="]Dim VolConvert As Double[/FONT]
[FONT="]If CellUoM = “g” Then ToGrams = Val(CellQty)[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 2) = “gr” Then ToGrams = Val(CellQty)[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If LCase(CellUoM) = “oz” Then ToGrams = Val(CellQty) * 28.3495[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 2) = “ts” Or Left(LCase(CellUoM), 2) = “tb” Or Left(LCase(CellUoM), 2) = “c” Then VolConvert = Val(CnvFactor) * Val(CellQty)[/FONT]
[FONT="]‘ ts will cover tsp[/FONT]
[FONT="]‘ tb will cover tbs[/FONT]
[FONT="]‘ c will cover cups[/FONT]
[FONT="] If Left(LCase(CellUoM), 2) = “ts” Then ToGrams = VolConvert[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="] If Left(LCase(CellUoM), 2) = “tb” Then ToGrams = VolConvert * 3[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="] ToGrams = VolConvert * 48[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 2) = “ml” Then ToGrams = Val(CnvFactor) * Val(CellQty) * 5[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 4) = “drop” Then ToGrams = Val(CnvFactor) * Val(CellQty) / 100[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 5) = “piece” Then ToGrams = Val(CnvFactor) * Val(CellQty)[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]End Function[/FONT]
the following code snippet is used as follows[/FONT]
[FONT="] [/FONT]
[FONT="]Cell #f21 has a value of tograms(a21,b21,16)[/FONT]
[FONT="]A21 is typed as a numeric and has a value of 16[/FONT]
[FONT="]B21 is typed as text and has a value of “oz”[/FONT]
[FONT="]16 is a number[/FONT]
[FONT="]The issue is:[/FONT]
[FONT="]The first test should fail because the arg = “oz”, not “g”, but the if test always passes[/FONT]
[FONT="]So that means i am doing something stupid (or rather not doing something i should be)[/FONT]
[FONT="]I can’t believe i am stuck on such an elementary thing…[/FONT]
[FONT="]FRUSTRATED[/FONT]
[FONT="] [/FONT]
[FONT="] [/FONT]
[FONT="]Function ToGrams(CellQty As Double, CellUoM As String, CnvFactor As Double) As Double[/FONT]
[FONT="]‘ This function converts cell contents to grams, as appropriate[/FONT]
[FONT="]Dim VolConvert As Double[/FONT]
[FONT="]If CellUoM = “g” Then ToGrams = Val(CellQty)[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 2) = “gr” Then ToGrams = Val(CellQty)[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If LCase(CellUoM) = “oz” Then ToGrams = Val(CellQty) * 28.3495[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 2) = “ts” Or Left(LCase(CellUoM), 2) = “tb” Or Left(LCase(CellUoM), 2) = “c” Then VolConvert = Val(CnvFactor) * Val(CellQty)[/FONT]
[FONT="]‘ ts will cover tsp[/FONT]
[FONT="]‘ tb will cover tbs[/FONT]
[FONT="]‘ c will cover cups[/FONT]
[FONT="] If Left(LCase(CellUoM), 2) = “ts” Then ToGrams = VolConvert[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="] If Left(LCase(CellUoM), 2) = “tb” Then ToGrams = VolConvert * 3[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="] ToGrams = VolConvert * 48[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 2) = “ml” Then ToGrams = Val(CnvFactor) * Val(CellQty) * 5[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 4) = “drop” Then ToGrams = Val(CnvFactor) * Val(CellQty) / 100[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]If Left(LCase(CellUoM), 5) = “piece” Then ToGrams = Val(CnvFactor) * Val(CellQty)[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="]End Function[/FONT]