Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA = 0# ????

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB
Hi,
I'm new to VBA (Excel) and I've come across a piece of syntax that I don't understand and can find no information on.
The piece of code is:
issheetalreadyhere = 0#

To me this looks like it's setting the variable value. But I have no idea what the 0# bit is.

Can anyone advise?
Thanks,
Roy
 
Is this used inside an IF?

It looks to me like it is probably the name of a public function that is being called by the code you are reading. If you step through that part of the code (using [F8]), see if it takes you to the other module.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Roy,

If you set a variable equal to 0.0 in code then move your cursor off that line, the compiler converts the 0.0 to 0#, although I must admit I don't know why. It may have to do with the fact that the # symbol is used for the double data type and the compiler stores all real number values internally as doubles.


Regards,
Mike
 
Hi John,
Yes it is in a function (shown below) and is called from a sub procedure using this code:
ans = issheetalreadyhere("Control") 'true or false
What I need to know is what does the 0# denote?
Thanks for your help,
Roy

Function issheetalreadyhere(Control As String) As Boolean
Dim found As Boolean
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
issheetalreadyhere = 0#
found = False
For Each ws In wb.Worksheets
If ws.Name = "Control" Then
found = True
End If
Next ws
issheetalreadyhere = found
End Function
 
0# forces to treat '0' as double. 0# assigned to Boolean variable is converted to False (same as for instance 0& - '0' as Long).

combo
 
Thanks combo and Mike,
That's answered my question. But what is the difference between typing:
issheetalreadyhere = 0#
and typing
issheetalreadyhere = False
Or isn't there a difference?
Thanks again,
Roy
 
False is an intrinsic integer constant with value = zero.

Anyway, this function may be simplified:
Function isSheetAlreadyHere(strControl As String) As Boolean
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = strControl Then
isSheetAlreadyHere = True
Exit Function
End If
Next ws
End Function

Or even shorter:
Function isSheetAlreadyHere(strControl As String) As Boolean
On Error Resume Next
isSheetAlreadyHere =(ActiveWorkbook.Worksheets(strControl).Name = strControl)
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
They are the same characters that were used in old Basic to define variable types before the "As" capability was introduced (they are still available for backwards compatibility). Here are some of the more obscure number manipulators in VB:-

Code:
Dim i%                ' signed integer
Dim j#                ' signed double
Dim k&                ' signed long
Dim a$                ' string
Dim b@                ' currency

# forces any number to double (0.0)
! forces any number to single (0.0)
& forces any number to Long (0)
% forces any number to Integer (0)
$ for String
@ for Currency

&H Hexadecimal (&H7777AA)
&O Octal (&O01234567)
^ Exponential (2^32)
\ Integer divide operator (forces result to integer)

Using these operators can be more efficient than the more 'human' methods, for example:-

Code:
Dim varX
If Len(Text) = 0 Then
    varX = 0#

is more efficient than

Code:
Dim varX as Variant
If Text = "" Then
    varX = 0.0



This is a good article to help clear up some of the "forgotten" aspects of Basic.

J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top