如何删除 excel 末尾的空行

how to remove empty lines at the end of excel

我编译了一个 excel 实用程序来将一个大文件拆分成较小的文件,在我的例子中是 100k 行。然而,最后一个文件显示了 100k 行,尽管数据直到 100k 行才存在。如何删除最后一个分区文件末尾的空行。

这是我的代码。

   Set objWorkbook = objExcel.Workbooks.Open(filePath) 

  'Initialize data  
   Set ThisSheet = objWorkbook.ActiveSheet

  'Getting number of columns and rows for calculation
   NumOfColumns = ThisSheet.UsedRange.Columns.Count
   rowCount = ThisSheet.UsedRange.Rows.Count  

   WorkbookCounter = 1
   RowsInFile = 100000      

     If rowCount > 100000 Then
      ' extracting the file name alone.
       fName = Left(fileName, (InStrRev(fileName, ".") - 1)) ' file name  
      ' Copy the data of the first row (header)
      Set RangeOfHeader = ThisSheet.Range(ThisSheet.Cells(1, 1), ThisSheet.Cells(1, NumOfColumns))

     'Create the file system object for creating folders
     Set objFSO = CreateObject("Scripting.FileSystemObject")    

    'extracting the path name excluding the file name
     newPath = CStr(Left(filePath, InStrRev(filePath, "\")))     
     folderName = newPath & "Copy_of_" & fName & "\"     
     'Create the folders using objFSO
     'First check if folders exists and create only they dont exist
     if Not objFSO.FolderExists(folderName) Then      
       objFSO.CreateFolder(folderName)
     Else
       WScript.Echo "Folder already Exists"
   End If    

当我 select Ctrl+Shift+向下箭头.. 计数显示文件。当我单击 Ctrl+End 按钮时......控制转到第 100k 行......上面的许多行都是空白的。理想情况下,Ctrl+End 应该在数据结束处停止。

非常感谢您的指点,并提前致谢。

我不是 100% 确定,但我相信这会计算 excel 文件中分配的空行。

rowCount = ThisSheet.UsedRange.Rows.Count  

试试改用这个:

Sheets("Sheetname").Cells(.Rows.Count, "A").End(xlUp).Row