Код |
---|
Option Explicit 'v4 Sub Суммировать_выделенные_ячейки() CloseEmptyWb Dim rr As Range On Error Resume Next Set rr = Intersect(Selection, ActiveSheet.UsedRange) Set rr = Intersect(rr, rr.Cells(1, 1).EntireColumn) On Error GoTo 0 If rr Is Nothing Then Exit Sub SumRange rr End Sub Private Sub SumRange(rr As Range) Dim rOut As Range ' Set rOut = Workbooks.Add(1).Sheets(1).Cells(1, 1) Application.Calculation = xlCalculationAutomatic Dim rArea As Range For Each rArea In rr.Areas SumArea rArea, rOut Next rOut.Parent.Parent.Saved = True End Sub Private Sub CloseEmptyWb() Dim wb As Workbook For Each wb In Application.Workbooks If wb.Path = "" Then wb.Close False Next End Sub Private Sub SumArea(rArea As Range, rOut As Range) ' Set rOut = rOut.Resize(rArea.Rows.Count) Set rOut = rArea If Intersect(rArea, rOut) Is Nothing Then Dim xa As Variant For Each xa In Array(1, 5, 9, 11) rArea.Columns(xa).Copy rOut.Cells(1, xa).Resize(rArea.Rows.Count) Next End If rOut.Columns(21).Clear rOut.Columns(21).FormulaR1C1 = "=RC[" & 5 - 21 & "]" ' rOut.Columns(27).Clear Dim yo As Long, yOpt As Long, yb As Long, dd As Double, duMin As Double For yo = 1 To rOut.Rows.Count If rOut.Cells(yo, 21).Value > 0 Then yOpt = 0 For yb = 1 To rOut.Rows.Count If yo <> yb Then If rOut.Cells(yo, 1).Value = rOut.Cells(yb, 1).Value Then If rOut.Cells(yb, 21).Value > 0 Then dd = rOut.Cells(yo, 21).Value + rOut.Cells(yb, 21).Value dd = rOut.Cells(yb, 11).Value - dd If dd >= 0 Then If dd < duMin Or yOpt = 0 Then yOpt = yb duMin = dd End If End If End If End If End If Next If yOpt <> 0 Then rOut.Cells(yOpt, 21).Formula = rOut.Cells(yOpt, 21).Formula & "+" & rOut.Cells(yo, 5).Address(0, 0, xlA1) rOut.Cells(yo, 21).Formula = rOut.Cells(yo, 21).Formula & "-" & rOut.Cells(yo, 5).Address(0, 0, xlA1) End If ' If rOut.Cells(yb, 26 + 1).Value >= 0 Then ' If rOut.Cells(yo, 26).Value <= rOut.Cells(yb, 11).Value - rOut.Cells(yb, 26).Value Then ' If rOut.Cells(yo, 26).Value < rOut.Cells(yb, 26).Value Then ' If rOut.Cells(yb, 26 + 1).Formula = "" Then ' rOut.Cells(yb, 26 + 1).Formula = "=" & rOut.Cells(yo, 5).Address(0, 0, xlA1) ' Else ' rOut.Cells(yb, 26 + 1).Formula = rOut.Cells(yb, 26 + 1).Formula & "+" & rOut.Cells(yo, 5).Address(0, 0, xlA1) ' End If ' rOut.Cells(yo, 26 + 1).FormulaR1C1 = "=-RC[" & 5 - 26 - 1 & "]" ' Else ' If rOut.Cells(yo, 26 + 1).Formula = "" Then ' rOut.Cells(yo, 26 + 1).Formula = "=" & rOut.Cells(yb, 5).Address(0, 0, xlA1) ' Else ' rOut.Cells(yo, 26 + 1).Formula = rOut.Cells(yo, 26 + 1).Formula & "+" & rOut.Cells(yb, 5).Address(0, 0, xlA1) ' End If ' rOut.Cells(yb, 26 + 1).FormulaR1C1 = "=-RC[" & 5 - 26 - 1 & "]" ' End If ' Exit For ' End If ' End If ' End If ' End If ' End If End If Next Set rOut = rOut.Cells(rOut.Rows.Count + 1, 1) End Sub |
Цитата |
---|
написал: выдает ошибку 13))) не подскажете почему такое и как это устранять на будущее |
Как устранять? Можно, например, удалять текст, там, где ожидается число.