Open Side Menu Go to the Top

04-11-2013 , 05:38 AM
I'm doing a small project with Visual Basic, and I can't quite figure out how to do a couple things.

I've made a calculator. You pick one item from a listbox, and it's supposed to pick the value from certain cells based on your selection.

Quote:
Private Sub UserForm_Activate()
Me.ListBox1.AddItem "Atria Oyj"
Me.ListBox1.AddItem "Finnair"
Me.ListBox1.AddItem "Neste Oil Oyj"
Me.ListBox1.AddItem "Nokia"
Me.ListBox1.AddItem "Sponda"
Me.ListBox1.AddItem "UPM-Kymmene"
Dim Hinta As Double
If "Atria Oyj" Then Hinta = Range("N2").Value
If "Finnair" Then Hinta = Range("N3").Value
If "Neste Oil Oyj" Then Hinta = Range("N4").Value
If "Nokia" Then Hinta = Range("N5").Value
If "Sponda" Then Hinta = Range("N6").Value
If "UPM-Kymmene" Then Hinta = Range("N7").Value
End Sub
Does this look anywhere close to correct. How do I code this properly?
Help needed for simple Visual Basic Quote
Help needed for simple Visual Basic
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Help needed for simple Visual Basic
04-11-2013 , 10:10 AM
I'm posting from my phone so can't see entire lines. I don't have a lot of experience with Visual Basic but I think you're filling the list right. You can read the list box selection but you have to activate reading the selection. A push button would work. You have to initialize the list box properties too but you don't have to do that at run time. You have a ways to go.
Help needed for simple Visual Basic Quote
04-11-2013 , 10:46 AM
Activate is the wrong event. I expect an activate event every time you click back to the form. Do the adding in a one time event like load, or at design time.
Help needed for simple Visual Basic Quote
04-11-2013 , 10:48 AM
Your if statements are wrong. You need if somevar = "Nokia" then ...
Help needed for simple Visual Basic Quote
04-11-2013 , 11:44 AM
The listbox comes out fine. Need to figure out how to code the if part right. adding somevar gave me some sort of error.
Help needed for simple Visual Basic Quote
04-11-2013 , 11:55 AM
somevar is a placeholder. like "x" in math.

You want to type "listbox1." and look at the choices the autocomplete offers.
Help needed for simple Visual Basic Quote
04-11-2013 , 02:26 PM
You have to know what item is selected in the list box. You can do this two different ways. One on a double click within the list box and/or some other event you capture usually a button click. You also probably want to configure the list box properties for single line selection.
Help needed for simple Visual Basic Quote
04-11-2013 , 03:53 PM
Is this visual basic or visual basic for applications (like in excel)?

Generate the listbox using your existing listbox.additem code, but do it on application load instead of onactivate. Make sure you have it set for single or multi selection, based on what you need

Add code to the listbox OnSelectedIndexChanged event. You need to determine if certain values are selected, then take the appropriate action.
Help needed for simple Visual Basic Quote
04-11-2013 , 03:57 PM
Yeah, this is VBA in Excel. Your able to select one item from the listbox right now, and that's what I want.
Help needed for simple Visual Basic Quote
04-11-2013 , 04:53 PM
There's a few problems as others said. If the listbox displays properly then let's just set that aside.

1. You need some way to wait until the user actually picks a value. Just putting the listbox up and immediately checking what is selected doesn't give the user any time to do anything. So as Freakin says, you need to listen for an event that tells you the user actually selected something before you go acting on what is selected.

2. You need to check against the value in a variable. I haven't used VB in a long time, but it'll be something like
if Me.ListBox1.SelectedItem = "Joe" then ...
Help needed for simple Visual Basic Quote
04-11-2013 , 09:36 PM
Code:
Private Sub UserForm_Activate()
    Me.ListBox1.AddItem "Atria Oyj"
    Me.ListBox1.AddItem "Finnair"
    Me.ListBox1.AddItem "Neste Oil Oyj"
    Me.ListBox1.AddItem "Nokia"
    Me.ListBox1.AddItem "Sponda"
    Me.ListBox1.AddItem "UPM-Kymmene"
End Sub

Private Sub ListBox1_Change()
    Dim Hinta As Double
    Select Case ListBox1.Value
        Case "Atria Oyj"
            Hinta = Range("N2").Value
        Case "Finnair"
            Hinta = Range("N3").Value
        Case "Neste Oil Oyj"
            Hinta = Range("N4").Value
        Case "Nokia"
            Hinta = Range("N5").Value
        Case "Sponda"
            Hinta = Range("N6").Value
        Case "UPM-Kymmene"
            Hinta = Range("N7").Value
    End Select
    MsgBox Hinta
End Sub
Help needed for simple Visual Basic Quote
04-12-2013 , 01:33 AM
Thanks. That seems to work nicely.
Help needed for simple Visual Basic Quote
04-12-2013 , 08:25 AM
No worries, feel free to post in the mega excel thread in OOT next time i'm sure you'll get a quicker response
Help needed for simple Visual Basic Quote
04-12-2013 , 08:42 AM
Oh, didn't know there's such a thing. If I get another problem, I'll post there.
Help needed for simple Visual Basic Quote
Help needed for simple Visual Basic
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Help needed for simple Visual Basic

      
m