|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
Probably a trivially easy VB problem
I know next to no VB, and I haven't really had the time to learn how it works. My problem is, I need to modify some VB code, and don't know the language well enough to do this.
Here's the code that I need to modify, it takes the items of a listbox in an Excel dialog window that were selected (there are 4 entries, A through D), and it puts them into a 4-bit integer which represents which ones were selected. The integer would have a 1 in the MSB if "D" was selected, and so forth (so if ABC were selected, it would be 0111 = 0x7, if BD selected, it would be 1010 = 0xA). This works (I'll copy the code that is used below). It then puts this number into a cell. What I need is an inverse to this process -- a macro that takes the decimal value of the cell, and selects the appropriate list items in the dialog. So if 0xF was in the cell, all 4 entries would be selected, and so forth. Here's the macro MultiSelect(), which creates the numeric representation: Code:
Sub MultiSelect()
Dim CurList As Object
Dim ListTemp As Variant
Dim ListItem As Variant
Dim MultiList As ListBox
Dim counter As Integer
'If an error occurs, go to error handler.
On Error GoTo ErrorHandler
'Set an object name for easy referencing of the list box.
Set CurList = DialogSheets("Dialog1").ListBoxes(1)
'Put the Selected array of items into the variable LTemp.
ListTemp = CurList.Selected
'Initialize a counter variable.
counter = 1
OutVal = 0
'Iterate through the loop once for each item in the array.
For Each ListItem In ListTemp
If ListItem = True Then
OutVal = OutVal + 2 ^ (counter - 1)
End If
counter = counter + 1
Next
Worksheets("Dialog Temp").Cells(3, 5).Value = OutVal
'Keeps macro from executing Error Handler.
Exit Sub
'Error handler, displays a message to the user.
ErrorHandler:
MsgBox "Macro Error in MultiSelect()", , "Error"
End Sub
__________________
Paul M. Victorey ------------------ I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk. |
|
|
|
|
|
#2 | |
|
Professional gadfly
|
I would try something like this (I have done similar things with bit flags before, just nothing going the other way)
Quote:
|
|
|
|
|
|
|
#3 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
Thanks! A slight variation on that worked.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|