在Excel中使用VBA检查是否有任何数据input到单元格范围内

我想在第一个空行(减去最后一列)被填充时调用一个Sub(New_Row)。 我在如何在If语句中引用一系列单元格时遇到了麻烦。

Sub Data_Added() 'Check if anything has been entered into the first empty row in "Data" Dim sht As Worksheet Dim LastRow As Long Dim LastColumn As Long Dim StartCell As Range Sheets("Data").Select Set sht = Worksheets("Data") Set StartCell = Range("A1").End(xlDown).Select Worksheets("Data").UsedRange LastRow = StartCell.SpecialCells(xlCellTypeLastCell).Row LastColumn = StartCell.SpecialCells(xlCellTypeLastCell).Column Set InputRange = sht.Range(StartCell, sht.Cells(LastRow + 1, LastColumn - 1)) If InputRange Is Not Nothing Then Call New_Row End If End Sub 

我见过使用Application.Intersect方法的人,但是我不确定一个单元格的交叉是否合理。 完全新的VBA,但是,所以我不知道。 现在我得到一个“对象的无效使用”错误,指向If语句中的“Nothing”。

 Dim y As Long, lastx As Long Dim sht As Worksheet y = 1 'Row you want to check Set sht = ThisWorkbook.Worksheets("Sheet1") lastx = sht.Cells(y, sht.Columns.Count).End(xlToRight).Column - 1 If WorksheetFunction.CountA(Range(Cells(y, 1), Cells(y, lastx))) <> 0 Then 'Call New_Row when the row you are checking is empty Call New_Row End If 

你有没有尝试过这样的事情?