LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
k k is offline
external usenet poster
 
Posts: 8
Default Declaring As Variant, Changing to Double?

I'm working on a macro where an Application.Inputbox is used to have the
user enter a numerical value. When this variable is declared as Double,
everything works as expected. However, for the error-checking code for the
InputBox, it appears I need to use Variant as the value of this variable (if
the input is not entered properly) will not be a numerical value in all
cases. Multiple tests with varused as Variant return an incorrect
calculation.

Now, my question is as follows: is there a way to enter varused as a
Variant, but to convert it to a Double after I performed my error-check for
the Application.InputBox? I've included what I believe is all applicable
code, in the hopes it better illustrates my problem. Any insight into why
declaring varused As Variant causes my code to malfunction would be an added
bonues. ;-)

Thanks in advance for any assistance.

k

--

Dim LoadPerString As Double

varused = Application.InputBox("Enter the value.")

--

I was looking into some code to protect the macro in case the user entered
nothing & clicked OK or clicked Cancel when the InputBox appeared. The code
I'd like to use is as follows.

--

Dim varused As Variant
If varused = False Then
MsgBox "You clicked cancel."
Sheets("MainPage").Activate
Application.ScreenUpdating = True
Exit Sub
ElseIf varused = "" Then
MsgBox "You didn't enter anything."
Sheets("MainPage").Activate
Application.ScreenUpdating = True
Exit Sub
Else
MsgBox "You entered: " & varused
End If

--

I got this code from the following post to this group.

http://groups.google.com/groups?hl=e...sftngp05#link7


The calculations I am performing (not surprisingly, they originated in this
group too) are as follows.

--

Dim hi As Long, lo As Long
Dim XRates As Range, YRates As Range
Dim CountCells As Long

' combining ranges for interpolate function
Set XRates = Range(startxrange & ":" & endxrange)
Set YRates = Range(startyrange & ":" & endyrange)
CountCells = XRates.Cells.Count

If CountCells < YRates.Count Then
MsgBox "The ranges need to be the same size." & vbCrLf & "This program
will self-destruct."
Exit Sub
End If

For hi = 1 To CountCells
If XRates(hi) < LoadPerString Then Exit For
Next
If hi CountCells Then
DesignTime = YRates(CountCells)
' copying design time to MainPage
Worksheets("MainPage").Range("C19").FormulaR1C1 = DesignTime
Sheets("MainPage").Activate
Application.ScreenUpdating = True
Exit Sub
End If
If hi = 1 Then
DesignTime = YRates(hi)
' copying design time to MainPage
Worksheets("MainPage").Range("C19").FormulaR1C1 = DesignTime
Sheets("MainPage").Activate
Application.ScreenUpdating = True
Exit Sub
End If
lo = hi - 1
DesignTime = YRates(lo) + (varused - XRates(lo)) / _
(XRates(hi) - XRates(lo)) * _
(YRates(hi) - YRates(lo))
' copying design time to MainPage
Worksheets("MainPage").Range("C19").FormulaR1C1 = DesignTime

Sheets("MainPage").Activate
Application.ScreenUpdating = True

--



 
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
Excel 2002 and VB declaring number as double Gary Excel Discussion (Misc queries) 2 March 5th 09 03:43 AM
How do I assign range to variant and use Mike H[_2_] Excel Discussion (Misc queries) 7 June 7th 07 01:40 AM
Testing if Variant is Range or Double? Don Wiss Excel Programming 3 January 26th 04 12:35 AM
Variant to String Chip Pearson Excel Programming 1 September 3rd 03 03:10 PM
DLLs and VBA: Who free's a variant? Keith Willshaw Excel Programming 0 August 6th 03 09:42 AM


All times are GMT +1. The time now is 01:52 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"