Dim flag As Boolean
Dim dc1 As Long
Dim X As Byte
Dim ligentdes As Long
Dim nomfeuille1 As String
Private Sub CommandButton1_Click()
Dim col1 As Long
Dim data1 As String
Dim data2 As String
Dim col2 As Long
col1 = CLng(ComboBox1.List(ComboBox1.ListIndex, ComboBox1.ColumnCount - 1))
col2 = CLng(ComboBox2.List(ComboBox2.ListIndex, ComboBox2.ColumnCount - 1))
data1 = Replace(ComboBox1.Value, " ", "")
data2 = Replace(ComboBox2.Value, " ", "")
If ComboBox1.Value = "" Then Exit Sub
If ComboBox1.ListIndex = -1 Or ComboBox2.ListIndex = -1 Then Exit Sub
If data1 > data2 Then
MsgBox "La date de fin ne peut pas etre antérieure à la date de début ."
Exit Sub
End If
With Worksheets(nomfeuille1)
For i = 2 To Sheets(nomfeuille1).Range("a65536").End(xlUp).Row
cold = dc1 - col1 + 1 ' à vérifier
colf = dc1 - col2
.Cells(i, dc1).FormulaR1C1 = "=AVERAGE(RC[-" & cold & "]:RC[-" & colf & "])"
Next i
End With
'Fermeture UserForm
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim data1 As String
flag = True
ligentdes = 1
nomfeuille1 = "Feuil1"
'Remplit la ListBox pour choisir une année.
'Windows(Feuil1).Activate
dc1 = Sheets(nomfeuille1).Range("IV" & ligentdes).End(xlToLeft).Column
With ComboBox2
.Clear
.ColumnCount = 2
.ColumnWidths = "50;0"
End With
With ComboBox1
.Clear
.ColumnCount = 2
.ColumnWidths = "50;0"
For X = 10 To dc1 - 3
data1 = ""
For i = 1 To Len(Sheets(nomfeuille1).Cells(1, X))
If IsNumeric(Mid(Sheets(nomfeuille1).Cells(1, X), i, 1)) Then
data1 = data1 & Mid(Sheets(nomfeuille1).Cells(1, X), i, 1)
End If
Next i
data1 = Left(data1, 4) & " " & Right(data1, 2)
If .ListCount > 0 Then .Value = data1
If .ListIndex = -1 And data1 <> " " Then
.AddItem data1
.List(.ListCount - 1, 1) = X
ComboBox2.AddItem data1
ComboBox2.List(ComboBox2.ListCount - 1, 1) = X ' numero colonne
End If
Next X
.Style = 2
.Value = ""
End With
ComboBox2.Style = 2
End Sub