Error with Case Statement using a range of number - basic

I receive an error that states
end statement expected
Should I be using something besides to? I've tried or and & and receive similar errors.
Dim interp As String = ""
Select Case BmiValue
Case Is < 18.5
interp = "Underweight"
Case Is <18.5 to >=24.9
interp = "Normal"
Case Is <24.9 to >=29.9
interp = "Overweight"
Case Else >/= 30.0
interp = "Obese"

You need an End Select just after your code.
Dim interp As String = ""
Select Case BmiValue
Case Is < 18.5
interp = "Underweight"
Case Is <18.5 to >=24.9
interp = "Normal"
Case Is <24.9 to >=29.9
interp = "Overweight"
Case Else >/= 30.0
interp = "Obese"
End Select

Finally got it!!!!! I took out the 'to' in each line and the '/' between >+ and it worked!!!! Have not idea why it worked this time and not the 100 other times I tried different things!!!!

Related

How do I only loop through certain parts of a cell array?

I am trying to figure out a way to make a for loop in which I can compare two cells that will give me two different means. One for class char and the other for class double.
This is what I have so far.
V = {2; 'tree'; 3; 'hope'};
W = {2; 'tree'; 3; 'hope'};
for i = 1:length(V);
if isequal(class(V{i}), 'double')
num = V{i}
elseif isequal(class(V{i}), 'char')
str = V{i}
end
end
for i = 1:length(W);
if isequal(class(W{i}), 'double')
acc_n(i) = isequal(V{i}, W{i})
elseif isequal(class(W{i}), 'char')
acc_s(i) = strcmp(V{i}, W{i})
end
end
mean_d = mean(acc_n)
mean_s = mean(acc_s)
The output I get is:
acc_n =
1 0 1
acc_s =
0 1 0 1
mean_d =
0.6667
mean_s =
0.5000
The output I want is:
1 1 for string, mean = 1. 1 1 for double, mean = 1
How can I do a loop where it only takes the numbers of the cell and the words of the cell separately?
Is there any possible way to only loop through the words or the numbers?
You can first extract strings and doubles and treat them separately, that will avoid loops.
V = {2; 'tree'; 3; 'hope'};
W = {2; 'tree'; 3; 'hope'};
VChar=V(cellfun(#ischar,V));
WChar=W(cellfun(#ischar,W));
acc_s=VChar==WChar;
VNum=cell2mat(V(cellfun(#isnumeric,V)));
WNum=cell2mat(W(cellfun(#isnumeric,W)));
acc_n=VNum==WNum;
Loop version: I haven't tested this but it should work.
%Assumes V and W have equal number of elements.
acc_n=[];
acc_s=[];
for i=1:numel(V)
if isequal(class(V{i}), 'double') && isequal(V{i},W{i})
acc_n=[acc_n true];
elseif isequal(class(V{i}), 'char') && strcmp(V{i},W{i})
acc_s=[acc_s true];
end
end

Processing special characters

Let's say I receive the following string in Lua mÜ⌠⌠í∩and would like to apply it to my current processing code, which is the following
function inTable(tbl, item)
for key, value in pairs(tbl) do
if value == item then return true end
end
return false
end
function processstring(instr)
finmsg = ""
achar = {131,132,133,134,142,143,145,146,160,166,181,182,183,198,199,224}
echar = {130,137,138,144,228}
ichar = {139,140,141,161,173,179,244}
ochar = {147,148,149,153,162,167,229,233,234,248}
uchar = {129,150,151,154,163}
nchar = {164,165,227,252}
outmsg = string.upper(instr)
for c in outmsg:gmatch"." do
bc = string.byte(c)
if(bc <= 47 or (bc>=58 and bc<=64) or (bc>=91 and bc<=96) or bc >=123)then
elseif (bc == 52) then finmsg = finmsg.."A"
elseif (bc == 51) then finmsg = finmsg.."E"
elseif (bc == 49) then finmsg = finmsg.."I"
elseif (bc == 48) then finmsg = finmsg.."O"
elseif (inTable(achar, bc)==true) then finmsg = finmsg.."A"
elseif (inTable(echar, bc)==true) then finmsg = finmsg.."E"
elseif (inTable(ichar, bc)==true) then finmsg = finmsg.."I"
elseif (inTable(ochar, bc)==true) then finmsg = finmsg.."O"
elseif (inTable(uchar, bc)==true) then finmsg = finmsg.."U"
elseif (inTable(nchar, bc)==true) then finmsg = finmsg.."N"
else
finmsg = finmsg..c
end
end
return finmsg
end
function checkword (instr)
specword = [[]]
wordlist = {"FIN", "FFI", "PHIN", "PHEN", "FIN", "PHIN", "IFFUM", "MUF", "MEUFEEN", "FEN","FEEN"}
for i, v in ipairs (wordlist) do
if (string.match(processstring(instr), v) ~= nil)then
return 1
end
end
--if (string.match(instr,specword) ~= nil)then
-- return 1
--end
end
print (checkword("mÜ⌠⌠í∩"))
As of now, I have found no way to proof strings like that. Not even by using string.byte() to reduce it to ASCII have I been able to reliably work with exoctic characters like those. Even more weird is that if I do a print(bc) on processstring I get the folowing output
160 226 140 160 195 173 226 136 169
Now, that's 9 ASCII codes for a 6 letter word, how can this be? I built the code referencing http://www.asciitable.com/, is it wrong? How can I approach this processing?
local subst = {
U = "üûùÜú",
N = "ñÑπⁿ∩",
O = "ôöòÖóºσΘΩ°",
I = "ïîìí¡│",
F = "⌠",
A = "âäàåÄÅæÆáª╡╢╖╞╟α",
E = "éëèÉΣ",
}
local subst_utf8 = {}
for base_letter, list_of_letters in pairs(subst) do
for utf8letter in list_of_letters:gmatch'[%z\1-\x7F\xC0-\xFF][\x80-\xBF]*' do
subst_utf8[utf8letter] = base_letter
end
end
function processstring(instr)
return (instr:upper():gsub('[%z\1-\x7F\xC0-\xFF][\x80-\xBF]*', subst_utf8))
end
print(processstring("mÜ⌠⌠í∩")) --> MUFFIN

Lua split strings into Keys and Values of a table

So I want to split two strings, and be able to return a table with one string equaling the Keys and another the Values.
So if:
String1 = "Key1,Key2,Key3,Key4,Key Ect..."
String2 = "Value1,Value2,Value3,Value4,Value Ect..."
The output would be a table as folows:
Key1 - Value1
Key2 - Value2
Key3 - Value3
Key4 - Value4
Key Ect... - Value Ect...
I have been looking at this split function I found on the Lua wiki
split(String2, ",")
function split(String, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
But of course this only returns:
1 - Value1
2 - Value2
and so on...
I'm going to start trying to modify this code, but I don't know how far I'll get.
You can use it directly like this:
local t1 = split(String1, ",")
local t2 = split(String2, ",")
local result = {}
for k, v in ipairs(t1) do
result[v] = t2[k]
end
Or, create your own iterator:
local function my_iter(t1, t2)
local i = 0
return function() i = i + 1; return t1[i], t2[i] end
end
local result = {}
for v1, v2 in my_iter(t1, t2) do
result[v1] = v2
end
The code below avoids creating two temporary tables:
function join(s1,s2)
local b1,e1,k=1
local b2,e2,v=1
local t={}
while true do
b1,e1,k=s1:find("([^,]+)",b1)
if b1==nil then break end
b1=e1+1
b2,e2,v=s2:find("([^,]+)",b2)
if b2==nil then break end
b2=e2+1
t[k]=v
end
return t
end
String1 = "Key1,Key2,Key3,Key4"
String2 = "Value1,Value2,Value3,Value4"
for k,v in pairs(join(String1,String2)) do
print(k,v)
end

Parsing years from strings

I have 12-13 strings like
FVS1012 09 GO5
OCGG 85 0827KU9
where depending on length I have to get the bold part which means the year, in the example it would be 2009 and 1985.
How to get the "year"?
Dim str As String
Dim ResultStr As String
str = "FVS101209GO5"
Dim number As Integer = str.Length()
Select Case number
Case 12
ResultStr = str.Substring(8, 12)
ResultStr = ResultStr.Substring(1, ResultStr.Length() )
Case 13
ResultStr = str.Substring(5, 13)
ResultStr = ResultStr.Substring(1, ResultStr.Length() )
Case Else
Debug.WriteLine("other")
End Select
Is there any other better approach?
You could probably do it as.........
Select Case number
Case 12
ResultStr = mid(str, 8 ,2)
Case 13
ResultStr = mid(str, 5, 2)
Case Else
Debug.WriteLine("other")
End Select
Note that it only save one line per case statement so wether it is worth doing or not? i'm not sure

Assigning value to a cell in a function

I am writing a code where one of the lines( Range("H5").value = 10 ) should assign a specific value to a specific cell. However it gives me an #VALUE! error on my cell. Here is my code please help!!:
Function TotalAdUnits(Money As Currency, CycleEarning As Integer) As Integer
TapCost = 16
TotalAdUnits = 0
AdUnit = 0
i = 1
Money = Money + CycleEarning
Do While (Money >= TapCost)
Money = Money - TapCost
TotalAdUnits = TotalAdUnits + Range("L1").Offset(i, 0)
TapCost = Range("J2").Offset(i, 0)
Range("H5").value = 10
i = i + 1
Loop
End Function
You cannot set a cells value from a UDF - a function used in this way can only return a value to the calling cell (or range, if it's used as an array function). Edit your function to return the value, instead of trying to set it directly.
Having said that, I'm not clear on exactly what you're trying to do with that function. Why set the same value in a loop?
Also, if you reference ranges directly from your function, and those ranges are not passed as parameters, you need to include
Application.Volatile
in your function so Excel knows to recalculate it any time the sheet changes.
Function TotalAdUnits(Money As Currency, CycleEarning As Integer) As Integer
TapCost = 16
TotalAdUnits = 0
AdUnit = 0
i = 1
Money = Money + CycleEarning
Do While (Money >= TapCost)
Money = Money - TapCost
TotalAdUnits = TotalAdUnits + Range("L1").Offset(i, 0)
TapCost = Range("J2").Offset(i, 0)
Range("H5").value = 10
i = i + 1
Loop
'need to return a value...
TotalAdUnits = 10 '?
End Function

Resources