电话表" 1"匹配值并放入每个数组工作表,然后将公式添加到F8偏移1

时间:2018-02-02 03:41:35

标签: vba for-loop

当我运行它时错误说对象需要,我不知道为什么。我最想要的就是把公式放到" F"的末尾。列结束(xlDown)并将其全部汇总到下面的单元格中。

img1 image img2 image

ttlerrrorcount = "0"
Set sh = ActiveWorkbook.Sheets("1")
WshtNames = Array("32132121", "32131221", "31231", "32132123121", "321313213")

For Each WshtNameCrnt In WshtNames
    With Worksheets(WshtNameCrnt)

        For b = 8 To .Cells(8, "D").End(xlDown).Row
            ttl = 0
            ttlerror = ""
            vals = Split(.Cells(b, "D").Value2, Chr(44))
            For a = LBound(vals) To UBound(vals)
                pc = Application.Match(vals(a), sh.Columns(1), 0)
                If Not IsError(pc) Then
                    ttl = ttl + sh.Cells(pc, "B").Value2
                ElseIf IsError(pc) Then
                    ttlerror = "Need follow-up"
                    ttlerrrorcount = ttlerrrorcount + 1
                End If
            Next a
            .Cells(b, "F") = ttl
            .Cells(b, "G") = ttlerror
        Next b

        'Total count of each sheet
        k = .Range("F8").End(xlDown).Row
        WshtNameCrnt.Range("F8").End(xlDown).Offset(1, 0).Select
        ActiveCell.Formula = "=sum(F8:F" & k & ")"

        With ActiveCell.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        End With
        With ActiveCell.Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThick
        End With
        'Total count of each sheet

    End With
Next WshtNameCrnt

1 个答案:

答案 0 :(得分:0)

WshtNameCrnt.Range("F8").End(xlDown).Offset(1, 0)

应该是

.Range("F8").End(xlDown).Offset(1, 0)

WshtNameCrnt不是工作表

尝试类似:

k = .Range("F8").End(xlDown).Row

'make sure not at bottom of sheet
If k < .Rows.Count Then
    With .Range("F8").End(xlDown).Offset(1, 0)
        .Formula = "=sum(F8:F" & k & ")"
        With .Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With .Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .Weight = xlThick
        End With
    End With
Else
    'if at bottom of the sheet do something else...
End With