【问题标题】:Looking to delete named range2 when all values in named range1 are equal to “N/A” VBA Application.WorksheetFunction当命名范围 1 中的所有值都等于“N/A”VBA Application.WorksheetFunction 时,希望删除命名范围 2
【发布时间】:2019-01-09 15:16:37
【问题描述】:

当部分(命名范围“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

【问题讨论】:

  • Count 只有一个参数,看来你需要 COUNTIF。
  • 您是要删除行、清除值还是删除命名范围?
  • 我正在尝试删除等于第 156:171 行的命名范围“Remove_Allowances_Credits”。
  • DavidN 提供的答案解决了这个问题。

标签: excel vba count delete-row countif


【解决方案1】:

不完全确定您要达到的目标,所以我添加了一些不同的方法。

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

【讨论】:

  • 感谢您的回复!这个贡献者社区令人难以置信。感谢您的努力和帮助。
【解决方案2】:

尝试以下方法:

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 而不是COUNT。 COUNT 只计算数值。 COUNTA 计算所有非空值(包括#N/A)。

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

【讨论】:

  • 谢谢!这做到了!我是 VBA 新手,迫不及待地想变得更好,并为这个很棒的社区做出贡献。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多