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

With the following macro if a cell has a value of 5 in it, the macro
identifies it as a double, I would have expected it to come back as an
integer. Can you tell me what I'm doing incorrectly?

tia

Sub DisplayCellDataAttributes()

If Not RangeTools.IsRangeValid Then Exit Sub

Application.ScreenUpdating = False

For Each rngCell In Selection

If (VarType(rngCell.Cells) = vbInteger) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format - Integer" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf & vbCrLf &
"VarType: " & VarType(rngCell.Cells)
If (VarType(rngCell.Cells) = vbLong) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format - Long" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf & vbCrLf &
"VarType: " & VarType(rngCell.Cells)
If (VarType(rngCell.Cells) = vbDouble) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format - Double" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf & vbCrLf &
"VarType: " & VarType(rngCell.Cells)


Next

Application.ScreenUpdating = True

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default Question about VarType

Hi Bill,

You should use IF-ELSEIF-END. Please try the following one.

Sub DisplayCellDataAttributes()


Application.ScreenUpdating = False

For Each rngCell In Selection

If (VarType(rngCell.Cells) = vbInteger) Then
MsgBox rngCell.Address & vbCrLf & vbCrLf & "
Cell DATA Format - Integer" _
& vbCrLf & vbCrLf & "Data: " &
rngCell.Cells & vbCrLf & vbCrLf _
& "VarType: " & VarType(rngCell.Cells)

ElseIf (VarType(rngCell.Cells) = vbLong) Then
MsgBox rngCell.Address & vbCrLf & vbCrLf & "
Cell DATA Format - Long" _
& vbCrLf & vbCrLf & "Data: " &
rngCell.Cells & vbCrLf & vbCrLf _
& "VarType: " & VarType(rngCell.Cells)
ElseIf (VarType(rngCell.Cells) = vbDouble) Then
MsgBox rngCell.Address & vbCrLf & vbCrLf & "
Cell DATA Format - Double" _
& vbCrLf & vbCrLf & "Data: " &
rngCell.Cells & vbCrLf & vbCrLf _
& "VarType: " & VarType(rngCell.Cells)
End If

Next

Application.ScreenUpdating = True

End Sub


Best Regards

Bill

-----Original Message-----
With the following macro if a cell has a value of 5 in

it, the macro
identifies it as a double, I would have expected it to

come back as an
integer. Can you tell me what I'm doing incorrectly?

tia

Sub DisplayCellDataAttributes()

If Not RangeTools.IsRangeValid Then Exit Sub

Application.ScreenUpdating = False

For Each rngCell In Selection

If (VarType(rngCell.Cells) = vbInteger) Then

MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format -

Integer" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf &

vbCrLf &
"VarType: " & VarType(rngCell.Cells)
If (VarType(rngCell.Cells) = vbLong) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format -

Long" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf &

vbCrLf &
"VarType: " & VarType(rngCell.Cells)
If (VarType(rngCell.Cells) = vbDouble) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format -

Double" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf &

vbCrLf &
"VarType: " & VarType(rngCell.Cells)


Next

Application.ScreenUpdating = True

End Sub

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Question about VarType


Internally excels computes with something similar to the double data
type, which is how any spreadsheet calculates...

Sub test()
[a1] = CLng(1)
MsgBox TypeName([a1].Value)
End Sub

For numbers the Value property returns ... a Double (always!)

Step 1:
retrieve the cell.value into a Double variable.

Step2:
then test converting it to several other datatypes
if clng(x)=x then ''it maybe a long

Step3:
decide the most likely outcome and report it.
(which is what excel tries to do)


Sub test()
Dim res(1 To 5, 1 To 3) As Variant
Dim i%, j%, s$

On Error Resume Next
With ActiveSheet
For i = 1 To 5
.Cells(i) = Choose(i, 1, 1.1, "text", Date, Time)
res(i, 1) = .Cells(i).Value
res(i, 2) = TypeName(res(i, 1))
res(i, 3) = VarType(res(i, 1))
Next
End With

For i = 1 To 5
For j = 1 To 3
s = s & vbTab & res(i, j)
Next
s = s & vbNewLine
Next

MsgBox s

End Sub




keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


wrote:

With the following macro if a cell has a value of 5 in it, the macro
identifies it as a double, I would have expected it to come back as an
integer. Can you tell me what I'm doing incorrectly?

tia

Sub DisplayCellDataAttributes()

If Not RangeTools.IsRangeValid Then Exit Sub

Application.ScreenUpdating = False

For Each rngCell In Selection

If (VarType(rngCell.Cells) = vbInteger) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format - Integer" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf & vbCrLf &
"VarType: " & VarType(rngCell.Cells)
If (VarType(rngCell.Cells) = vbLong) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format - Long" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf & vbCrLf &
"VarType: " & VarType(rngCell.Cells)
If (VarType(rngCell.Cells) = vbDouble) Then MsgBox
rngCell.Address & vbCrLf & vbCrLf & " Cell DATA Format - Double" &
vbCrLf & vbCrLf & "Data: " & rngCell.Cells & vbCrLf & vbCrLf &
"VarType: " & VarType(rngCell.Cells)


Next

Application.ScreenUpdating = True

End Sub



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
Excel 2007 Macro/VB Question DDE Question MadDog22 Excel Worksheet Functions 1 March 10th 10 01:47 AM
How 2 Question 2Quick Excel Discussion (Misc queries) 2 April 22nd 09 01:10 AM
where can I see my question and answer? Yesterday I ask a question IP Excel Discussion (Misc queries) 2 May 10th 08 04:08 PM
Newbie Question - Subtraction Formula Question [email protected] Excel Discussion (Misc queries) 3 May 5th 06 05:50 PM
The question is an excel question that I need to figure out howto do in excel. Terry Excel Worksheet Functions 3 January 23rd 06 06:22 PM


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