首页 > 解决方案 > 如果范围包含大于 0 的值

问题描述

这个想法是计算一个范围内的单元格数量,并检查每个单元格的值是否大于 0,如果是,则键入“过期”,否则键入“非过期”有人可以帮我吗?

我下面的代码不起作用

错误 438/对象不支持此属性或方法

出现在 IF 开始的位置。

Option Explicit

Sub investigate()
    Dim wb1 As Workbook
    Dim w As String
    Dim Name1 As String
    Dim Path1 As String
    Dim Lr As Integer

    w = 2  
    Name1 = ThisWorkbook.Sheets("vba").Cells(w + 4, 1).Text
    Path1 = ThisWorkbook.Sheets("Path").Cells(1, 2) & "Download\"

    Set wb1 = Workbooks.Open(Path1 & Name1)

    Lr = wb1.Sheets("Sheet1").Range("V" & Application.Rows.Count).End(xlUp).Row

    If Application.WorksheetFunction.CountIf(wb1.Range("V" & Lr), ">" & 0) > 0 Then
        ThisWorkbook.Sheets("vba").Cells(w + 4, 2).Value = "Overdue"
    Else 
        ThisWorkbook.Sheets("vba").Cells(w + 4, 2).Value = "No Overdue"
    End If

    wb1.Close
End Sub

标签: excelvba

解决方案


我不确定你的代码会做你想做的事。如果您正在通过将重新计数保存在位置 Cells(w+ 4,2) 中来计算有多少过期以及有多少未过期,那么您将得到覆盖的值。

以下将在单元格(w+4,2)和单元格(w+5,2)中保存“过期”标题,因此它们不会重叠。在右侧的列中,您将找到这些付款的数量。

希望你喜欢

Option Explicit

Sub investigate()
    Dim wb1 As Workbook
    Dim w As Integer
    Dim Name1 As String
    Dim Path1 As String
    Dim Lr As Integer
    Dim overdueyes as long
    Dim overdueno as long

    w = 2  
    Name1 = ThisWorkbook.Sheets("vba").Cells(w + 4, 1).Text
    Path1 = ThisWorkbook.Sheets("Path").Cells(1, 2) & "Download\"

    Set wb1 = Workbooks.Open(Path1 & Name1)

    Lr = wb1.Sheets("Sheet1").Range("V" & Application.Rows.Count).End(xlUp).Row
    ThisWorkbook.Sheets("vba").Cells(w + 4, 2).Value = "Overdue"
    ThisWorkbook.Sheets("vba").Cells(w + 5, 2).Value = "No Overdue"
    for each cell in wb1.Sheets("Sheet1").range("V1:v" & lr)
        if cell.value > 0 then 
            overdueyes = overdueyes +1
        else
            overdueno = overdueyes +1
        end if
    next
    ThisWorkbook.Sheets("vba").Cells(w + 4, 3).Value = Overdueyes
    ThisWorkbook.Sheets("vba").Cells(w + 5, 3).Value = Overdueno

    wb1.Close
End Sub

推荐阅读