I have a procedure that I wrote to clear the empty rows and columns in a spreadsheet. The issue is it takes a lot longer than I'd like it to...my program is supposed to be a faster alternative to completing a task manually, and with this much needed procedure that task is not accomplished in a timely manner. Does anyone know of an easier way to accomplish this? Here's my code:
(objbook, objexcel, and objworksheet were all defined before the procedure is called)
All help is appreciated!Code:Public Sub ClearEmpties() Dim intIndex As Integer, intIndex2 As Integer Dim blnHasData As Boolean, blnDeleted As Boolean blnHasData = False blnDeleted = True intIndex = 0 intIndex2 = 0 objExcel.Visible = True 'Clear Rows Do Until blnDeleted = False DoEvents blnDeleted = False For intIndex = objWorksheet.Cells.SpecialCells(xlCellTypeLastCell).Row To 1 Step -1 DoEvents For intIndex2 = 1 To objWorksheet.Cells.SpecialCells(xlCellTypeLastCell).Column If objWorksheet.Cells(intIndex, intIndex2).Text <> "" Then blnHasData = True GoTo NextRow: End If Next intIndex2 If blnHasData = False Then objWorksheet.Rows(intIndex).Select objExcel.Selection.Delete Shift:=xlUp blnDeleted = True End If NextRow: blnHasData = False Next intIndex objBook.Save Loop blnHasData = False blnDeleted = True 'Clear Columns Do Until blnDeleted = False DoEvents blnDeleted = False For intIndex = objWorksheet.Cells.SpecialCells(xlCellTypeLastCell).Column To 1 Step -1 DoEvents For intIndex2 = 1 To objWorksheet.Cells.SpecialCells(xlCellTypeLastCell).Row If objWorksheet.Cells(intIndex2, intIndex).Text <> "" Then blnHasData = True GoTo NextCol: End If Next intIndex2 If blnHasData = False Then objWorksheet.Columns(intIndex).Select objExcel.Selection.Delete Shift:=xlLeft blnDeleted = True End If NextCol: blnHasData = False Next intIndex objBook.Save Loop End Sub




Reply With Quote