Bonjour,

J'ai un fichier avec 2 onglets qui correspondent à deux types d'opérations (Investissement et Entretien). Pour chaque onglet, j'ai créer un Inputbox qui me demande si je veux créer une opération (rajout d'une ligne) ou en modifier une.

(1 ligne = 1 opération).

Si je veux créer une opération, un deuxieme Inputbox apparait et me demande à quel programme cette opération correspond (il y a 6 programmes différents dans l'onglet Investissement, 9 dans l'onglet Entretien). Les intitulés des programmes correspondent à une ligne du tableau et chaque opération concernant ce programme doivent se placer à la ligne d'en dessous.

Voici le programme que j'ai mi en place pour l'onglet Investissement :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Private Sub Worksheet_Activate()
Dim i As Integer, Choix1 As Integer, Choix2 As String
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim n As Integer
 
Choix1 = InputBox("Voulez-vous créer ou modifier une opération ? - tapez 1 pour 'créer' ou 2 pour 'modifier'")
 
If Choix1 = 1 Then
  Choix2 = InputBox("Entrez le type de programme pour l'opération que vous voulez créer - tapez 'PRN' ou 'OA' ou 'ACPS' ou 'IC' ou 'E' ou 'ENV'")
  i = Sheets("Investissement").Range("B" & Sheets("Protection contre les Risques Naturels").Rows.Count).End(xlUp).Row + 1
  i = IIf(i >= 4, i, 4)
  j = Sheets("Investissement").Range("B" & Sheets("Ouvrages d'art").Rows.Count).End(xlUp).Row + 1
  j = IIf(j >= 14, j, 14)
  k = Sheets("Investissement").Range("B" & Sheets("Aménagements de Carrefours et Points Singuliers").Rows.Count).End(xlUp).Row + 1
  k = IIf(k >= 34, j, 34)
  l = Sheets("Investissement").Range("B" & Sheets("Pistes cyclables").Rows.Count).End(xlUp).Row + 1
  l = IIf(l >= 36, j, 36)
  m = Sheets("Investissement").Range("B" & Sheets("Etudes").Rows.Count).End(xlUp).Row + 1
  m = IIf(m >= 38, j, 38)
  n = Sheets("Investissement").Range("B" & Sheets("Environnement").Rows.Count).End(xlUp).Row + 1
  n = IIf(n >= 44, j, 44)
 
If Choix2 = "PRN" Then
Sheets("Investissement").Range("B" & i).Value = Choix2
End If
 
        If Choix2 = "OA" Then
        Sheets("Investissement").Range("B" & j).Value = Choix2
        End If
 
            If Choix2 = "ACPS" Then
            Sheets("Investissement").Range("B" & k).Value = Choix2
            End If
 
                If Choix2 = "IC" Then
                Sheets("Investissement").Range("B" & l).Value = Choix2
                End If
 
                    If Choix2 = "E" Then
                    Sheets("Investissement").Range("B" & m).Value = Choix2
                    End If
 
                        If Choix2 = "ENV" Then
                        Sheets("Investissement").Range("B" & n).Value = Choix2
                        End If
 
Else
'.........
End If
 
i = i + 1
j = j + 1
k = k + 1
l = l + 1
m = m + 1
n = n + 1
 
End Sub

L'erreur qui s'affiche est "l'indice n'appartient pas à la sélection".

Deux choses :
-Pourquoi ce message d'erreur s'affiche ? Comment régler ce problème ?
- N'y a t_il pas de meilleure solution plus simple pour ce type de travail ?


Merci d'avance