如何跳过列中的空单元格?

时间:2016-04-18 21:09:44

标签: excel vba excel-vba macros

我的专栏A的值类似于 KAT1 KAT2 空 kat3 我希望程序基本上跳过空单元格并使用kat3作为单元格来设置最后一行(lrow)。我正在使用的代码如下所示。 以下是修订后的代码:

Private Sub txt_BPName1_Exit(ByVal cancel As MSForms.ReturnBoolean)
 Dim ws As Worksheet
    Dim Lrow As Long
    Dim c As Range, rng As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")


    With ws

         Lrow = .Range("A" & .Rows.Count).End(xlUp).row
         'Lrow = .Range("A" & .Rows.Count, 1).End(xlUp).Select
         'Lrow = .Range("A65536").End(xlUp).row + 1
        Set rng = .Range("A2:A" & Lrow)

    End With

    For Each c In rng
        If c.Value = txt_BPName1 And c.Value <> "" Then
           MsgBox "Cell " & c.Address & " Duplicate Found."
              cancel = True


            Exit Sub
        End If
    Next

     MsgBox ("Base Product is not duplicate,Ok to Add")

     Cells(Lrow + 1, 1).Value = txt_BPName1.Text

     txt_BPName1.Text = ""
    ActiveCell.Offset(1, 0).Select

End Sub

该值将输入空单元格而不是其下方的单元格。

1 个答案:

答案 0 :(得分:1)

只需在循环中添加if语句

For Each c In rng
    If not "" = c.Value Then