Ich habe 4 Listboxen.
Wenn ich in jeder Listbox etwas anklicke, klappt alles.
Wenn ich die obersten Listbox2 leer lasse und nur in der Listbox3 etwas eingebe, dann bekomme ich einen Laufzeitfehler 1004 "Dies ist bei verbundenen Zellen leider nicht möglich"
Er bleibt bei
rng3.Copy .Cells(lngNext, 2)
hängen.
Hier der gesamte Code dazu:
Private Sub cmdOK_Click()
Dim lngI As Long, lngNext As Long, rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
With Sheets("Listbox2")
For lngI = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(lngI) Then
If rng2 Is Nothing Then
Set rng2 = .Cells(lngI + 1, 1)
Else
Set rng2 = Union(rng2, .Cells(lngI + 1, 1))
End If
End If
Next
End With
With Sheets("Listbox3")
For lngI = 0 To ListBox3.ListCount - 1
If ListBox3.Selected(lngI) Then
If rng3 Is Nothing Then
Set rng3 = .Cells(lngI + 1, 1)
Else
Set rng3 = Union(rng3, .Cells(lngI + 1, 1))
End If
End If
Next
End With
With Sheets("Listbox4")
For lngI = 0 To ListBox4.ListCount - 1
If ListBox4.Selected(lngI) Then
If rng4 Is Nothing Then
Set rng4 = .Cells(lngI + 1, 1)
Else
Set rng4 = Union(rng4, .Cells(lngI + 1, 1))
End If
End If
Next
End With
With Sheets("Listbox1")
For lngI = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lngI) Then
If rng1 Is Nothing Then
Set rng1 = .Cells(lngI + 1, 1)
Else
Set rng1 = Union(rng1, .Cells(lngI + 1, 1))
End If
End If
Next
End With
With Sheets("Fahrzeugbegleitkarte")
If Not rng2 Is Nothing Then rng2.Copy .Cells(13, 2)
lngNext = Application.Max(11, .Cells(.Rows.Count, 2).End(xlUp).Row + 1)
If Not rng3 Is Nothing Then rng3.Copy .Cells(lngNext, 2)
lngNext = Application.Max(11, .Cells(.Rows.Count, 2).End(xlUp).Row + 1)
If Not rng4 Is Nothing Then rng4.Copy .Cells(lngNext, 2)
lngNext = Application.Max(11, .Cells(.Rows.Count, 2).End(xlUp).Row + 1)
If Not rng1 Is Nothing Then rng1.Copy .Cells(lngNext, 2)
Unload Me
End With
Set rng1 = Nothing
Set rng2 = Nothing
Set rng3 = Nothing
Set rng4 = Nothing