Wednesday, September 14, 2011

Fill Non Blank Cells From Above

If you want to fill the blank cell in range with data from non blank cells above. Snapshot Below-


Step 1

Select the range.



Step 2

Press F5 ->Special -> choose Blanks



Step 3.



VBA Solution


Sub blank_cells_from_above()
Dim fillrng As Range, cl As Range
Dim i, j, s As Long
' change the range here
Set fillrng = Range("a1:a16")
For i = 1 To fillrng.Columns.Count
For j = 1 To fillrng.Rows.Count
If fillrng(j, i).Value <> vbNullString Then
s = j
Exit For
End If
Next j
If Application.WorksheetFunction.CountBlank(Range(Cells(fillrng(s, i).Row, fillrng(j, i).Column), Cells(fillrng(1, i).Row + fillrng.Rows.Count - 1, fillrng(s, i).Column))) >= 1 Then
Range(Cells(fillrng(s, i).Row, fillrng(j, i).Column), Cells(fillrng(1, i).Row + fillrng.Rows.Count - 1, fillrng(s, i).Column)).SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
End If
Range(Cells(fillrng(s, i).Row, fillrng(j, i).Column), Cells(fillrng(1, i).Row + fillrng.Rows.Count - 1, fillrng(s, i).Column)).Copy
Cells(fillrng(s, i).Row, fillrng(j, i).Column).Select
Selection.PasteSpecial Paste:=xlPasteValues
Next i
Application.CutCopyMode = False

End Sub

No comments:

Post a Comment

Import data from SQL

Macro to import data from SQL using ADO connection string: Sub Import_data_from_SQL() ' Tools -> References -> Microsoft Active...