Option Explicit
'v2
Private tovar_ras As Variant
Private kolvo_ras As Variant
Private cena__ras As Variant
Private tovar_pri As Variant
Private kolvo_pri As Variant
Private cena__pri As Variant
Sub Цена_расхода()
FillArrays
FillPriceArray
ClearArrays
ResizePriceArray
PrintPriceArray
End Sub
Private Sub FillArrays()
Dim sh As Worksheet, arr As Variant, brr As Variant, crr As Variant
Set sh = Sheets("Расход")
arr = sh.Cells(sh.UsedRange.Row, 1).Resize(sh.UsedRange.Rows.Count, 1).Value
brr = sh.Cells(sh.UsedRange.Row, 2).Resize(sh.UsedRange.Rows.Count, 1).Value
FillOneDimArrays sh.UsedRange.Row + 1, tovar_ras, kolvo_ras, cena__ras, arr, brr, crr
Set sh = Sheets("Приход")
arr = sh.Cells(sh.UsedRange.Row, 2).Resize(sh.UsedRange.Rows.Count, 1).Value
brr = sh.Cells(sh.UsedRange.Row, 3).Resize(sh.UsedRange.Rows.Count, 1).Value
crr = sh.Cells(sh.UsedRange.Row, 4).Resize(sh.UsedRange.Rows.Count, 1).Value
FillOneDimArrays sh.UsedRange.Row + 1, tovar_pri, kolvo_pri, cena__pri, arr, brr, crr
End Sub
Private Sub FillOneDimArrays(first_row As Long, tovar As Variant, kolvo As Variant, cena_ As Variant, arr As Variant, brr As Variant, crr As Variant)
Dim ya As Long
ReDim tovar(first_row To UBound(arr))
kolvo = tovar
cena_ = tovar
For ya = LBound(tovar) To UBound(tovar)
If Not IsEmpty(arr(ya, 1)) Then
If IsNumeric(brr(ya, 1)) Then
tovar(ya) = arr(ya, 1)
kolvo(ya) = brr(ya, 1)
If Not IsEmpty(crr) Then cena_(ya) = crr(ya, 1)
End If
End If
Next
End Sub
Private Sub FillPriceArray()
Dim firstRow As Object
Set firstRow = CreateObject("Scripting.Dictionary")
Dim yr As Long, yp As Long, dd As Long, cena As Variant
For yr = LBound(tovar_ras) To UBound(tovar_ras)
If Not IsEmpty(tovar_ras(yr)) Then
If firstRow.Exists(tovar_ras(yr)) Then
yp = firstRow(tovar_ras(yr))
Else
For yp = LBound(tovar_pri) To UBound(tovar_pri)
If tovar_pri(yp) = tovar_ras(yr) Then
Exit For
End If
Next
End If
Do
If kolvo_ras(yr) <= 0 Then Exit Do
Do
If tovar_pri(yp) = tovar_ras(yr) Then
If kolvo_pri(yp) > 0 Then
Exit Do
End If
End If
yp = yp + 1
If yp > UBound(tovar_pri) Then GoTo ExitDo
DoEvents
Loop
dd = kolvo_ras(yr)
If dd > kolvo_pri(yp) Then dd = kolvo_pri(yp)
cena = cena__ras(yr)
If IsEmpty(cena) Then
ReDim cena(0 To 0)
Else
ReDim Preserve cena(0 To UBound(cena) + 1)
End If
cena(UBound(cena)) = Array(yp, dd)
cena__ras(yr) = cena
cena = Empty
kolvo_ras(yr) = kolvo_ras(yr) - dd
kolvo_pri(yp) = kolvo_pri(yp) - dd
DoEvents
Loop
firstRow(tovar_ras(yr)) = yp
ExitDo:
End If
Next
End Sub
Sub ClearArrays()
tovar_ras = Empty
kolvo_ras = Empty
tovar_pri = Empty
kolvo_pri = Empty
cena__pri = Empty
End Sub
Private Sub ResizePriceArray()
Const sPrefix = "Приход!D"
Dim arr As Variant, ya As Long, yr As Long, yc As Long, cena As Variant, brr As Variant, iSum As Long
ReDim arr(1 To UBound(cena__ras) - LBound(cena__ras) + 1, 1 To 1)
yr = LBound(cena__ras) - 1
For ya = 1 To UBound(arr, 1)
yr = yr + 1
cena = cena__ras(yr)
If Not IsEmpty(cena) Then
If UBound(cena) > LBound(cena) Then
ReDim brr(0 To UBound(cena))
iSum = 0
For yc = 0 To UBound(cena)
brr(yc) = cena(yc)(1) & "*" & sPrefix & cena(yc)(0)
iSum = iSum + cena(yc)(1)
Next
arr(ya, 1) = Join(brr, "+")
arr(ya, 1) = "=(" & arr(ya, 1) & ")/" & iSum
Else
arr(ya, 1) = "=" & sPrefix & cena(0)(0)
End If
End If
Next
cena__ras = arr
End Sub
Private Sub PrintPriceArray()
Sheets("Расход").Range("C2").Resize(UBound(cena__ras, 1), 1).Formula = cena__ras
End Sub
|