我怎样才能写一个macros来总结一个有#N / A值的列?

我正在寻找创build一个总是包含几个#N / A值的列的macros,该列中的单元格数量将每天都在变化,#N / A值的位置也会变化。

我也想把结果放在最后一个值的第一个空单元格,也就是列底部的第一个空单元格。

这是我所能得到的:

Option Explicit Sub Total() ' ' Total Macro Dim rg As Range Dim Cell As Range, Target As Range Set Target = Range("D65536").End(xlUp) For Each Cell In Target Cell.Errors(xlEvaluateToError).Ignore = True Set rg = Range("D65536").End(xlUp) rg.Offset(1, 0).Value = "=sumif(D1:D100 " <> 0 & rg.Row & ")" Range("D65536").End(xlUp).Select Selection.Font.Bold = True Next End Sub 

我使用D1:D100,但只是因为100将覆盖我的列中的单元格的数量。 这是一个奇怪的结果,更多#N / As后面跟着一个True。 我不认为我应该试图在代码中使用公式。

谢谢

我会循环,并检查值是数字,然后将其添加到总数:

 Sub mysum() Dim lstRow As Long Dim ws As Worksheet Dim i As Long Set ws = Worksheets("Sheet5") 'Change to your sheet With ws lstRow = .Cells(.Rows.Count, 4).End(xlUp).Row For i = 1 To lstRow If IsNumeric(.Cells(i, 4)) Then .Cells(lstRow + 1, 4) = .Cells(lstRow + 1, 4) + .Cells(i, 4) End If Next i End With End Sub 

你可以使用SpecialCells():

 Sub total() With Worksheets("SheetName") '<--| Change to your actual sheet name With .Range("D1", .Cells(.Rows.Count, 4).End(xlUp)) .Cells(.Rows.Count,1).Offset(1).Value = WorksheetFunction.Sum(.SpecialCells(xlCellTypeConstants, xlNumbers)) End With End With End Sub