Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Type change in a declared Variable?
At a guess, do you have declaration like :
Dim lCreditSum, MySingleVar as single If so, this ONLY declares "MySingleVar" as a single ; "lCreditSum" is the default for undeclared variables - Variant. You need to explicitly state the datatype of each variable. Dim lCreditSum As Single, MySingleVar as Single NickHK <RjS; <CISSP; "CISA" wrote in message ... Hi - I have a VB program I wrote for excell some months ago. It has been processing a bank report every day without a hitch for all that time. Today it produced an entirely different and erroneous result. I traced the error to the following statement: lCreditSum = lCreditSum + wsSRC.Cells(i, 13).Value Where lCreditSum is previously declared as a type Single. The SRC value is formatted as a number with 2 decimal points. When I set a watch on the lCreditSum variable and process a previous input report, its type assignment remains as type Single throughout the entire run, but when it processes todays input report the type assignment changes to a "Variant/String" on the very first assignment. It then proceeds to append each new value to the variable instead of adding the new values to the sum as its supposed to. Please, anybody... any suggestions? Absolutely nothing has changed with this program for several months and I have examined in input reports in minute detail and can detect no changes. Thanks much, |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Type change in a declared Variable?
What is current value of wsSRC.Cells(i, 13) that is being treated
differently ? As for variable naming, you are referring "Hungarian Notation". http://en.wikipedia.org/wiki/Hungarian_notation It is a personal/work environment issue. Here's the MS convention : http://support.microsoft.com/kb/110264 NickHK <RjS; <CISSP; "CISA" wrote in message ... Thanks much for all the thoughtful replies. The conversion using cSng solves the problem. In addition, the original Dim line read: Dim HighPptyRow, lCreditSum, lDebitSum As Single which, according to NickHK results in only the last value being declared as Single and the remainder being declared as Variable. I was unaware of this and it is certainly non-intuitive. So, declaring each variable as Single also solves the problem. I am still confused as to why, after months of working fine, the program suddenly decides that an input value, formatted as Number, is now a Type "Variant/String"? Lastly, I am aware that the beginning character of each variable has implications as to how that variable is or should be treated but I have never ran across a table of those values. Does anyone know a refrence to this info? Thanks again for all the great replies. They were all helpful, informative, and valuable and I learned a lot from them. "NickHK" wrote: At a guess, do you have declaration like : Dim lCreditSum, MySingleVar as single If so, this ONLY declares "MySingleVar" as a single ; "lCreditSum" is the default for undeclared variables - Variant. You need to explicitly state the datatype of each variable. Dim lCreditSum As Single, MySingleVar as Single NickHK <RjS; <CISSP; "CISA" wrote in message ... Hi - I have a VB program I wrote for excell some months ago. It has been processing a bank report every day without a hitch for all that time. Today it produced an entirely different and erroneous result. I traced the error to the following statement: lCreditSum = lCreditSum + wsSRC.Cells(i, 13).Value Where lCreditSum is previously declared as a type Single. The SRC value is formatted as a number with 2 decimal points. When I set a watch on the lCreditSum variable and process a previous input report, its type assignment remains as type Single throughout the entire run, but when it processes todays input report the type assignment changes to a "Variant/String" on the very first assignment. It then proceeds to append each new value to the variable instead of adding the new values to the sum as its supposed to. Please, anybody... any suggestions? Absolutely nothing has changed with this program for several months and I have examined in input reports in minute detail and can detect no changes. Thanks much, |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Reference a variable declared in VBA | Excel Worksheet Functions | |||
Type change in a declared Variable? | Excel Programming | |||
Type change in a declared Variable? | Excel Programming | |||
Type change in a declared Variable? | Excel Programming | |||
Get an object's type if it's declared as Variant | Excel Programming |