Combobox selettiva



  • Combobox selettiva
    di Roby (utente non iscritto) data: 18/04/2014 08:50:11

    Buongiorno ho un problema nel estrapolare dei numeri da varie tabelle sul foglio excel mediante tre combobox, nel file di esempio non riesco a capire come mai in base alle scelte delle tre combobox il numero estratto è diverso da quello che si trova nelle tabelle sul foglio, e poi se scelgo l'altro range di ricerca il programma da errore.
    Dove sbaglio?
    grazie anticipatamente
     
    Dim Uriga1 As Long, R As Long, C As Long, X As Long, Riga As Object, Col As Object
    Private Sub UserForm_Activate()
    UserForm2.ComboBox1.RowSource = "RANGE"
    
    End Sub
    
    Private Sub ComboBox1_change()
    If UserForm2.ComboBox1.Text = "0,01-0,05" Or UserForm2.ComboBox1.Text = "0,06-0,10" Or UserForm2.ComboBox1.Text = "0,11-0,15" Or UserForm2.ComboBox1.Text = "0,16-0,20" Or UserForm2.ComboBox1.Text = "0,21-0,25" Then
            ComboBox2.RowSource = "COLORE1"
            ComboBox3.RowSource = "QUALITA1"
            Else
           '  ComboBox1.Text = "0,26-0,30" Or ComboBox1.Text = "0,31-0,36" And ComboBox2 <> "" And ComboBox3 <> ""
              ComboBox2.RowSource = "COLORE2"
              ComboBox3.RowSource = "QUALITA2"
    End If
    UserForm2.TextBox2.Text = ""
    If UserForm2.ComboBox1 <> "" And UserForm2.ComboBox2 <> "" And UserForm2.ComboBox3 <> "" Then
    Uriga = Range("A" & Rows.Count).End(xlUp).Row
    C = Rows("1:1" & Columns.Count).End(xlToRight).Column
    Set Col = Range(Cells(1, 1), Cells(1, C)).Find(UserForm2.ComboBox3.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Col Is Nothing Then
        C = Col.Column 'Row
    End If
    Set Riga = Range(Cells(1, 1), Cells(Uriga, 1)).Find(UserForm2.ComboBox1.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Riga Is Nothing Then
        R = Riga.Row
        For X = X + 1 To X + 30
        If Cells(R + X, 1) <> "" Then
            If Cells(R + X, 1) = UserForm2.ComboBox2.Text Then
                R = R + X
                Exit For
            End If
        End If
        Next X
    End If
    If UserForm2.TextBox1.Value = True Then
        UserForm2.TextBox2.Text = UserForm2.TextBox1.Value * Cells(R, C)
    End If
    End If
    End Sub
    
    Private Sub ComboBox2_Change()
            Call Change
    End Sub
    Private Sub ComboBox3_Change()
            Call Change
    End Sub
    Private Sub TextBox1_Change()
            Call Change
    End Sub
    
    Private Sub Change()
    UserForm2.TextBox2.Text = ""
    If UserForm2.ComboBox1 <> "" And UserForm2.ComboBox2 <> "" And UserForm2.ComboBox3 <> "" Then
    Uriga = Range("A" & Rows.Count).End(xlUp).Row
    C = Rows("1:1" & Columns.Count).End(xlToRight).Column
    Set Col = Range(Cells(1, 1), Cells(1, C)).Find(UserForm2.ComboBox3.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Col Is Nothing Then
        C = Col.Column 'Row
    End If
    Set Riga = Range(Cells(1, 1), Cells(Uriga, 1)).Find(UserForm2.ComboBox1.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Riga Is Nothing Then
        R = Riga.Row
        For X = X + 1 To X + 30
        If Cells(R + X, 1) <> "" Then
            If Cells(R + X, 1) = UserForm2.ComboBox2.Text Then
                R = R + X
                Exit For
            End If
        End If
        Next X
    End If
    If UserForm2.TextBox1.Value = True Then
        UserForm2.TextBox2.Text = UserForm2.TextBox1.Value * Cells(R, C)
    End If
    End If
    End Sub
    
    
    
    



  • di lepat (utente non iscritto) data: 18/04/2014 09:08:52

    devi spiegare come si usa, come funziona e come si fa a capire se il risultato è giusto o sbagliato



  • di Roby (utente non iscritto) data: 18/04/2014 09:28:03

    Ciao, il programma funziona così: selezionando la prima combobox scelgo il range di misura (la prima o la seconda ecc. tabella sul foglio) con la seconda combobox scelgo i dati che sono sulle rige della tabella e con la terza combobox le colonne della tabella selezionata; l'incrocio di queste scelte mi dovrebbe estrapolare il valore corrispondente situato nel foglio excel nella textbox nel form, che a sua volta verra moltiplicato, ma quello è di semplice esecuzione.
    spero di essere stato più chiaro, scusate.



  • di lepat (utente non iscritto) data: 18/04/2014 09:40:25

    prova così
     
    Dim Uriga1 As Long, R As Long, C As Long, X As Long, Riga As Object, Col As Object
    Private Sub UserForm_Activate()
    UserForm2.ComboBox1.RowSource = "RANGE"
    
    End Sub
    
    Private Sub ComboBox1_change()
    If UserForm2.ComboBox1.Text = "0,01-0,05" Or UserForm2.ComboBox1.Text = "0,06-0,10" Or UserForm2.ComboBox1.Text = "0,11-0,15" Or UserForm2.ComboBox1.Text = "0,16-0,20" Or UserForm2.ComboBox1.Text = "0,21-0,25" Then
            ComboBox2.RowSource = "COLORE1"
            ComboBox3.RowSource = "QUALITA1"
            Else
           '  ComboBox1.Text = "0,26-0,30" Or ComboBox1.Text = "0,31-0,36" And ComboBox2 <> "" And ComboBox3 <> ""
              ComboBox2.RowSource = "COLORE2"
              ComboBox3.RowSource = "QUALITA2"
    End If
    UserForm2.TextBox2.Text = ""
    If UserForm2.ComboBox1 <> "" And UserForm2.ComboBox2 <> "" And UserForm2.ComboBox3 <> "" Then
    Uriga = Range("A" & Rows.Count).End(xlUp).Row
    C = Rows("1:1" & Columns.Count).End(xlToRight).Column
    Set Col = Range(Cells(1, 1), Cells(1, C)).Find(UserForm2.ComboBox3.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Col Is Nothing Then
        C = Col.Column 'Row
    End If
    Set Riga = Range(Cells(1, 1), Cells(Uriga, 1)).Find(UserForm2.ComboBox1.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Riga Is Nothing Then
        R = Riga.Row
        For X = X + 1 To X + 30
        If Cells(R + X, 1) <> "" Then
            If Cells(R + X, 1) = UserForm2.ComboBox2.Text Then
                R = R + X
                Exit For
            End If
        End If
        Next X
    End If
    If UserForm2.TextBox1.Value = True Then
        UserForm2.TextBox2.Text = UserForm2.TextBox1.Value * Cells(R, C)
    End If
    End If
    End Sub
    Private Sub ComboBox3_Change()
            Call Change
    End Sub
    Private Sub TextBox1_Change()
    If UserForm2.TextBox1.Value = True Then
        UserForm2.TextBox2.Text = UserForm2.TextBox1.Value * Cells(R, C)
    End If
    
    End Sub
    
    Private Sub Change()
    UserForm2.TextBox2.Text = ""
    If UserForm2.ComboBox1 <> "" And UserForm2.ComboBox2 <> "" And UserForm2.ComboBox3 <> "" Then
    Uriga = Range("A" & Rows.Count).End(xlUp).Row
    LC = Rows("1:1" & Columns.Count).End(xlToRight).Column
    Set Col = Range(Cells(1, 1), Cells(1, LC)).Find(UserForm2.ComboBox3.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Col Is Nothing Then
        C = Col.Column 'Row
    End If
    Set Riga = Range(Cells(1, 1), Cells(Uriga, 1)).Find(UserForm2.ComboBox1.Text, LookIn:=xlValues, LookAt:=xlWhole)
    If Not Riga Is Nothing Then
        R = Riga.Row
        For X = X + 1 To X + 30
        If Cells(R + X, 1) <> "" Then
            If Cells(R + X, 1) = UserForm2.ComboBox2.Text Then
                R = R + X
                Exit For
            End If
        End If
        Next X
    End If
    End If
    End Sub



  • di Roby (utente non iscritto) data: 18/04/2014 09:47:25

    ok, bravo adesso il numero estrapolato è giusto, ma se provo a cambiare il rang (0,26-0,30) mi da errore.
    sigh.



  • di lepat (utente non iscritto) data: 18/04/2014 12:19:39

    nella sub change modifica così

     
    Set Col = Range(Cells(1, 1), Cells(Uriga, LC)).Find(UserForm2.ComboBox3.Text, LookIn:=xlValues, LookAt:=xlWhole)
    



  • di Roby (utente non iscritto) data: 18/04/2014 12:31:55

    bravo, ci siamo quasi, xò se cambio i valori della seconda combobox i valori non cambiano.



  • di Roby (utente non iscritto) data: 18/04/2014 12:43:57

    nel senso che per far uscire il numero giusto devo cambiare i valori di entrambe le combobox, mentre se vario solo una combobox il numero non cambia,



  • di lepat (utente non iscritto) data: 18/04/2014 13:08:39

    aggiungi
     
    Private Sub ComboBox2_Change()
            Call Change
    End Sub



  • di Roby (utente non iscritto) data: 18/04/2014 13:16:34

    funziona solo alla prima selezione delle tre combobox, ma se vado a variare successivamente le combobox il numero estrapolato è sbagliato, mannaggia.



  • di Zer0Kelvin data: 18/04/2014 15:24:08

    Ciao.
    Prova questa alternativa; ti rimane solo da impostare TextBox2
     
    Option Explicit
    
    Dim Tabella As Range
    
    Private Sub SetResult()
       Me.TextBox1 = Tabella.Offset(Me.ComboBox2.ListIndex + 1, Me.ComboBox3.ListIndex + 1)
    End Sub
    
    Private Function vCombos() As Boolean
       vCombos = Me.ComboBox1 <> "" And Me.ComboBox2 <> "" And Me.ComboBox3 <> ""
    End Function
    
    Private Sub ComboBox2_Change()
       If vCombos Then SetResult
    End Sub
    
    Private Sub ComboBox3_Change()
       If vCombos Then SetResult
    End Sub
    
    Private Sub ComboBox1_change()
    Dim I As Long
       Me.ComboBox2.Clear
       Me.ComboBox3.Clear
       Set Tabella = Foglio1.UsedRange.Columns(1).Find(Me.ComboBox1)
       If Not Tabella Is Nothing Then
          With Me.ComboBox2
             For I = 1 To Tabella.CurrentRegion.Rows.Count - 1
                .AddItem Tabella.Offset(I, 0)
             Next I
          End With
          With Me.ComboBox3
             For I = 1 To Tabella.CurrentRegion.Columns.Count - 1
                .AddItem Tabella.Offset(0, I)
             Next I
          End With
       End If
    End Sub
    
    Private Sub UserForm_Activate()
       UserForm2.ComboBox1.RowSource = "RANGE"
    End Sub
    
    Private Sub UserForm_Terminate()
       Set Tabella = Nothing
    End Sub
    



  • di Zer0Kelvin data: 18/04/2014 15:37:01

    Scusate, forse servono due righe di spiegazione.
    ComboBox1_change effettua una ricerca del valore nella prima colonna di Foglio1.
    Dopo la ricerca, la variabile Tabella punta alla cella dell'angolo in alto a destra della tabella interessata, imposta i valori di ComboBox1 e 2 leggendo direttamente i valori che si trovano sotto e a destra di Tabella.
    Dopo aver effettuato tutte le selezioni, gli indici delle due Combo vengono utilizzati per puntare al valore all'incrocio della riga e colonna selezionate, che viene assegnato a TextBox1.



  • di Roby (utente non iscritto) data: 18/04/2014 16:07:07

    grazie, adesso è un po più chiaro, OTTIMO! e grazie ancora.



  • di lepat (utente non iscritto) data: 18/04/2014 17:27:59

    in questi casi è meglio rincominciare daccapo come ha fatto zer0