View Full Version : Solved: What is wrong with this code???
Rayman
03-22-2011, 04:14 AM
I have a combo box (CmbPersonale) with multiselection
whit this code when MyValue is = to CmbPersonale.List(Ciclo) i getg run time error 404 " needed object" Why????
 
 
Dim oCell As Range, MyValue, ciclo
    
    With cmbPersonale
        .ColumnCount = 1
        .ColumnHeads = False
        
        For Each oCell In ActiveSheet.Range("C" & RigaInizio, "C" & NumRiga)
        If oCell <> "" Then oCell.Select
        MyValue = oCell
         Exit For
           Next
                    
          For ciclo = 1 To cmbPersonale.ListCount - 1
          If cmbPersonale.List(ciclo) = MyValue Then
          
    cmbPersonale.List(ciclo).Selected = True
        End If
           Next
             End With
 
Thanks in advance for your help
Bob Phillips
03-22-2011, 04:29 AM
Dim oCell As Range, MyValue, ciclo
 
    With cmbPersonale
        .ColumnCount = 1
        .ColumnHeads = False
         
        For Each oCell In ActiveSheet.Range("C" & RigaInizio, "C" & NumRiga)
        
            If oCell.Value <> "" Then
            
                MyValue = oCell.Text
                Exit For
            End If
        Next
         
        For ciclo = 0 To cmbPersonale.ListCount - 1
        
            If cmbPersonale.List(ciclo) = MyValue Then
                 
                cmbPersonale.ListIndex = ciclo + 1
                Exit For
            End If
        Next
    End With
Rayman
03-22-2011, 05:37 AM
Dim oCell As Range, MyValue, ciclo
 
With cmbPersonale
.ColumnCount = 1
.ColumnHeads = False
 
For Each oCell In ActiveSheet.Range("C" & RigaInizio, "C" & NumRiga)
 
If oCell.Value <> "" Then
 
MyValue = oCell.Text
Exit For
End If
Next
 
For ciclo = 0 To cmbPersonale.ListCount - 1
 
If cmbPersonale.List(ciclo) = MyValue Then
 
cmbPersonale.ListIndex = ciclo + 1
Exit For
End If
Next
End With
 
Thanks for reply Xld, but with i want to do is get selected MyValue in CmbPersonale if they are present in Range("C" & RigaInizio, "C" & NumRiga).
CmbPersonale is alredy filled in my code. I need only a sort of autoselected MyValue in CmbPersonale if it is present in the indicate range of activesheet.
Sorry for my bad english, i hope you can understand me....
Thanks again
mikerickson
03-22-2011, 06:27 AM
Try
    For ciclo = 1 To cmbPersonale.ListCount - 1 
        If cmbPersonale.List(ciclo) = MyValue Then 
             
            cmbPersonale.Selected(ciclo) = True 
        End If 
    Next
Rayman
03-22-2011, 09:00 AM
Try
    For ciclo = 1 To cmbPersonale.ListCount - 1 
        If cmbPersonale.List(ciclo) = MyValue Then 
 
            cmbPersonale.Selected(ciclo) = True 
        End If 
    Next 
 
Thanks for reply Mike,
with your modification i get selected only the last item present in range....
probably a problem with the Exit For wich make a cicle from 1 to List count only for the first name find in range ...
 Any ideas
Rayman
03-22-2011, 09:56 AM
Try
    For ciclo = 1 To cmbPersonale.ListCount - 1 
        If cmbPersonale.List(ciclo) = MyValue Then 
 
            cmbPersonale.Selected(ciclo) = True 
        End If 
    Next 
 
Thanks to yours input i solved the problem here is the right code:
 
Dim oCell As Range, MyValue, ciclo
 
With cmbPersonale
    .ColumnCount = 1
    .ColumnHeads = False
     
    For Each oCell In ActiveSheet.Range("C" & RigaInizio, "C" & NumRiga)
         If oCell.Value <> "" Then oCell.Activate
         MyValue = ActiveCell.Text
    For ciclo = 0 To cmbPersonale.ListCount - 1
        If cmbPersonale.List(ciclo) = MyValue Then
            cmbPersonale.Selected(ciclo) = True
        End If
    Next
Next
End With
 
Thanks to all you
mdmackillop
03-22-2011, 04:01 PM
Hi Rayman
Please use the green VBA button to format posted code as shown
mdmackillop
03-22-2011, 04:07 PM
Try to avoid selecting cells.  It's very rarely required and slows down code execution
For Each oCell In ActiveSheet.Range("C" & RigaInizio, "C" & NumRiga)
        If oCell.Value <> "" Then MyValue = oCell.Value
        For ciclo = 0 To cmbPersonale.ListCount - 1
            If cmbPersonale.List(ciclo) = MyValue Then
                cmbPersonale.Selected(ciclo) = True
            End If
        Next
    Next
Rayman
03-22-2011, 06:25 PM
Try to avoid selecting cells. It's very rarely required and slows down code execution
For Each oCell In ActiveSheet.Range("C" & RigaInizio, "C" & NumRiga)
If oCell.Value <> "" Then MyValue = oCell.Value
For ciclo = 0 To cmbPersonale.ListCount - 1
If cmbPersonale.List(ciclo) = MyValue Then
cmbPersonale.Selected(ciclo) = True
End If
Next
Next
 
Yes Md you are right, select is not necessary, ooh and yes next time i will use the green VBA button.:dunno 
 
Many thanks
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.