如果范围的值大于0

时间:2019-07-01 10:28:51

标签: excel vba

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

我下面的代码不起作用

  

错误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

1 个答案:

答案 0 :(得分:0)

我不太确定您的代码会执行您想要的操作。如果您想通过将重新计数保存在位置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