简体   繁体   English

如果isblank(cell)然后恢复下一个VBA

[英]If isblank(cell) then resume next VBA

Good afternoon all, 大家下午好

I am writing a code that do the following: find column "x", then find column "y", then in column "z" take the difference between x and y. 我正在编写执行以下操作的代码:找到列“ x”,然后找到列“ y”,然后在列“ z”中求出x和y之间的差。

In other words, the cell in Z1 will be: X1-Y1 and so on. 换句话说,Z1中的单元格为:X1-Y1,依此类推。

Now, the columns x and y are not always values, but there are also dates and empty cells. 现在,x和y列并不总是值,但是也有日期和空单元格。 I maanged to solve the problem with dates and strings by using on error resume next so the code just skips it. 我想通过on error resume next使用on error resume next来解决日期和字符串的问题,因此代码只是跳过了它。

But I cant find a way to handle blank cells. 但是我找不到处理空白单元格的方法。 I tried to use if isblank(cell) = true then resume next 我尝试使用if isblank(cell) = true then resume next

With the code as today, if vba finds a blank cell in x and y, it just takes the difference from 0 to 0, so in column z will be displayed 0, which is not. 使用今天的代码,如果vba在x和y中找到一个空白单元格,则只需取0到0之间的差,因此在z列中将显示0,而不会。

The part of the code that gives me problem is this: 代码中给我带来问题的部分是:

For z = 1 To lastrow
    For j = rownum + 1 To finalrow

        On Error Resume Next

        ws.Cells(j, lastcol + 2 + z).Value = ws.Cells(j, acol).Value - ws.Cells(j, colnum + z - 1).Value
        ws.Cells(j, lastcol + 2 + z).NumberFormat = "0.0"

    Next j
Next z

I would need the code to firstly see if in column x OR y there is a blank cell. 我需要代码首先查看在x或y列中是否有空白单元格。 If this is the case, then go to next iteration. 如果是这种情况,请转到下一个迭代。

I tried to write something like this but it gives me error: 我试图写这样的东西,但它给了我错误:

For z = 1 To lastrow
    For j = rownum + 1 To finalrow

        On Error Resume Next
        if isblank(ws.Cells(j, lastcol + 2 + z).Value)=true then resume next

        ws.Cells(j, lastcol + 2 + z).Value = ws.Cells(j, acol).Value - ws.Cells(j, colnum + z - 1).Value
        ws.Cells(j, lastcol + 2 + z).NumberFormat = "0.0"

    Next j
Next z

end if

The variables are well settled and the code works without any problem (if ran without the isblank statement) 变量已很好地设置,并且代码可以正常工作(如果在没有isblank语句的情况下运行)

Thank you! 谢谢!

You could try this: 您可以尝试以下方法:

For z = 1 To lastrow
    For j = rownum + 1 To finalrow
        if ws.Cells(j, acol).Value = "" or ws.Cells(j, colnum + z - 1).Value = "" then 
            'One or more Cells are null
        else
            ws.Cells(j, lastcol + 2 + z).Value = ws.Cells(j, acol).Value - ws.Cells(j, colnum + z - 1).Value
            ws.Cells(j, lastcol + 2 + z).NumberFormat = "0.0"
        end if
    Next j
Next z

IsBlank() does not exist in VBA. IsBlank()在VBA中不存在。 It is an Excel function. 这是一个Excel函数。 However, there are plenty of ways to check whether a range is blank. 但是,有很多方法可以检查范围是否为空白。 A possible way is to Trim() the range and see whether it is equal to "" : 一种可能的方法是Trim()范围,看看它是否等于""

For z = 1 To lastRow
    For j = rownum + 1 To finalrow
        If Trim(ws.Cells(j, lastcol + 2 + z)) = "" Or _
                Not IsNumeric(ws.Cells(j, lastcol + 2 + z)) Then
            ws.Cells(j, lastcol + 2 + z) = ws.Cells(j, acol) - ws.Cells(j, colnum + z - 1)
            ws.Cells(j, lastcol + 2 + z).NumberFormat = "0.0"
        End If
    Next j
Next z

As a rule of thumb in VBA - whenever you feel like writing On Error Resume Next , most probably there is a better way to write the whole piece, by avoiding it. 作为VBA的经验法则,每当您想编写On Error Resume Next ,很可能有一种更好的方法来避免编写整篇文章。 The On Error Resume Next ignores all errors in the code and forces it to continue, thus errors that may appear are ignored. On Error Resume Next忽略代码中的所有错误并强制其继续执行,因此,可能出现的错误将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM