P. 1


|Views: 0|Likes:
Macro in excel
Macro in excel

More info:

Published by: D'Artagnan Raghavendra on Jan 31, 2014
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less





Sub DeleteBlankRows(Optional WorksheetName As Variant) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' DeleteBlankRows ' This function will delete all blank rows on the worksheet

' named by WorksheetName. This will delete rows that are ' completely blank (every cell = vbNullString) or that have ' cells that contain only an apostrophe (special Text control ' character). ' The code will look at each cell that contains a formula, ' then look at the precedents of that formula, and will not ' delete rows that are a precedent to a formula. This will ' prevent deleting precedents of a formula where those ' precedents are in lower numbered rows than the formula ' (e.g., formula in A10 references A1:A5). If a formula ' references cell that are below (higher row number) the ' last used row (e.g, formula in A10 reference A20:A30 and ' last used row is A15), the refences in the formula will ' be changed due to the deletion of rows above the formula. ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim RefColl As Collection RowNum As Long Prec As Range Rng As Range DeleteRange As Range LastRow As Long FormulaCells As Range Test As Long WS As Worksheet PrecCell As Range

If IsMissing(WorksheetName) = True Then Set WS = ActiveSheet Else On Error Resume Next Set WS = ActiveWorkbook.Worksheets(WorksheetName) If Err.Number <> 0 Then ''''''''''''''''''''''''''''''' ' Invalid worksheet name. ''''''''''''''''''''''''''''''' Exit Sub End If End If If Application.WorksheetFunction.CountA(WS.UsedRange.Cells) = 0 Then '''''''''''''''''''''''''''''' ' Worksheet is blank. Get Out. '''''''''''''''''''''''''''''' Exit Sub End If '''''''''''''''''''''''''''''''''''''' ' Find the last used cell on the ' worksheet. '''''''''''''''''''''''''''''''''''''' Set Rng = WS.Cells.Find(what:="*", after:=WS.Cells(WS.Rows.Count, WS.Columns.Cou nt), lookat:=xlPart, _ searchorder:=xlByColumns, searchdirection:=xlPrevious, MatchCase:=False)

' add row R to the DeleteRange. Add the row number ' of each precedent to the RefColl ' collection. If it is .Rows(RowNum) ) End If Else '''''''''''''''''''''''''' ' R is in the collection. ''''''''''''''''''''''''''''''''''''' For RowNum = LastRow To 1 Step -1 Set FormulaCells = Nothing If Application. Cells that contain ' only an apostrophe are counted ' by CountA.Row Set RefColl = New Collection ''''''''''''''''''''''''''''''''''''' ' We go from bottom to top to keep ' the references intact. Add it to ' the DeleteRange variable. and for ' each cell with a formula.Clear Else ''''''''''''''''''''''''''''''''''''' ' CountA > 0.LastRow = Rng.CountA(WS. ' Do nothing.Union(DeleteRange. ''''''''''''''''''''''''''''''''''''' If IsRowClear(RowNum:=RowNum) = True Then ''''''''''''''''''''''''''''''''' ' Row contains nothing but blank ' cells or cells with only an ' apostrophe. Find the cells ' containing formula. See if R is in the RefColl ' reference Collection. find ' its precedents.Rows(RowNum)) = 0 Then '''''''''''''''''''''''''''''''''''' ' There are no non-blank cells in ' row R.Number <> 0 Then '''''''''''''''''''''''''' ' R is not in the RefColl ' collection.Rows(RowNum) Else Set DeleteRange = Application. WS. preventing ' #REF errors. If not.WorksheetFunction. '''''''''''''''''''''''''' If DeleteRange Is Nothing Then Set DeleteRange = WS. so we use IsRowClear ' to test for only apostrophes. '''''''''''''''''''''''''''''''''''' On Error Resume Next Test = RefColl(CStr(RowNum)) If Err. '''''''''''''''''''''''''' End If On Error GoTo 0 Err. ' Test if this row is in the ' RefColl collection.

''''''''''''''''''''''''''''''''''''''''''''''''''' On Error Resume Next For Each Rng In FormulaCells.Cells For Each Prec In Rng.Row) Next Prec Next Rng On Error GoTo 0 End If End If End If ''''''''''''''''''''''''' ' Go to the next row. ' moving upwards. '''''''''''''''''''''''''''''''''''''' Else If DeleteRange Is Nothing Then Set DeleteRange = WS.Rows(Row Num)) End If End If Else On Error Resume Next Set FormulaCells = Nothing Set FormulaCells = WS. Loop through the formula ' cells.Cells RefColl. and for each cell.SpecialCells(xlCellTypeFormulas) On Error GoTo 0 If FormulaCells Is Nothing Then ''''''''''''''''''''''''' ' No formulas found.Row. add it ' to the DeleteRange. delete them.Add Item:=Prec. Do ' nothing.Rows(RowNum) Else Set DeleteRange = Application.Number = 0 Then '''''''''''''''''''''''''''''''''''''' ' Row exists in RefColl.Precedents. find its precedents ' and add the row number of each precedent cell ' to the RefColl collection. WS. ' Do not delete the row. That means ' a formula is referencing this row. ''''''''''''''''''''''''''''''''' On Error Resume Next Test = RefColl(CStr(RowNum)) If Err. ''''''''''''''''''''''''' Else ''''''''''''''''''''''''''''''''''''''''''''''''''' ' Formulas found. '''''''''''''''''''''''''''''''''''''''''' If Not DeleteRange Is Nothing Then . key:=CStr(Prec.Union(DeleteRange.' not in the collection. ''''''''''''''''''''''''' Next RowNum '''''''''''''''''''''''''''''''''''''''''' ' If we have rows to delete.Rows(RowNum).

ColNdx).Value <> vbNullString) Then IsRowClear = False Exit Function End If Set Rng = Cells(RowNum. '''''''''''''''''''''''''''''''''''''''''''''''''' Dim ColNdx As Long Dim Rng As Range ColNdx = 1 Set Rng = Cells(RowNum.HasFormula = True) Or (Rng. It returns False ' if the row contains only data or formulas.EntireRow.Count If (Rng.End(xlToRight) ColNdx = Rng.Column Loop IsRowClear = True End Function . ColNdx) Do Until ColNdx = Columns.DeleteRange.Delete shift:=xlShiftUp End If End Sub Function IsRowClear(RowNum As Long) As Boolean '''''''''''''''''''''''''''''''''''''''''''''''''' ' IsRowClear ' This procedure returns True if all the cells ' in the row specified by RowNum as empty or ' contains only a "'" character.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->