循环并检查单元格是否为空 - 未注册单元格为空

时间:2015-10-06 18:01:46

标签: excel vba excel-vba

这是我的代码:

For Each Cell In NewBook.Sheets(1).Range("T2:T" & lastRow)
If Cell.Value = 76 Or Cell.Value = 69 Then
    MsgBox Cells(Cell.Row, 8).Value
    If NewBook.Sheets(1).Cells(Cell.Row, 8).Value = "" Then
            Do Until IsEmpty(NewBook.Sheets(1).Cells(Cell.Row, col).Value)
                If Not NewBook.Sheets(1).Cells(Cell.Row, col).Value = "" Then
                col = col + 1
                End If
            Loop
            NewBook.Sheets(1).Cells(Cell.Row, col) = "Codes 69 or 76 Reported w/ No applicable Bankrupcty Rptd"
    End If
End If
col = 50
Next Cell

我也尝试过:

For Each Cell In NewBook.Sheets(1).Range("T2:T" & lastRow)
If Cell.Value = 76 Or Cell.Value = 69 Then
    MsgBox Cells(Cell.Row, 8).Value
    If IsEmpty(NewBook.Sheets(1).Cells(Cell.Row, 8).Value) = True Then
            Do Until IsEmpty(NewBook.Sheets(1).Cells(Cell.Row, col).Value)
                If Not NewBook.Sheets(1).Cells(Cell.Row, col).Value = "" Then
                col = col + 1
                End If
            Loop
            NewBook.Sheets(1).Cells(Cell.Row, col) = "Codes 69 or 76 Reported w/ No applicable Bankrupcty Rptd"
    End If
End If
col = 50
Next Cell

cells(cell.row, 8).value确实是""时,两者都不会注册。不知道该怎么做。

2 个答案:

答案 0 :(得分:1)

这可能与您的Cell变量有关。强烈建议不要将该字用作变量,因为它在VB中使用。另外,使用With减少一点长度:

For Each cel In NewBook.Sheets(1).Range("T2:T" & lastRow)
If cel.Value = 76 Or cel.Value = 69 Then
    MsgBox Cells(cel.Row, 8).Value
    With NewBook.Sheets(1)
    If IsEmpty(.Cells(cel.Row, 8).Value) = True Then
            Do Until IsEmpty(.Cells(cel.Row, col).Value)
                If Not .Cells(cel.Row, col).Value = "" Then
                col = col + 1
                End If
            Loop
            .Cells(cel.Row, col) = "Codes 69 or 76 Reported w/ No applicable Bankrupcty Rptd"
    End If
    End With
End If
col = 50
Next cel

End Sub

主要是,尝试将Cell变量更改为cel(或您喜欢的任何内容)并查看其是否有效。 With部分只是个人偏好。

答案 1 :(得分:1)

问题在于原始数据。它来自CSV文件,而cell.value不是""。它是" &#34 ;.讨厌的空间。