我列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
值越來越進入空單元格,而不是一個在它下面。
添加一個if語句有什麼問題'Lrow = .Range( 「A」 和.Rows.Count).END(xlUp).row'? – findwindow
錯誤是因爲您在嘗試將其設置爲變量後正在使用'.Select'。改用'.Row'代替。但是,這可能不是(很可能不是)解決您的整體問題......這就是爲什麼你的錯誤正在發生。你可以嘗試'如果c.Value = txt_BPName1和c.Value <>「」然後' – BruceWayne