Excel VBA - 在空白单元格之前的列中的SUM单元格

时间:2017-03-30 11:53:17

标签: excel vba excel-vba sum

我有Excel Offer Sheet的位置和宏,当B列中的值不为0时,会将行从此工作表传输到第二张工作表。它还会在字后面添加空行&#34; SUMA&#34;。< / p>

Option Explicit
Dim MyWorkbook As Workbook
Dim MyWorksheet As Worksheet
Dim MyOutputWorksheet As Worksheet

Sub PullData()
Set MyWorkbook = Workbooks(ActiveWorkbook.Name)
Set MyWorksheet = MyWorkbook.Sheets("Roboczy")
Set MyOutputWorksheet = MyWorkbook.Sheets("Oferta")

    Dim myValue As Long
    Dim RowPointer As Long

    For RowPointer = 1 To MyWorksheet.Cells(Rows.Count, "B").End(xlUp).Row
        If MyWorksheet.Range("B" & RowPointer).Value > 0 And MyWorksheet.Range("B" & RowPointer).Value <> "" Then
            If MyOutputWorksheet.Cells(Rows.Count, "B").End(xlUp).Row > 155 Then
                Exit Sub
            End If
            MyWorksheet.Range(("A" & RowPointer) & ":D" & RowPointer).Copy Destination:=MyOutputWorksheet.Range("A" & MyOutputWorksheet.Cells(Rows.Count, "B").End(xlUp).Row + 1)
        End If
    Next RowPointer


     Dim b As Range
For Each b In MyOutputWorksheet.Range("A1:A200")
 If b.Value Like "*SUMA*" Then
 b.Offset(1, 0).EntireRow.Insert
 End If
Next b

End Sub

我想念一些能清除#ADR的代码!并总结每个部分中的所有单元格。 更多细节 - &gt; Image of Excel sheet

1 个答案:

答案 0 :(得分:0)

这是一个纯粹的猜测,因为我看不到Roboczy表,但我认为你得到了我称之为#REF的东西!错误。可能由包含公式的源表引起,该公式引用了在转换为目标工作表时无效的范围。我能想到的最简单的解决方案是粘贴/特殊值而不是一般的粘贴来删除公式: 用以下内容替换您的一个粘贴行:

MyWorksheet.Range(("A" & RowPointer) & ":D" & RowPointer).Copy
MyOutputWorksheet.Range("A" & Cells(MyOutputWorksheet.Rows.Count, "B").End(xlUp).Row + 1).PasteSpecial xlPasteValues