weiß zufällig einer von Euch, wie ich eine Tabelle in eine UserForm einbinden kann? Dabei wäre es gut, wenn sowohl die Anzahl der Spalten, als auch die der Zeilen variabel wären.
Vielen Dank schon mal im Voraus!!!
Gruß Karl
Option Explicit
Private wksSource As Worksheet
Private Sub ResetListbox(objListbox As Control, wksQuelle As Worksheet, _
Optional Spalte1 As Long = 1, Optional Zeile1 As Long = 2, _
Optional Spalte2 As Double = 0, Optional Zeile2 As Long = 0, _
Optional FaktorSpalte As Double = 8, _
Optional bolColumnHeads As Boolean = True, _
Optional lngListstyle As Long = 1, _
Optional lngMultiselect As Long = 0)
'Spalte1 = 1. Spalte des Quellblatts, die in Listbox angezeigt werden soll
'Zeile1 = 1. Zeile des Quellblatts, die in Listbox angezeigt werden soll
'Spalte2 = wenn 0 dann ist dies die letzte Spalte die in Listbox _
angezeigt werden soll, bei 0 wird bis letzte Datenspalte angezeigt
'Zeile2 = wenn 0 dann ist dies die letzte Zeile die in Listbox _
angezeigt werden soll, bei 0 wird bis letzte Datenzeile angezeigt
'FaktorSpalte: Rechnet die Spaltenbreiten der Tabelle in SpaltenBreiten für die Listbox um
'lngListstyle: 1 = mit Optionbuttons, 0 = einfach
'lngMultiselect: 0 = Einfachauswahl, 1 = Multiselect, 2 = Multiselect extended
Dim Spalte As Long, strColumnwidths As String, strRowSource As String
Dim dblWidth As Double
With wksQuelle
With .Cells.SpecialCells(xlCellTypeLastCell)
If Spalte2 = 0 Then Spalte2 = .Column
If Zeile2 = 0 Then Zeile2 = .Row
End With
strRowSource = "'" & .Name & "'!" & .Range(.Cells(Zeile1, Spalte1), _
.Cells(Zeile2, Spalte2)).Address
strColumnwidths = ""
dblWidth = 30
For Spalte = Spalte1 To Spalte2
If strColumnwidths = "" Then
strColumnwidths = .Columns(Spalte).ColumnWidth * FaktorSpalte & "Pt"
Else
strColumnwidths = strColumnwidths & ";" _
& .Columns(Spalte).ColumnWidth * FaktorSpalte & "Pt"
End If
dblWidth = dblWidth + .Columns(Spalte).ColumnWidth * FaktorSpalte
Next
End With
With objListbox
.Width = Application.WorksheetFunction.Min(Me.Width - .Left - 15, dblWidth)
' .Width = Application.WorksheetFunction.Min(.Width, dblWidth)'
.ListStyle = lngListstyle
.MultiSelect = lngMultiselect
.ColumnWidths = strColumnwidths
.ColumnHeads = bolColumnHeads
.ColumnCount = Spalte2 - Spalte1 + 1
.RowSource = strRowSource
End With
Me.Repaint
End Sub
Private Sub UserForm_Initialize()
Set wksSource = Worksheets("Tabelle1") 'Quelltabelle der Daten
'Spalten 1 bis 3 der Tabelle1 ab Zeile 2 als Listbox-Quelle verwenden.
Call ResetListbox(objListbox:=Me.Controls("ListBox1"), wksQuelle:=wksSource, Spalte2:=3)
End Sub