用格式检测空行和列
我正在尝试使用VBScript检查任何空单元格的Excel工作表。 我写了以下简单的代码来检测电子表格中的行数和列数,然后查找它是否为null:
Set excelWorkbook = CreateObject("Excel.application") excelWorkbook.Application.WorkBooks.Open("My_Excel_Sheet_Name") excelWorkbook.Application.Visible = True WScript.Sleep 2000 Set excelSheet = excelWorkbook.Worksheets("Sheet1") UsedColumns = excelSheet.UsedRange.Columns.Count UsedRows = excelSheet.UsedRange.Rows.Count For columnVariable = 1 to UsedColumns For rowVariable = 1 to UsedRows if IsEmpty(excelSheet.Cells(rowVariable,ColumnVariable)) Then msgbox "Empty cell found at row: " + rowVariable + " column: " + ColumnVariable else msgbox "Data Found in this cell" End if Next Next Msgbox "Script completed, check logs for more details"
只要最后没有空行和列,这个脚本工作正常。 这个脚本失败的地方:
我在我的Excel工作表上做了Ctrl + Shift + End,它给了我几个额外的行和一列被格式化但不包含数据的列。 我无法编写代码来检测这些额外的单元格。
注意 :当我检查variablesUsedColumns的值,它返回6
和UsedRows为4
。 所以这段代码并没有parsing最后被格式化但没有数据的空行和列,因此我的代码失败了。
放弃Worksheet.UsedRange属性以支持Range.CurrentRegion属性 。
UsedColumns = excelSheet.Cells(1, 1).CurrentRegion.Columns.Count UsedRows = excelSheet.Cells(1, 1).CurrentRegion.Rows.Count
.CurrentRegion
是从参考单元(在这种情况下.Cells(1, 1)
单元( .Cells(1, 1)
向外扩展的数据的“岛”,它向所有方向扩展,直到遇到完全空白的行或列。