2016-04-18 34 views
0

我列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

添加一個if語句有什麼問題'Lrow = .Range( 「A」 和.Rows.Count).END(xlUp).row'? – findwindow

+0

錯誤是因爲您在嘗試將其設置爲變量後正在使用'.Select'。改用'.Row'代替。但是,這可能不是(很可能不是)解決您的整體問題......這就是爲什麼你的錯誤正在發生。你可以嘗試'如果c.Value = txt_BPName1和c.Value <>「」然後' – BruceWayne

回答

1

只需在你的循環

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

我個人翻轉,使它更直觀一點,'如果c.Value <>「」然後...'除了「如果不是空白就是C值」與「如果c值不是空白,那麼」 – BruceWayne

+0

@ Abhi0609 - 您可以改爲將該註釋編輯到OP中嗎?閱讀評論中的代碼很困難。 – BruceWayne

+0

@布魯斯韋恩:OP編輯。 – Abhi0609

相關問題