Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Strange values


Visual Basic 6.3
Excel 2003

I created a form that has some text boxes in which a user inputs
numbers.
Sounds simple to me.

The name of the form is frmEasyLyteQC
The name of one textbox is txtMinNa
The name of second textbox is txtMaxNa

User opens the form and inputs 135 in first box and 145 in second box,
then clicks OK.

I declared the following variables before showing the form:


Code:
--------------------
Public NaMin As Variant
Public NaMax As Variant
Public MedianNa As Variant

Sub QCTrack()

frmEasyLyteQC.txtAnalDate.SetFocus
frmEasyLyteQC.Show

End Sub
--------------------


Here is the section of the program where those values are seen:


Code:
--------------------
NaMin = frmEasyLyteQC.txtMinNa.Value
NaMax = frmEasyLyteQC.txtMaxNa.Value
MedianNa = (NaMin + NaMax)/2
--------------------


When I step through the program and place cursor over NaMin the value
displayed is "135" and the value for NaMax is "145"
When I cursor over to MedianNa the value is "67572.5"

I've written many other programs before with similar code using
different variables, but can't figure out why the calculation is
strange. Obviously Median Na sholud be 140.

Any clues?


--
scantor145
------------------------------------------------------------------------
scantor145's Profile: http://www.excelforum.com/member.php...o&userid=14766
View this thread: http://www.excelforum.com/showthread...hreadid=532674

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Strange values

Not sure, but maybe it helps to declare the variables as
Long or Double and doing instead of:

frmEasyLyteQC.txtMinNa.Value

Val(Trim(frmEasyLyteQC.txtMinNa.Text))

RBS


"scantor145" wrote
in message ...

Visual Basic 6.3
Excel 2003

I created a form that has some text boxes in which a user inputs
numbers.
Sounds simple to me.

The name of the form is frmEasyLyteQC
The name of one textbox is txtMinNa
The name of second textbox is txtMaxNa

User opens the form and inputs 135 in first box and 145 in second box,
then clicks OK.

I declared the following variables before showing the form:


Code:
--------------------
Public NaMin As Variant
Public NaMax As Variant
Public MedianNa As Variant

Sub QCTrack()

frmEasyLyteQC.txtAnalDate.SetFocus
frmEasyLyteQC.Show

End Sub
--------------------


Here is the section of the program where those values are seen:


Code:
--------------------
NaMin = frmEasyLyteQC.txtMinNa.Value
NaMax = frmEasyLyteQC.txtMaxNa.Value
MedianNa = (NaMin + NaMax)/2
--------------------


When I step through the program and place cursor over NaMin the value
displayed is "135" and the value for NaMax is "145"
When I cursor over to MedianNa the value is "67572.5"

I've written many other programs before with similar code using
different variables, but can't figure out why the calculation is
strange. Obviously Median Na sholud be 140.

Any clues?


--
scantor145
------------------------------------------------------------------------
scantor145's Profile:
http://www.excelforum.com/member.php...o&userid=14766
View this thread: http://www.excelforum.com/showthread...hreadid=532674


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Strange values

Textboxes return string values and because you use Variants they will
interpret it as String unless you specify otherwise. Adding them (as string
values) concatenates them, so "135"+"145" = "135145." When you get to the
division, VBA can no longer interpret it as a string and so now it "forces"
the conversion to number and you get the result 135145/2=67572.5

When using variants you have to consider carefully how VBA will interpret
the contents. It is better to explicitly convert them either by storing in a
properly typed variable or using the conversion functions (e.g. CInt, Val,
etc.)
--
- K Dales


"scantor145" wrote:


Visual Basic 6.3
Excel 2003

I created a form that has some text boxes in which a user inputs
numbers.
Sounds simple to me.

The name of the form is frmEasyLyteQC
The name of one textbox is txtMinNa
The name of second textbox is txtMaxNa

User opens the form and inputs 135 in first box and 145 in second box,
then clicks OK.

I declared the following variables before showing the form:


Code:
--------------------
Public NaMin As Variant
Public NaMax As Variant
Public MedianNa As Variant

Sub QCTrack()

frmEasyLyteQC.txtAnalDate.SetFocus
frmEasyLyteQC.Show

End Sub
--------------------


Here is the section of the program where those values are seen:


Code:
--------------------
NaMin = frmEasyLyteQC.txtMinNa.Value
NaMax = frmEasyLyteQC.txtMaxNa.Value
MedianNa = (NaMin + NaMax)/2
--------------------


When I step through the program and place cursor over NaMin the value
displayed is "135" and the value for NaMax is "145"
When I cursor over to MedianNa the value is "67572.5"

I've written many other programs before with similar code using
different variables, but can't figure out why the calculation is
strange. Obviously Median Na sholud be 140.

Any clues?


--
scantor145
------------------------------------------------------------------------
scantor145's Profile: http://www.excelforum.com/member.php...o&userid=14766
View this thread: http://www.excelforum.com/showthread...hreadid=532674


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very-2 strange Amiit Mangla Excel Discussion (Misc queries) 4 December 21st 05 12:26 PM
Something Strange ame9 Setting up and Configuration of Excel 2 July 5th 05 10:31 PM
Strange Values Ashman Excel Programming 5 May 13th 05 12:14 AM
Strange behavior with text box values . . . help! WillRn Excel Programming 1 January 24th 05 10:14 PM
Strange behavior Chart.SeriesCollection(n).Values property Emile van Mierlo Excel Programming 3 June 19th 04 06:07 PM


All times are GMT +1. The time now is 11:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"