Home » excel » excel – Where does my "error of compatibilty" comes from?

excel – Where does my "error of compatibilty" comes from?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have to code a little app in VBA, for school.
For this app, I need to add items in some different listbox, as it this the same way of doing for each listbox, I created a method with parameters : the sheet I’m working with, the column where I start and the listbox i have to fill.

But when I execute the code, I have an error of compatibility of type”. I think it comes from the line where I call my sub. But I didn’t find a way to correct that since 3 days.

The var ‘cellulePrecedente’ represents the text in the cell that was read the iteration before, the content can be repeated multiple times so I only add the name of the course when it is different from the one before. To avoid writing two times the same course.

I also take any tips, as I’m a beginner in VBA.

Here comes the code :

Private Sub UserForm_Initialize()
    ' On va appeler, pour chaque listbox a remplir la méthode d'initialisation
    Call Initialiser_ListBoxs("Cours Ig", "D", ListBox1)
End Sub

Private Sub Initialiser_ListBoxs(Feuille As String, ColonneDepart As String, ListBoxConcernee As ListBox)
    Dim CellulePrecedente As String
    ' On doit fournir la feuille dans laquelle on doit rechercher les éléments, la colonne de depart (si on fait les cours ou les ue
    ' Il faut aussi fournir la listbox a remplir
    Sheets(Feuille).Activate
    ' On sélectionne la cellule correspondant au point de départ (1er cours/1ere UE)
    ' La première ligne étant le titre de la colonne
    Range(ColonneDepart & "2").Select
    CellulePrecedente = ""
    ' On continue de parcourir tant que tous les cours/UE n'ont pas été lu(e)s
    While Range(ColonneDepart & ActiveCell.Row) <> ""
        ' On ne réécrit pas 2 fois le/la même cours/UE
        If CellulePrecedente <> Range(ColonneDepart & ActiveCell.Row) Then
            ListBoxConcernee.AddItem Range(ColonneDepart & ActiveCell.Row)
        End If
        CellulePrecedente = Range(ColonneDepart & ActiveCell.Row)
        Range(ColonneDepart & ActiveCell.Row + 1).Select
    Wend
End Sub

Thank you for help !

PS: I know the code is written in french but I tried my best to explain how it works. I assume a possibility you don’t understand everything, feel free to ask any question !

How to&Answers:

Change:

Private Sub Initialiser_ListBoxs(Feuille As String, ColonneDepart As String, _
                             ListBoxConcernee As ListBox)

To:

Private Sub Initialiser_ListBoxs(Feuille As String, ColonneDepart As String, _
                             ListBoxConcernee As MSForms.ListBox)