Private Sub CmdConfirm_Click()
Application.ScreenUpdating = False
Dim extraLines As Integer
Dim wsTarget As Worksheet
Set wb = ThisWorkbook
If Me.CmbCurrentMonth = "" Then
MsgBox "请选择账单月份"
Exit Sub
End If
If Me.CmbDeadLine = "" Then
MsgBox "请选择最晚月份"
Exit Sub
End If
If Me.CmbPresident = "" Then
If Not wContinue("未选择法人,将生成所有法人的对账单!") Then Exit Sub
End If
Set wsSource = ThisWorkbook.Sheets("模版")
wsSource.Visible = True
With wsSource
lastRow = .UsedRange.Rows.Count
For i = 1 To lastRow
If .Cells(i, 1) = "编号" Then
tbFirstLine = i + 1
ElseIf .Cells(i, 1) = "小计" Then
tbLastLine = i - 1
Exit For
End If
Next
For i = lastRow To 1 Step -1
If .Cells(i, 1) = "小计" Then
tbLastLine2 = i - 1
ElseIf .Cells(i, 1) = "编号" Then
tbFirstLine2 = i + 1
Exit For
End If
Next
End With
If Me.CmbPresident = "" Then
'未选择法人,则生成所有法人的
For Each Key In dic.keys
k = 0
fileName = Key
ReDim arr1(1 To dic(Key), 1 To 12)
ReDim arr2(1 To dic(Key), 1 To 3)
For i = 2 To UBound(arrDetail)
If arrDetail(i, 1) = Key Then
'Stop
k = k + 1
For m = 2 To 13
arr1(k, m - 1) = arrDetail(i, m)
Next
arr2(k, 1) = arrDetail(i, 2)
arr2(k, 2) = arrDetail(i, 14)
arr2(k, 3) = arrDetail(i, 15)
End If
Next
If Me.OptCurrentTable Then
Call CopyWorksheet(wsSource, fileName)
Set wsTarget = wb.Sheets(fileName)
extraLines = UBound(arr1) - (tbLastLine - tbFirstLine + 1)
If extraLines > 0 Then
With wsTarget
.Range("B3") = Key
.Range("A5") = Me.CmbCurrentMonth
.Range("F5") = Me.CmbDeadLine
.Rows(tbFirstLine + 1 & ":" & tbFirstLine + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine, 2).Resize(UBound(arr1), UBound(arr1, 2)) = arr1
.Rows(tbFirstLine2 + 1 + extraLines & ":" & tbFirstLine2 + extraLines + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine2 + extraLines, 2).Resize(UBound(arr2), UBound(arr2, 2)) = arr2
For j = 1 To dic(Key)
.Cells(tbFirstLine + j - 1, 1) = j
.Cells(tbFirstLine2 + extraLines + j - 1, 1) = j
Next
memoLine = tbLastLine2 + 4 + IIf(extraLines > 0, extraLines * 2, 0)
.Cells(memoLine, 1) = "请各位法人于" & Me.CmbCurrentMonth & "缴完应付额"
End With
End If
Else
Set wb = Workbooks.Add
wsSource.Copy before:=wb.Sheets(1)
Set wsTarget = wb.Sheets(1)
wsTarget.Name = Key
extraLines = UBound(arr1) - (tbLastLine - tbFirstLine + 1)
If extraLines > 0 Then
With wsTarget
.Range("B3") = Key
.Range("A5") = Me.CmbCurrentMonth
.Range("F5") = Me.CmbDeadLine
.Rows(tbFirstLine + 1 & ":" & tbFirstLine + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine, 2).Resize(UBound(arr1), UBound(arr1, 2)) = arr1
.Rows(tbFirstLine2 + 1 + extraLines & ":" & tbFirstLine2 + extraLines + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine2 + extraLines, 2).Resize(UBound(arr2), UBound(arr2, 2)) = arr2
For j = 1 To dic(Key)
.Cells(tbFirstLine + j - 1, 1) = j
.Cells(tbFirstLine2 + extraLines + j - 1, 1) = j
Next
memoLine = tbLastLine2 + 4 + IIf(extraLines > 0, extraLines * 2, 0)
.Cells(memoLine, 1) = "请各位法人于" & Me.CmbCurrentMonth & "缴完应付额"
End With
End If
Application.DisplayAlerts = False
wb.SaveAs Me.TxbFilePath & "\" & Key & ".xlsx"
Application.DisplayAlerts = True
wb.Close
End If
Next
Else
'未选择法人,则生成所有法人的
k = 0
fileName = Me.CmbPresident
ReDim arr1(1 To dic(fileName), 1 To 12)
ReDim arr2(1 To dic(fileName), 1 To 3)
For i = 2 To UBound(arrDetail)
If arrDetail(i, 1) = fileName Then
'Stop
k = k + 1
For m = 2 To 13
arr1(k, m - 1) = arrDetail(i, m)
Next
arr2(k, 1) = arrDetail(i, 2)
arr2(k, 2) = arrDetail(i, 14)
arr2(k, 3) = arrDetail(i, 15)
End If
Next
If Me.OptCurrentTable Then
Call CopyWorksheet(wsSource, fileName)
Set wsTarget = wb.Sheets(fileName)
extraLines = UBound(arr1) - (tbLastLine - tbFirstLine + 1)
If extraLines > 0 Then
With wsTarget
.Range("B3") = fileName
.Range("A5") = Me.CmbCurrentMonth
.Range("F5") = Me.CmbDeadLine
.Rows(tbFirstLine + 1 & ":" & tbFirstLine + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine, 2).Resize(UBound(arr1), UBound(arr1, 2)) = arr1
.Rows(tbFirstLine2 + 1 + extraLines & ":" & tbFirstLine2 + extraLines + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine2 + extraLines, 2).Resize(UBound(arr2), UBound(arr2, 2)) = arr2
For j = 1 To dic(fileName)
.Cells(tbFirstLine + j - 1, 1) = j
.Cells(tbFirstLine2 + extraLines + j - 1, 1) = j
Next
memoLine = tbLastLine2 + 4 + IIf(extraLines > 0, extraLines * 2, 0)
.Cells(memoLine, 1) = "请各位法人于" & Me.CmbCurrentMonth & "缴完应付额"
End With
End If
Else
Set wb = Workbooks.Add
wsSource.Copy before:=wb.Sheets(1)
Set wsTarget = wb.Sheets(1)
wsTarget.Name = fileName
extraLines = UBound(arr1) - (tbLastLine - tbFirstLine + 1)
If extraLines > 0 Then
With wsTarget
.Range("B3") = fileName
.Range("A5") = Me.CmbCurrentMonth
.Range("F5") = Me.CmbDeadLine
.Rows(tbFirstLine + 1 & ":" & tbFirstLine + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine, 2).Resize(UBound(arr1), UBound(arr1, 2)) = arr1
.Rows(tbFirstLine2 + 1 + extraLines & ":" & tbFirstLine2 + extraLines + extraLines).Insert Shift:=xlDown
.Cells(tbFirstLine2 + extraLines, 2).Resize(UBound(arr2), UBound(arr2, 2)) = arr2
For j = 1 To dic(fileName)
.Cells(tbFirstLine + j - 1, 1) = j
.Cells(tbFirstLine2 + extraLines + j - 1, 1) = j
Next
memoLine = tbLastLine2 + 4 + IIf(extraLines > 0, extraLines * 2, 0)
.Cells(memoLine, 1) = "请各位法人于" & Me.CmbCurrentMonth & "缴完应付额"
End With
End If
Application.DisplayAlerts = False
wb.SaveAs Me.TxbFilePath & "\" & fileName & ".xlsx"
Application.DisplayAlerts = True
wb.Close
End If
End If
MsgBox "Done!"
wsSource.Visible = False
Unload Me
Application.ScreenUpdating = True
End Sub