Private Sub btnXuatKho_Click()
ThisWorkbook.Activate
Dim i&, k&
Dim ArrUndo(), ArrData(1 To 1, 1 To 6)
Dim aRow%, jRow
If Me.XUANCHIEN1.ListIndex = -1 Then Exit Sub
aRow = Me.XUANCHIEN1.ListCount
ReDim ArrUndo(1 To aRow, 1 To 6)
Application.ScreenUpdating = False
jUndo = 0: jRow = 0
For i = 0 To Me.XUANCHIEN1.ListCount - 1
If Me.XUANCHIEN1.Selected(i) And WorksheetExists(Me.XUANCHIEN1.List(i, 3)) Then
With ThisWorkbook.Sheets(Me.XUANCHIEN1.List(i, 3))
ArrData(1, 1) = .Range("A65000").End(xlUp).Row
For k = 1 To 5
ArrData(1, k + 1) = Me.XUANCHIEN1.List(i, k)
Next k
.Range("A" & (.Range("A65000").End(xlUp).Row + 1)).Resize(, 5) = ArrData
End With
Else
jUndo = jUndo + 1: ArrUndo(jUndo, 1) = jUndo
For k = 1 To 5
ArrUndo(jUndo, k + 1) = Me.XUANCHIEN1.List(i, k)
Next k
End If
Next i
Sheet2.Range("A2:F" & (Sheet2.Range("A65000").End(xlUp).Row + 1)).Delete
If jUndo Then Sheet2.Range("A" & (Sheet2.Range("A65000").End(xlUp).Row + 1)).Resize(jUndo, 5) = ArrUndo
XUANCHIEN1.Clear
If jUndo Then
ReDim ArrTemp(1 To jUndo, 1 To 6)
For i = 1 To jUndo
For k = 1 To 6
ArrTemp(i, k) = ArrUndo(i, k)
Next k
Next i
XUANCHIEN1.List() = ArrTemp
End If
Application.ScreenUpdating = True
End Sub
Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
WorksheetExists = Not sht Is Nothing
End Function