当命名范围 1 中的所有值都等于“N/A”VBAApplication.WorksheetFunction 时,希望删除命名范围 2

Looking to delete named range2 when all values in named range1 are equal to “N/A” VBA Application.WorksheetFunction

当该部分(命名范围 "Allowances_Credits_Range")中的所有值都等于“N/A 时,我们希望删除整个行部分(命名范围“Remove_Allowances_Credits”) ”.
例如,当 C161:C170 中的每个值都等于“N/A”时,我们希望删除行 156:171。我如何使用 application.worksheetFunction count 和 countif 来完成这个?

If Application.WorksheetFunction.Count(Range("Allowances_Credits_Range")) = Application.WorksheetFunction.Count(Range("Allowances_Credits_Range", "n/a")) Then

    Workbooks(PharmacyPricingGuarantees2).Sheets("Pharmacy Pricing Guarantees").Range("Remove_Allowances_Credits").Delete
End If

我收到错误 1004

If Application.WorksheetFunction.Count(Range("Allowances_Credits_Range")) = Application.WorksheetFunction.Count(Range("Allowances_Credits_Range", "n/a")) Then

    Workbooks(PharmacyPricingGuarantees2).Sheets("Pharmacy Pricing Guarantees").Range("Remove_Allowances_Credits").Delete
End If

删除命名范围"Remove_Allowances_Credits" 我收到错误 1004

尝试以下操作:

If Application.WorksheetFunction.COUNTA(Range("Allowances_Credits_Range")) = 
Application.WorksheetFunction.COUNTIF(Range("Allowances_Credits_Range"), "#N/A")) Then

    Workbooks(PharmacyPricingGuarantees2).Sheets("Pharmacy Pricing Guarantees").Range("Remove_Allowances_Credits").Delete

End If

首先,您想在第一部分使用 COUNTA 而不是 COUNTCOUNT 只计算数值。 COUNTA统计所有非空值(包括#N/A)。

其次,您想使用COUNTIF函数来计算值=#N/A.

不完全确定您要实现的目标,因此我添加了一些不同的方法。

Sub Examples()
    'Might need to change Workbook reference to PharmacyPricingGuarantees2?
    Dim ws As Worksheet: Set ws = Thisworkbook.Sheets("Pharmacy Pricing Guarantees")
    If ws.Range("Allowances_Credits_Range").Cells.Count = WorksheetFunction.CountIf(ws.Range("Allowances_Credits_Range"), "n/a") Then
        ws.Range("Remove_Allowances_Credits").ClearContents  '<-- Clear the values?
        ws.Range("Remove_Allowances_Credits").EntireRow.Delete '<-- Delete the rows?
        ThisWorkbook.Names("Remove_Allowances_Credits").Delete '<-- Delete the named range?
    End If
End Sub