Dim arr()
Dim iRow
Dim iCol
Dim tbTitle()
Dim ws As Worksheet, lastRow As Long, lastCol As Integer
Dim dic As Object
Dim dkey As String, temp()
Dim clsRG As New clsRanges
Private Sub CmdPrint_Click()
Dim t As Integer
Dim strOrder As String
With Me.LvProductList
For i = 1 To .ListItems.Count
If .ListItems(i).Checked Then
t = t + 1
strOrder = strOrder & .ListItems(i).SubItems(1) & "/"
End If
Next
End With
If t = 0 Then
MsgBox "未勾选任何记录!"
Exit Sub
End If
strOrder = Left(strOrder, Len(strOrder) - 1)
If Not wContinue("即将打印以下" & t & "条记录:" & Chr(10) & strOrder) Then Exit Sub
If Not Application.Dialogs(xlDialogPrinterSetup).Show Then Exit Sub
With Me.LvProductList
For i = 1 To .ListItems.Count
If .ListItems(i).Checked Then
dkey = .ListItems(i).SubItems(1)
Call printOrder(dkey)
End If
Next
End With
MsgBox "打印完成!"
End Sub
Private Sub printOrder(orderNo As String)
Dim arrTitle(), pages As Integer, iRow As Integer
Dim rng As Range, maxRows As Integer
arrTitle = dic(orderNo)("title")
temp = dic(orderNo)("data")
iRow = UBound(temp, 2)
Set ws = ThisWorkbook.Sheets("打印页")
With ws
Set rng = clsRG.物料明细
clsRG.产品编码 = arrTitle(2)
clsRG.产品名称 = arrTitle(1)
clsRG.订单号 = orderNo
clsRG.计划数量 = arrTitle(3)
clsRG.日期 = arrTitle(0)
maxRows = rng.Rows.Count
pages = Application.WorksheetFunction.RoundUp(iRow / maxRows, 0)
For i = 1 To pages
clsRG.编号 = "第" & i & "页,共" & pages & "页"
clsRG.编号.ShrinkToFit = True
rng.ClearContents
With rng
For j = (i - 1) * maxRows + 1 To Application.WorksheetFunction.Min(iRow, i * maxRows)
For k = 1 To UBound(temp)
If k < 3 Then
.Cells((j - 1) Mod maxRows + 1, k) = temp(k, j)
Else
.Cells((j - 1) Mod maxRows + 1, k + 1) = temp(k, j)
End If
Next
Next
End With
.PrintOut
Next
End With
End Sub
Private Sub CmdSearch_Click()
On Error Resume Next
Me.LvDetail.ListItems.Clear
Me.LvProductList.ListItems.Clear
Dim searchStr As String
Dim Lvitem As ListItem
iRow = UBound(arr)
iCol = 7
For i = 1 To iRow
For j = 1 To iCol
searchStr = searchStr & "|" & arr(i, j)
Next
If InStr(1, searchStr, Me.TextBox1.Value, 1) Then
Set Lvitem = Me.LvProductList.ListItems.Add
Lvitem.Text = Format(arr(i, 1), "YYYY/MM/DD")
For j = 2 To 7
Lvitem.SubItems(j - 1) = arr(i, j)
Next
End If
searchStr = ""
Next
End Sub
Private Sub CmdSelectAll_Click()
With Me.LvProductList
If Me.CmdSelectAll.Caption = "全选" Then
For i = 1 To .ListItems.Count
.ListItems(i).Checked = True
Next
Me.CmdSelectAll.Caption = "全消"
Me.CmdSelectAll.BackColor = RGB(176, 224, 230)
Else
For i = 1 To .ListItems.Count
.ListItems(i).Checked = False
Next
Me.CmdSelectAll.Caption = "全选"
Me.CmdSelectAll.BackColor = RGB(143, 188, 143)
End If
End With
End Sub
Private Sub UserForm_Activate()
'On Error Resume Next
Dim Lvitem As ListItem
Me.CmdSelectAll.Visible = True
Me.CmdSelectAll.BackColor = RGB(143, 188, 143)
Me.CmdPrint.Visible = True
Me.Caption = "生产领料单打印"
Set dic = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Sheets("列表")
With ws
lastRow = .UsedRange.Rows.Count
lastCol = .UsedRange.Columns.Count
arr = .Range(.Cells(2, 1), .Cells(lastRow, lastCol))
tbTitle = .Range(.Cells(1, 1), .Cells(1, lastCol)).Value
End With
For i = 1 To 8
With Me.LvProductList
.ColumnHeaders.Add , , tbTitle(1, i), 70
End With
Next
With Me.LvDetail
.View = lvwReport 'listview控件的显示外观
.Gridlines = True '是否有表格线,True有表格线
'.Sorted = True
'.CheckBoxes = True
.LabelEdit = lvwManual
.FullRowSelect = True
.ForeColor = vbBlue
End With
With Me.LvProductList
.View = lvwReport 'listview控件的显示外观
.Gridlines = True '是否有表格线,True有表格线
.Sorted = True
.CheckBoxes = True
.LabelEdit = lvwManual
.FullRowSelect = True
End With
iRow = UBound(arr)
iCol = UBound(arr, 2)
Me.LvProductList.ListItems.Clear
For i = 1 To iRow
If arr(i, 2) <> "" Then
dkey = arr(i, 2)
Set Lvitem = Me.LvProductList.ListItems.Add
Lvitem.Text = Format(arr(i, 1), "YYYY/MM/DD")
For j = 2 To 7
Lvitem.SubItems(j - 1) = arr(i, j)
Next
End If
If Not dic.exists(dkey) Then
dic.Add dkey, CreateObject("Scripting.Dictionary")
dic(dkey).Add "title", Array(arr(i, 1), arr(i, 3), arr(i, 4), arr(i, 5))
k = 1
ReDim temp(1 To 4, 1 To 1)
temp(1, k) = arr(i, 11)
temp(2, k) = arr(i, 12)
temp(3, k) = arr(i, 13)
temp(4, k) = arr(i, 14)
dic(dkey).Add "data", temp
Else
temp = dic(dkey)("data")
k = UBound(temp, 2) + 1
ReDim Preserve temp(1 To 4, 1 To k)
temp(1, k) = arr(i, 11)
temp(2, k) = arr(i, 12)
temp(3, k) = arr(i, 13)
temp(4, k) = arr(i, 14)
dic(dkey)("data") = temp
End If
Next
For i = 11 To 14
With Me.LvDetail
.ColumnHeaders.Add , , tbTitle(1, i), 70
End With
Next
End Sub
Private Sub LvProductList_ItemClick(ByVal Item As MSComctlLib.ListItem)
Dim temp(), Lvitem As ListItem
dkey = Me.LvProductList.SelectedItem.SubItems(1)
temp = dic(dkey)("data")
With Me.LvDetail
.ListItems.Clear
For i = 1 To UBound(temp, 2)
Set Lvitem = .ListItems.Add
With Lvitem
.Text = temp(1, i)
For j = 2 To UBound(temp)
.SubItems(j - 1) = temp(j, i)
Next
End With
Next
End With
End Sub
Private Sub Cmd_Exit_Click()
Unload Me
End Sub