|
-
May 30th, 2022, 03:50 AM
#1
Thread Starter
Junior Member
Excel visual basic form, overflow issue
I'm working on a custom form that contains 32 checkboxes, I'm using a double type variable to store the checked flags via bit shifting.
The problem is that the max value of this variable can be 4294967295, and when the last checkbox is checked, I get an error that says "overflow".
What type of variables should I use to store the value 4294967295?
This is the code I'm using:
Code:
Private Sub CancelBttn_Click()
Unload Me
End Sub
Private Sub OKBttn_Click()
Cells.Item(ActiveCell.Row, ActiveCell.Column) = GetFlags()
Unload Me
End Sub
Private Sub UserForm_Activate()
'propogate the flag names into the check boxes
Dim i As Double
Dim flagCaption As String
For i = 0 To 31
flagCaption = Sheets("FlagNames").Cells.Item(i + 2, 2)
If (flagCaption = "") Then
flagCaption = "Flag #" + Str$(i) + " (Unused)"
End If
Call SetFlagCaption(i, flagCaption)
Next i
SetFlags (Cells.Item(ActiveCell.Row, ActiveCell.Column))
End Sub
Public Sub SetFlagCaption(ByVal position As Double, ByVal flagCaption As String)
'Iterate over all controls
For Each oCtrl In Me.Controls
If TypeName(oCtrl) = "CheckBox" Then
If oCtrl.Name = "FlagCheckBoxCtrl" & position Then
oCtrl.Caption = flagCaption
Exit For
End If
End If
Next
End Sub
Public Sub SetFlags(ByVal flags As Double)
For index = 0 To 31
If shr(flags, index) And 1 Then
For Each oCtrl In Me.Controls
If TypeName(oCtrl) = "CheckBox" Then
If StrComp(oCtrl.Name, "FlagCheckBoxCtrl" & index) = 0 Then
oCtrl.Value = 1
Exit For
End If
End If
Next
End If
Next
End Sub
Public Function GetFlags() As Double
Dim selectedFlags As Double
selectedFlags = 0
For index = 0 To 31
For Each oCtrl In Me.Controls
If TypeName(oCtrl) = "CheckBox" Then
If StrComp(oCtrl.Name, "FlagCheckBoxCtrl" & index) = 0 Then
If oCtrl.Value = True Then
selectedFlags = selectedFlags Or shl(1, index)
End If
Exit For
End If
End If
Next
Next
GetFlags = selectedFlags
End Function
Private Function shr(ByVal Value As Double, ByVal Shift As Byte) As Double
shr = Value
If Shift > 0 Then
shr = Int(shr / (2 ^ Shift))
End If
End Function
Private Function shl(ByVal Value As Double, ByVal Shift As Byte) As Double
shl = Value
If Shift > 0 Then
shl = Value * (2 ^ Shift)
End If
End Function
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|