Deletes Blank Rows


/ Published in: VB.NET
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Sub DeleteBlankRows(Optional WorksheetName As Variant)
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. ' DeleteBlankRows
  4. ' This function will delete all blank rows on the worksheet
  5. ' named by WorksheetName. This will delete rows that are
  6. ' completely blank (every cell = vbNullString) or that have
  7. ' cells that contain only an apostrophe (special Text control
  8. ' character).
  9. ' The code will look at each cell that contains a formula,
  10. ' then look at the precedents of that formula, and will not
  11. ' delete rows that are a precedent to a formula. This will
  12. ' prevent deleting precedents of a formula where those
  13. ' precedents are in lower numbered rows than the formula
  14. ' (e.g., formula in A10 references A1:A5). If a formula
  15. ' references cell that are below (higher row number) the
  16. ' last used row (e.g, formula in A10 reference A20:A30 and
  17. ' last used row is A15), the refences in the formula will
  18. ' be changed due to the deletion of rows above the formula.
  19. '
  20. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  21.  
  22. Dim RefColl As Collection
  23. Dim RowNum As Long
  24. Dim Prec As Range
  25. Dim Rng As Range
  26. Dim DeleteRange As Range
  27. Dim LastRow As Long
  28. Dim FormulaCells As Range
  29. Dim Test As Long
  30. Dim WS As Worksheet
  31. Dim PrecCell As Range
  32.  
  33. If IsMissing(WorksheetName) = True Then
  34. Set WS = ActiveSheet
  35. Else
  36. On Error Resume Next
  37. Set WS = ActiveWorkbook.Worksheets(WorksheetName)
  38. If Err.Number <> 0 Then
  39. '''''''''''''''''''''''''''''''
  40. ' Invalid worksheet name.
  41. '''''''''''''''''''''''''''''''
  42. Exit Sub
  43. End If
  44. End If
  45.  
  46.  
  47. If Application.WorksheetFunction.CountA(WS.UsedRange.Cells) = 0 Then
  48. ''''''''''''''''''''''''''''''
  49. ' Worksheet is blank. Get Out.
  50. ''''''''''''''''''''''''''''''
  51. Exit Sub
  52. End If
  53.  
  54. ''''''''''''''''''''''''''''''''''''''
  55. ' Find the last used cell on the
  56. ' worksheet.
  57. ''''''''''''''''''''''''''''''''''''''
  58. Set Rng = WS.Cells.Find(what:="*", after:=WS.Cells(WS.Rows.Count, WS.Columns.Count), lookat:=xlPart, _
  59. searchorder:=xlByColumns, searchdirection:=xlPrevious, MatchCase:=False)
  60.  
  61. LastRow = Rng.Row
  62.  
  63. Set RefColl = New Collection
  64.  
  65. '''''''''''''''''''''''''''''''''''''
  66. ' We go from bottom to top to keep
  67. ' the references intact, preventing
  68. ' #REF errors.
  69. '''''''''''''''''''''''''''''''''''''
  70. For RowNum = LastRow To 1 Step -1
  71. Set FormulaCells = Nothing
  72. If Application.WorksheetFunction.CountA(WS.Rows(RowNum)) = 0 Then
  73. ''''''''''''''''''''''''''''''''''''
  74. ' There are no non-blank cells in
  75. ' row R. See if R is in the RefColl
  76. ' reference Collection. If not,
  77. ' add row R to the DeleteRange.
  78. ''''''''''''''''''''''''''''''''''''
  79. On Error Resume Next
  80. Test = RefColl(CStr(RowNum))
  81. If Err.Number <> 0 Then
  82. ''''''''''''''''''''''''''
  83. ' R is not in the RefColl
  84. ' collection. Add it to
  85. ' the DeleteRange variable.
  86. ''''''''''''''''''''''''''
  87. If DeleteRange Is Nothing Then
  88. Set DeleteRange = WS.Rows(RowNum)
  89. Else
  90. Set DeleteRange = Application.Union(DeleteRange, WS.Rows(RowNum))
  91. End If
  92. Else
  93. ''''''''''''''''''''''''''
  94. ' R is in the collection.
  95. ' Do nothing.
  96. ''''''''''''''''''''''''''
  97. End If
  98. On Error GoTo 0
  99. Err.Clear
  100. Else
  101. '''''''''''''''''''''''''''''''''''''
  102. ' CountA > 0. Find the cells
  103. ' containing formula, and for
  104. ' each cell with a formula, find
  105. ' its precedents. Add the row number
  106. ' of each precedent to the RefColl
  107. ' collection.
  108. '''''''''''''''''''''''''''''''''''''
  109. If IsRowClear(RowNum:=RowNum) = True Then
  110. '''''''''''''''''''''''''''''''''
  111. ' Row contains nothing but blank
  112. ' cells or cells with only an
  113. ' apostrophe. Cells that contain
  114. ' only an apostrophe are counted
  115. ' by CountA, so we use IsRowClear
  116. ' to test for only apostrophes.
  117. ' Test if this row is in the
  118. ' RefColl collection. If it is
  119. ' not in the collection, add it
  120. ' to the DeleteRange.
  121. '''''''''''''''''''''''''''''''''
  122. On Error Resume Next
  123. Test = RefColl(CStr(RowNum))
  124. If Err.Number = 0 Then
  125. ''''''''''''''''''''''''''''''''''''''
  126. ' Row exists in RefColl. That means
  127. ' a formula is referencing this row.
  128. ' Do not delete the row.
  129. ''''''''''''''''''''''''''''''''''''''
  130. Else
  131. If DeleteRange Is Nothing Then
  132. Set DeleteRange = WS.Rows(RowNum)
  133. Else
  134. Set DeleteRange = Application.Union(DeleteRange, WS.Rows(RowNum))
  135. End If
  136. End If
  137. Else
  138. On Error Resume Next
  139. Set FormulaCells = Nothing
  140. Set FormulaCells = WS.Rows(RowNum).SpecialCells(xlCellTypeFormulas)
  141. On Error GoTo 0
  142. If FormulaCells Is Nothing Then
  143. '''''''''''''''''''''''''
  144. ' No formulas found. Do
  145. ' nothing.
  146. '''''''''''''''''''''''''
  147. Else
  148. '''''''''''''''''''''''''''''''''''''''''''''''''''
  149. ' Formulas found. Loop through the formula
  150. ' cells, and for each cell, find its precedents
  151. ' and add the row number of each precedent cell
  152. ' to the RefColl collection.
  153. '''''''''''''''''''''''''''''''''''''''''''''''''''
  154. On Error Resume Next
  155. For Each Rng In FormulaCells.Cells
  156. For Each Prec In Rng.Precedents.Cells
  157. RefColl.Add Item:=Prec.Row, key:=CStr(Prec.Row)
  158. Next Prec
  159. Next Rng
  160. On Error GoTo 0
  161. End If
  162. End If
  163.  
  164. End If
  165.  
  166. '''''''''''''''''''''''''
  167. ' Go to the next row,
  168. ' moving upwards.
  169. '''''''''''''''''''''''''
  170. Next RowNum
  171.  
  172.  
  173. ''''''''''''''''''''''''''''''''''''''''''
  174. ' If we have rows to delete, delete them.
  175. ''''''''''''''''''''''''''''''''''''''''''
  176.  
  177. If Not DeleteRange Is Nothing Then
  178. DeleteRange.EntireRow.Delete shift:=xlShiftUp
  179. End If
  180.  
  181. End Sub
  182.  
  183. Function IsRowClear(RowNum As Long) As Boolean
  184. ''''''''''''''''''''''''''''''''''''''''''''''''''
  185. ' IsRowClear
  186. ' This procedure returns True if all the cells
  187. ' in the row specified by RowNum as empty or
  188. ' contains only a "'" character. It returns False
  189. ' if the row contains only data or formulas.
  190. ''''''''''''''''''''''''''''''''''''''''''''''''''
  191. Dim ColNdx As Long
  192. Dim Rng As Range
  193. ColNdx = 1
  194. Set Rng = Cells(RowNum, ColNdx)
  195. Do Until ColNdx = Columns.Count
  196. If (Rng.HasFormula = True) Or (Rng.Value <> vbNullString) Then
  197. IsRowClear = False
  198. Exit Function
  199. End If
  200. Set Rng = Cells(RowNum, ColNdx).End(xlToRight)
  201. ColNdx = Rng.Column
  202. Loop
  203.  
  204. IsRowClear = True
  205.  
  206. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.