0

Assign value from a cell to a variable

i wanna ask i was just try to assign a value from a single cell to a variable in an UDF the problem is, when i use the UDF, it results as #VALUE! the part of the code that produce the #VALUE! error are looks like this: public function test1 (rn as range) as string dim num_from_rn as integer num_from_rn = rn.value ' --> here is where the error occurr end function does anyone know why it resulting an error and how to fix it?

11th May 2024, 4:52 PM
Chipp
Chipp - avatar
3 Answers
+ 1
I read about VBA but not actually coded anything with it. However, it looks like you get a cell value wrongly. num_from_rn = rn.value rn doesn't seem pointing to a specific cell, therefore it cannot retrieve any value from it. I'm not sure it is correct, but you can try... dim v as integer v = range("A1").value
13th May 2024, 5:53 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
The issue you're encountering is likely due to trying to assign the value of a range directly to an integer variable. When you assign rn.value to num_from_rn, it's trying to assign an array of values from the range rn to an integer variable, which causes a type mismatch error.
13th May 2024, 2:33 PM
JaiprakashJH
JaiprakashJH - avatar
+ 1
Wong Hei Ming n JaiprakashJH thx for the reply.. i alrdy figured it out.. the cause is more likely to Jai's answer... it's about maximum value of the Integer data type... it's just around 32,000 while the value from the range (it's actually just a single cell) is 1,234,567 someone told me that, and now the error doesn't happen anymore coz i'm using LongLong data type... thx for the reply, once again...
13th May 2024, 2:50 PM
Chipp
Chipp - avatar