How to transfer data from a listbox to the sheet?

Solved
SOMUM Posted messages 28 Status Member -  
SOMUM Posted messages 28 Status Member -
Hello,

As part of my work, for the creation of an automated "thing" :), I came up with this. The values entered in the textboxes transfer well to the sheet, but I can't manage to transfer the selected data from the listbox (Accepted or Refused).

Private Sub CommandButtonOK_Click()
Range("V13") = TextBox9.Value
Range("N23") = TextBox2.Value
Range("N24") = TextBox3.Value
Range("X27") = TextBox4.Value
Range("X28") = TextBox5.Value
Range("X29") = TextBox6.Value
Range("N32") = TextBox7.Value
Range("X32") = TextBox8.Value
Unload Me
End Sub

Private Sub UserForm_Initialize()
ListBox1.List() = Range("BU20:BU22").Value
End Sub

Private Sub UserForm_Activate()
With Me
.StartUpPosition = 1
.Left = 100
.Top = 50
End With
End Sub


Is there a kind soul who can help me? I've been working on this since yesterday and I'm pulling my hair out even though I don't have much left. I've searched Google (who is my friend), but it let me down :'(

Thank you in advance.

Configuration: Windows 7 / Chrome 49.0.2623.110 / Excel 2010

1 answer

  1. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773
     
    Hello,

    Something like this:
    If listBox1.listIndex = -1 Then 'If no line is selected, the macro would return an error HERE Else Range("A1") = ListBox1.List(ListBox1.ListIndex) End If


    So, in a shorter version:
    If listBox1.listIndex > -1 Then Range("A1") = ListBox1.List(ListBox1.ListIndex)


    Before, I never managed to finish my sentences... but now I
    0
    1. SOMUM Posted messages 28 Status Member
       
      Private Sub CommandButtonOK_Click()
      Range("V13") = TextBox9.Value
      Range("N23") = TextBox2.Value
      Range("N24") = TextBox3.Value
      Range("X27") = TextBox4.Value
      Range("X28") = TextBox5.Value
      Range("X29") = TextBox6.Value
      Range("N32") = TextBox7.Value
      Range("X32") = TextBox8.Value
      Unload Me
      End Sub


      Private Sub UserForm_Initialize()
      ListBox1.List() = Range("BU20:BU22").Value
      If ListBox1.ListIndex > -1 Then Range("H62") = ListBox1.List(ListBox1.ListIndex)
      End Sub

      Private Sub UserForm_Activate()
      With Me
      .StartUpPosition = 1
      .Left = 100
      .Top = 50
      End With
      End Sub


      Yes, that’s it?
      0
      1. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773 > SOMUM Posted messages 28 Status Member
         
        Well no, not at the initialization of the userform come on...

        You said:
        The values entered in the textboxes transfer correctly to the sheet, but there’s no way to transfer the selected data from the listbox

        What procedure are you using for the values entered in your textboxes to transfer to the sheet?
        CommandButtonOK_Click

        Okay?
        So it’s in the code of your button:
        Private Sub CommandButtonOK_Click() Range("V13") = TextBox9.Value Range("N23") = TextBox2.Value Range("N24") = TextBox3.Value Range("X27") = TextBox4.Value Range("X28") = TextBox5.Value Range("X29") = TextBox6.Value Range("N32") = TextBox7.Value Range("X32") = TextBox8.Value If ListBox1.ListIndex > -1 Then Range("H62") = ListBox1.List(ListBox1.ListIndex) Unload Me End Sub
        0
    2. SOMUM Posted messages 28 Status Member
       
      You deserve a kiss :)

      I want to clarify that I am totally a newbie at Excel. But in the end, I managed (and the forum users too) to create something really well done :)
      0
      1. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773 > SOMUM Posted messages 28 Status Member
         
        Good for you.
        See you later.
        0
    3. SOMUM Posted messages 28 Status Member
       
      Thank you very much!
      0