Dim SourceX!
Dim T()
Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim i&
Dim colLarg!
Dim A$
Dim pos&
If Button = 2 And Shift = 1 Then
With ListBox1
If Y < .Font.Size Then
SourceX! = X
ReDim T(1 To .ColumnCount)
A$ = .ColumnWidths & ";"
For i& = 1 To UBound(T)
pos& = InStr(1, A$, ";")
T(i&) = CSng(Val(Mid(A$, 1, pos& - 1)))
A$ = Mid(A$, pos& + 1)
Next i&
For i& = 1 To UBound(T)
colLarg! = colLarg! + T(i&)
Next i&
If X > colLarg! Then
SourceX! = 0
Erase T
End If
End If
End With
End If
End Sub
Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim i&
Dim deb!
Dim fin!
Dim A$
If SourceX! > 0 Then
For i& = 1 To UBound(T)
fin! = fin! + T(i&)
If X > SourceX! Then
If SourceX > deb! And SourceX < fin! Then
T(i&) = T(i&) + X - fin!
Exit For
End If
ElseIf X < SourceX! Then
If X > deb! And X < fin! Then
T(i&) = X - deb!
Exit For
End If
End If
deb! = fin!
Next i&
For i& = 1 To UBound(T)
If T(i&) < 15 Then T(i&) = 15
A$ = A$ & T(i&) & ";"
Next i&
If A$ <> "" Then ListBox1.ColumnWidths = "" & Mid(A$, 1, Len(A$) - 1) & ""
SourceX! = 0
Erase T
End If
End Sub
Private Sub UserForm_Initialize()
Dim S As Worksheet
Dim R As Range
Dim var
Dim nbCol&
Dim i&
Dim A$
On Error Resume Next
Set S = Sheets(MA_FEUILLE)
If Err <> 0 Then
MsgBox "La feuille ''" & MA_FEUILLE & "'' est introuvable."
Unload Me
Exit Sub
End If
On Error GoTo 0
With Me
.Caption = "Modifier la largeur des colonnes d'une ListBox"
.Width = 400
.Height = 220
End With
With ListBox1
.Top = 50
.Left = 20
.Width = Me.Width - 40
.Height = Me.Height - 100
End With
With Label1
.Top = 10
.Left = 30
.Caption = "Pour modifier la largeur des colonnes, maintenez Shift et clic droit" & _
" puis bougez latéralement la souris dans la ligne de titre de la ListBox"
.AutoSize = True
.AutoSize = False
.Width = Me.Width - 60
End With
Set R = Sheets(MA_FEUILLE).UsedRange
var = R
nbCol& = UBound(var, 2)
With ListBox1
.ColumnCount = UBound(var, 2)
.ColumnHeads = True
.List = var
'--- Fabrique une chaîne des largeurs du type "50;50;50..." ---
For i& = 1 To nbCol&
A$ = A$ & "50;"
Next i&
A$ = "" & Mid(A$, 1, Len(A$) - 1) & ""
.ColumnWidths = A$
End With
End Sub