Predict The Output III

How many did you get right?

  • Zero

    Votes: 6 37.5%
  • 1

    Votes: 2 12.5%
  • 2

    Votes: 6 37.5%
  • 3

    Votes: 2 12.5%

  • Total voters
    16

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Great explanation. I am left with one question - why is this even possible in VBA? Under what circumstances would someone want to do this?

I suppose the most literal minded way of answering that question would be "because it's possible in VB6 which VBA is based on". As for why would it be possible at all, it's a way to store multiple pieces of information in one simple little number. A long integer is 32 bits. That means you could define 32 Boolean values in one field and store them in a single Long instead of 32 Boolean (true/false) fields. If one were trying to save on storage, or minimize the number of parameters being passed around, this would be pretty handy. A trivial example follows:

Code:
Option Explicit

Public Enum eEmployeeData
    'Shifts:
    eFirstShift = 1
    eSecondShift = 2
    eThirdShift = 4
    eSwingShift = 8
    'Pay Type:
    eSalary = 16
    eFullTimeHourly = 32
    ePartTimeHourly = 64
    'Is Contractor?:
    eContractor = 128
    'Status:
    eRetired = 256
    eFired = 512
    eQuit = 1024
    eLaidOff = 2048
    eActive = 4096
    [_max] = 4096
End Enum

Public Type EmployeeInfo
    Name As String
    Data As eEmployeeData
End Type

Sub Example()
    Dim employee As EmployeeInfo
    employee.Name = "John Q. Public"
    employee.Data = eActive + eContractor + eFirstShift + eFullTimeHourly
    MsgBox "So from this one little number """ & employee.Data & """, we know all of this about " & employee.Name & ":" & DataToString(employee.Data)
End Sub

Private Function DataToString(ByVal dataValue As eEmployeeData) As String
    Dim strRtnVal As String
    Dim lngExp As Long
    For lngExp = 0 To Log2(eEmployeeData.[_max])
        Select Case dataValue And (2 ^ lngExp)
        Case eFirstShift:       strRtnVal = strRtnVal & vbCr & "eFirstShift"
        Case eSecondShift:      strRtnVal = strRtnVal & vbCr & "eSecondShift"
        Case eThirdShift:       strRtnVal = strRtnVal & vbCr & "eThirdShift"
        Case eSwingShift:       strRtnVal = strRtnVal & vbCr & "eSwingShift"
        Case eSalary:           strRtnVal = strRtnVal & vbCr & "eSalary"
        Case eFullTimeHourly:   strRtnVal = strRtnVal & vbCr & "eFullTimeHourly"
        Case ePartTimeHourly:   strRtnVal = strRtnVal & vbCr & "ePartTimeHourly"
        Case eContractor:       strRtnVal = strRtnVal & vbCr & "eContractor"
        Case eRetired:          strRtnVal = strRtnVal & vbCr & "eRetired"
        Case eFired:            strRtnVal = strRtnVal & vbCr & "eFired"
        Case eQuit:             strRtnVal = strRtnVal & vbCr & "eQuit"
        Case eLaidOff:          strRtnVal = strRtnVal & vbCr & "eLaidOff"
        Case eActive:           strRtnVal = strRtnVal & vbCr & "eActive"
        End Select
    Next
    DataToString = strRtnVal
End Function

Private Function Log2(ByVal value As Long) As Long
    Const dblLog2_c As Double = 0.693147180559945
    Log2 = Log(value) / dblLog2_c
End Function
 

Forum statistics

Threads
1,222,646
Messages
6,167,311
Members
452,109
Latest member
nathanle89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top