Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Seems there is a bug with long integers in VBA

Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is he
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue)
End If
End Sub

Cheers,
Alex
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Seems there is a bug with long integers in VBA

On Tue, 8 May 2007 09:52:02 -0700, Alex wrote:

Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is he
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue)
End If
End Sub

Cheers,
Alex


I believe this occurs because hex numbers with four or fewer digits are
converted to signed 2-byte integers. When the signed integer intermediate value
is converted to a Long (or 4-byte) integer, the sign is propagated.


--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Seems there is a bug with long integers in VBA


&hFF processes correctly.

I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check
lvalue ?!

But I guess this question not for you. :)

Thank you.

"Ron Rosenfeld" wrote:

On Tue, 8 May 2007 09:52:02 -0700, Alex wrote:

Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is he
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue)
End If
End Sub

Cheers,
Alex


I believe this occurs because hex numbers with four or fewer digits are
converted to signed 2-byte integers. When the signed integer intermediate value
is converted to a Long (or 4-byte) integer, the sign is propagated.


--ron

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Seems there is a bug with long integers in VBA

But I guess this question not for you. :)

Why that - Ron gave you a good concise answer !! I merely followed to show
how to set the Hex to a Long.

As Ron said, 4 digit Hex values are Integers unless explicitly set to a Long
with the trailing &

Integers range from -32768 to +32767, so -
&H8000 + &H7FFF = &HFFFF = -1

If you assign &HFFFF to a Long variable the sign is propagated and coerced
into the new Long value of -
Debug.Print Hex(CLng(&HFFFF)) ' &HFFFFFFFF

Regards,
Peter T


"Alex" wrote in message
...

&hFF processes correctly.

I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't

check
lvalue ?!

But I guess this question not for you. :)

Thank you.

"Ron Rosenfeld" wrote:

On Tue, 8 May 2007 09:52:02 -0700, Alex

wrote:

Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is he
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," &

Hex(WrongValue)
End If
End Sub

Cheers,
Alex


I believe this occurs because hex numbers with four or fewer digits are
converted to signed 2-byte integers. When the signed integer

intermediate value
is converted to a Long (or 4-byte) integer, the sign is propagated.


--ron



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Seems there is a bug with long integers in VBA

On Tue, 8 May 2007 10:41:01 -0700, Alex wrote:

&hFF processes correctly.

I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check
lvalue ?!

But I guess this question not for you. :)

Thank you.


Your initial theory was that there is a "bug with long integers in VBA"

As I explained to you, the problem is not that, but rather the problem is your
understanding of how VBA treats HEX numbers, and also with data conversions.

For example, a "minor" change in your routine prevents the hexadecimal values
from being treated as signed two-bit integers:

========================
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF&
Dim lVal As Long

lVal = &HFFFF&

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue)
End If
End Sub
=============================

The immediate window shows:

lVal: 65535 FFFF
Equal






--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Seems there is a bug with long integers in VBA

Try this -

Debug.Print &HFFFF, &HFFFF& ' -1 65535

Regards,
Peter T

"Alex" wrote in message
...
Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is he
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," &

Hex(WrongValue)
End If
End Sub

Cheers,
Alex



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Seems there is a bug with long integers in VBA

Thank you!

"Peter T" wrote:

Try this -

Debug.Print &HFFFF, &HFFFF& ' -1 65535

Regards,
Peter T

"Alex" wrote in message
...
Hi everybody,

I tried assign values using hexidecimal notation in Excel 2003 SP2
and found that &hFFFF is not equal to &h10000 - 1 for LONG integers.
Sample is he
Sub test()
Const WorkaroundValue As Long = &H10000 - 1
Const WrongValue As Long = &HFFFF
Dim lVal As Long

lVal = &HFFFF

Debug.Print "lVal: " & lVal & " " & Hex(lVal)

If WorkaroundValue = WrongValue Then
Debug.Print "Equal"
Else
Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue
Debug.Print "In hex: " & Hex(WorkaroundValue) & "," &

Hex(WrongValue)
End If
End Sub

Cheers,
Alex




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
http://CannotDeleteFile.net - Cannot Delete File? Try Long Path ToolFilename is too long? Computer Complaining Your Filename Is Too Long? TheLong Path Tool Can Help While most people can go about their businessblissfully unaware of the Windo Max Loger Excel Discussion (Misc queries) 0 June 14th 11 04:30 PM
Long Long Long Nested If Function sed Excel Discussion (Misc queries) 4 December 9th 09 06:44 PM
Clearing cells takes long, long time unclemuffin Excel Discussion (Misc queries) 9 August 17th 07 02:22 AM
A list of Consecutive Integers, can I search for missing integers CM Excel Worksheet Functions 4 September 2nd 05 06:38 PM
converting to Integers bmordhorst Excel Worksheet Functions 5 January 6th 05 04:55 PM


All times are GMT +1. The time now is 01:21 PM.

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"