检查是否在Excel中使用VBA输入了一系列单元格中的数据

时间:2016-07-11 18:07:54

标签: excel vba excel-vba

我正在尝试在填充第一个空行(减去最后一列)时调用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”。

1 个答案:

答案 0 :(得分:2)

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

你尝试过这样的事吗?