Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-05-2002, 03:26 PM   #1
Member (12 bit)
 
Paul Victorey's Avatar
 
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
I know enough programming in general to understand what it's doing just fine, but I lack the VB knowledge to create a new macro performing the inverse function. I know we have some VB gurus here, so I'm hoping someone can help me out.
__________________
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.
Paul Victorey is offline   Reply With Quote
Old 06-05-2002, 03:46 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
I would try something like this (I have done similar things with bit flags before, just nothing going the other way)

Quote:
For counter = 1 To 4
If (OutVal And (2 ^ (counter - 1))) = 2 ^ (counter - 1) Then
CurList.Selected(counter) = True
End If
Next
doctorgonzo is offline   Reply With Quote
Old 06-07-2002, 01:51 PM   #3
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
Thanks! A slight variation on that worked.
Paul Victorey is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:18 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2