VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} UserForm2 
   Caption         =   "Перегруппировка данных"
   ClientHeight    =   3885
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   5970
   OleObjectBlob   =   "ReGroupForm.frx":0000
   StartUpPosition =   1  'CenterOwner
End
Attribute VB_Name = "UserForm2"
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub CommandAllLeft_Click()
    If ListBox2.ListCount > 0 Then
        ListBox2.ListIndex = 0
        For i = 1 To ListBox2.ListCount
            ListBox1.AddItem (ListBox2.List(ListBox2.ListIndex))
            ListBox1.List(ListBox1.ListCount - 1, 1) = ListBox2.List(ListBox2.ListIndex, 1)
            ListBox2.RemoveItem (ListBox2.ListIndex)
        Next i
    End If
End Sub
Private Sub CommandAllRight_Click()
    If ListBox1.ListCount > 0 Then
        ListBox1.ListIndex = 0
        For i = 1 To ListBox1.ListCount
            ListBox2.AddItem (ListBox1.List(ListBox1.ListIndex))
            ListBox2.List(ListBox2.ListCount - 1, 1) = ListBox1.List(ListBox1.ListIndex, 1)
            ListBox1.RemoveItem (ListBox1.ListIndex)
        Next i
    End If
End Sub

Private Sub CommandCancel_Click()
    Unload Me
End Sub

Private Sub CommandExec_Click()
    j = 0
    If ListBox2.ListCount > 0 Then
        For i = 0 To ListBox2.ListCount - 1
            j = j + 1
            wsd.Cells(j + GroupStartRow, 1).Value = ListBox2.List(i)
            wsd.Cells(j + GroupStartRow, 2).Value = ListBox2.List(i, 1)
            wsd.Cells(j + GroupStartRow, 3).Value = 1
        Next i
    End If
    If ListBox1.ListCount > 0 Then
        For i = 0 To ListBox1.ListCount - 1
            j = j + 1
            wsd.Cells(j + GroupStartRow, 1).Value = ListBox1.List(i)
            wsd.Cells(j + GroupStartRow, 2).Value = ListBox1.List(i, 1)
            wsd.Cells(j + GroupStartRow, 3).Value = 0
        Next i
    End If
    lnCount = j + 1
    Application.ScreenUpdating = False
    wsd.Visible = True
    wsd.Activate
    Worksheets("Чистый").Copy after:=Worksheets("Чистый")
    Application.ActiveSheet.Name = "Документ"
    Set wso = Worksheets("Документ")
    Worksheets("Чистый").Cells.Copy Worksheets("Документ").Cells
    wso.Visible = True
    Call DeleteWorkSheets
    wsd.Rows(Trim(Str(DataStartRow - 1)) + ":32767").Delete Shift:=xlUp
    Call t1
    Application.ThisWorkbook.Saved = True
    Unload Me
End Sub

Private Sub CommandRight_Click()
    Do
        nSelItem = 0
        If ListBox1.ListCount > 0 Then
            For i = 0 To ListBox1.ListCount - 1
                If ListBox1.Selected(i) Then
                    ListBox2.AddItem (ListBox1.List(i))
                    ListBox2.List(ListBox2.ListCount - 1, 1) = ListBox1.List(i, 1)
                    ListBox1.RemoveItem (i)
                    nSelItem = 1
                    Exit For
                End If
            Next i
        End If
    Loop Until nSelItem = 0
End Sub
Private Sub CommandLeft_Click()
    Do
        nSelItem = 0
        If ListBox2.ListCount > 0 Then
            For i = 0 To ListBox2.ListCount - 1
                If ListBox2.Selected(i) Then
                    ListBox1.AddItem (ListBox2.List(i))
                    ListBox1.List(ListBox1.ListCount - 1, 1) = ListBox2.List(i, 1)
                    ListBox2.RemoveItem (i)
                    nSelItem = 1
                    Exit For
                End If
            Next i
        End If
    Loop Until nSelItem = 0
End Sub
Private Sub CommandUp_Click()
    If ListBox2.ListCount > 0 Then
        For i = 0 To ListBox2.ListCount - 1
            If ListBox2.Selected(i) And (i > 0) Then
                If Not ListBox2.Selected(i - 1) Then
                    t = ListBox2.List(i - 1)
                    t2 = ListBox2.List(i - 1, 1)
                    ListBox2.List(i - 1) = ListBox2.List(i)
                    ListBox2.List(i - 1, 1) = ListBox2.List(i, 1)
                    ListBox2.List(i) = t
                    ListBox2.List(i, 1) = t2
                    ListBox2.Selected(i) = False
                    ListBox2.Selected(i - 1) = True
                End If
            End If
        Next i
    End If
End Sub
Private Sub CommandDown_Click()
    If ListBox2.ListCount > 0 Then
        For i = ListBox2.ListCount - 1 To 0 Step -1
            If ListBox2.Selected(i) And (i < ListBox2.ListCount - 1) Then
                If Not ListBox2.Selected(i + 1) Then
                    t = ListBox2.List(i + 1)
                    t2 = ListBox2.List(i + 1, 1)
                    ListBox2.List(i + 1) = ListBox2.List(i)
                    ListBox2.List(i + 1, 1) = ListBox2.List(i, 1)
                    ListBox2.List(i) = t
                    ListBox2.List(i, 1) = t2
                    ListBox2.Selected(i) = False
                    ListBox2.Selected(i + 1) = True
                End If
            End If
        Next i
    End If
End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Call CommandRight_Click
End Sub
Private Sub ListBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Call CommandLeft_Click
End Sub

Private Sub UserForm_Initialize()
    lbFind = False
    For Each loSheet In Application.ActiveWorkbook.Sheets
        lbFind = lbFind Or loSheet.Name = "Группы"
    Next loSheet
    If lbFind Then
        Set wsd = Application.ActiveWorkbook.Worksheets("Данные")
        Set wsh = Application.ActiveWorkbook.Worksheets("Группы")
        i = 1
        While wsd.Cells(i + GroupStartRow, 1).Value <> ""
            If wsd.Cells(i + GroupStartRow, 3).Value > 0 Then
                ListBox2.AddItem (wsd.Cells(i + GroupStartRow, 1).Value)
                ListBox2.List(ListBox2.ListCount - 1, 1) = wsd.Cells(i + GroupStartRow, 2).Value
            Else
                ListBox1.AddItem (wsd.Cells(i + GroupStartRow, 1).Value)
                ListBox1.List(ListBox1.ListCount - 1, 1) = wsd.Cells(i + GroupStartRow, 2).Value
            End If
            i = i + 1
        Wend
    Else
        Unload Me
    End If
End Sub

