Microsoft Excel - Can't select Blank Cells properly

Asked By Jason
10-Nov-11 12:42 PM
So what I'm trying to do is fill in any blank cells with the cell value above them.  Here is my code so far.  It works sometimes but other times the last cell value doesn't fill down to the bottom of the range selected.  I went through the code step by step and I found out that it's failing at the Selection.SpecialCells(xlCellTypeBlanks).Select line.  For some reason it doesn't select the correct blank cells all of the time... Any ideas why?


Sub FillDownCells()
filldownresponse = MsgBox("This will paste values.  Are you sure you want to continue?", vbYesNo + vbQuestion)
    If filldownresponse = vbNo Then
    Exit Sub
    Else
    GoTo filldownresume
    End If
filldownresume:
With Selection
jasonrowtop = .Row
jasonfilldowncolumn = .Column
jasonrowbottom = .Row + .Rows.Count - 1
End With
On Error GoTo jasonhelp
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
Range(Cells(jasonrowtop, jasonfilldowncolumn), Cells(jasonrowbottom, jasonfilldowncolumn)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

Exit Sub
jasonhelp:
MsgBox "You didn't select a blank cell"
End Sub
  wally eye replied to Jason
10-Nov-11 04:49 PM
It looks to me that if you don't have the right cells selected to start with, it might only find the bottom row in the selected column.  You might try using UsedRange:

With Selection
jasonrowtop = .Row
jasonfilldowncolumn = .Column
jasonrowbottom = .parent.usedrange.rows(.parent.usedrange.rows.count).row
End With

It gets a bit wordy, but essentially it returns the last row of the used range.  Taking this long way is a bit cleaner than just using the parent.usedrange.rows.count because there might be unused rows above the used range and they wouldn't be included in the rows.count property.
  Pichart Y. replied to Jason
10-Nov-11 11:16 PM
Hi Jason,

I haven't seen your spreadsheet, but from the code attached, I think that the selected range is not cover all your data. 


    With Selection
    jasonrowtop = .Row
    jasonfilldowncolumn = .Column
    jasonrowbottom = .Row + .Rows.Count - 1 
'The line above should be wrong both column reference and row reference.


I want to suggest you to attach your file here so that I have more clear of your work...

pichart Y.

  Anoop S replied to Jason
11-Nov-11 12:58 AM
Fill Blanks Via a Macro

To use this, go to Tools>Macro>Visual Basic Editor (Alt+F11) then to Insert>Module and then paste in the code below.

Sub FillBlanks()
Dim rRange1 As Range, rRange2 As Range
Dim iReply As Integer
    If Selection.Cells.Count = 1 Then
      MsgBox "You must select your list and include the blank cells", _
        vbInformation, "OzGrid.com"
        Exit Sub

    ElseIf Selection.Columns.Count > 1 Then
      MsgBox "You must select only one column", _
        vbInformation, "OzGrid.com"
        Exit Sub
    End If

    Set rRange1 = Range(Selection.Cells(1, 1), _
      Cells(65536, Selection.Column).End(xlUp))

    On Error Resume Next
    Set rRange2 = rRange1.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If rRange2 Is Nothing Then
      MsgBox "No blank cells Found", _
        vbInformation, "OzGrid.com"
      Exit Sub
    End If
    rRange2.FormulaR1C1 = "=R[-1]C"
    iReply = MsgBox("Convert to Values", vbYesNo + vbQuestion, "OzGrid.com")
    If iReply = vbYes Then rRange1 = rRange1.Value
End Sub

After pasting in the above code, click the top right X to get back to Excel and Save. Now go to Tools>Macro>Macros (Alt+F8) select FillBlanks and click Run, or Options to assign a shortcut key.
  Jason replied to wally eye
11-Nov-11 04:24 PM
I tried this method but I still have a problem that the last value in the column doesn't fill down to the last cell in the range that I've selected. 
  Jason replied to Anoop S
11-Nov-11 04:49 PM
I also tried your method Anoop and I have the same problem that the last value in the column doesn't fill down to the last cell of the range that I've selected.  For example lets say I have a  1 in cell b2 and a 2 in cell b4 for simplicity.  Then I select the range b2:b8.  I would want a 1 to be filled into cells b2 and b3.  I would also want the value 2 to be filled in cells b4 to b8.  Obviously I'm normally working with a greater number of values but thats the concept.  Thank you for your help so far though, it is much appreciated.
  wally eye replied to wally eye
12-Nov-11 05:22 PM
Alrighty then, this one works:

Public Sub FillDownCells()
 
  Dim arrFormulas     As Variant
 
  Dim intReply        As Integer
  Dim lngCurrRow      As Long
 
  intReply = MsgBox("This will paste values.  Are you sure you want to continue?", _
    vbYesNo + vbQuestion)
  If intReply = vbYes Then
    arrFormulas = Selection.FormulaR1C1
    For lngCurrRow = LBound(arrFormulas) To UBound(arrFormulas)
      If arrFormulas(lngCurrRow, 1) = "" Then
        arrFormulas(lngCurrRow, 1) = "=R[-1]C"
      End If
    Next lngCurrRow
    Selection = arrFormulas
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
      SkipBlanks:=False, Transpose:=False
  End If
 
End Sub

It reads the entire selection into an array, scrolls through the array finding the blanks and replacing them with your formula.  Then it posts the array back to the spreadsheet, and copies/pastes special the values.  The problem with the original procedure was when it selected the blank cells, it didn't recognize cells past the UsedRange.  This version doesn't care what the UsedRange is...
  wally eye replied to Jason
12-Nov-11 05:51 PM
You'll see this faster if I reply to you rather than myself...
  Jason replied to wally eye
17-Nov-11 12:18 PM
Haha thanks for noticing that.  Code worked perfectly.  Thanks for all your help.

Jason
Create New Account
help
Copying range of formulas to anotehr workbook Excel Excel 2007: I have a large range of cells with formulas I want to copy to the sheet to the destination workbook, only to a new workbook. Anyone know how? Thanks Excel Discussions Microsoft Excel (1) Excel 2007 (1) Worksheets (1) Workbook (1) VB (1) Resize (1) Shift (1) Range (1) I it appear). . . Workbooks("Example Workbook.xls").Worksheets("Sheet1").Range("C3").Resize( _ Selection.Rows.Count, Selection.Columns.Count).Formula = Selection.Formula Next, change the name of my destination workbook from my "Example
Replacing 0 value with empty Excel I need a vba code to replace all the cells having 0 value with empty As Long rowstart = Selection.Row rowend = Selection.Rows.Count + rowstart - 1 colstart = Selection.Column colend = Selection.Columns.Count + colstart - 1 For c = colstart To colend For r = rowstart To rowend If Cells(r, c) = 0 Then Cells(r, c) = "" Next r Next c End Sub Excel Miscellaneous Discussions Microsoft Excel (1) Selection.Columns.Count (1) Selection.Rows.Count (1) Workbooks (1) Sheets (1) Macro (1) Selection.Replace
How to copy large block of cells but paste into one column Excel Hi How can I take a large block of data (A1:Y300 which is 24 the 7200 data cells all in one column from say B1:B7200. thank you!! Craig Excel Worksheet Discussions Microsoft Excel (1) Application.CutCopyMode (1) Selection.Columns.Count (1) Selection.Rows.Count (1) Sheets (1) ActiveSheet.Paste (1) TRANSPOSE (1) Macro (1) Sub 4. run the macro Sheets("Sheet2").Columns(2).ClearContents With ActiveSheet For i = 0 To Selection.Columns.Count - 1 For j = 1 To Selection.Rows.Count counter = counter + 1 Sheets("Sheet2
XML File Excel Hello What is the process for creating an XML file from a spreadsheet? Excel Programming Discussions Selection.Columns.Count (1) Selection.Rows.Count (1) Excel (1) Selection.Cells (1) FNum (1) CNdx (1) RNdx (1) TheSave (1) Using Excel's XML maps can be tricky and you need the maps to use the Save RNdx = 1 To Selection.Rows.Count Print #FNum, Space(4) & "<" & RowName & "> " For CNdx = 1 To Selection.Columns.Count Print #FNum, Space(8) & _ Selection.Cells(RNdx, CNdx) & "< / " & _ Selection.Cells(1
Hidden Cells Excel I have the following code: Sub HideAroundSelection() Dim intRows As Integer Dim intCols As Integer As Range Dim rngBelow As Range Dim rngLeft As Range intRows = Selection.Rows.Count intCols = Selection.Columns.Count 'MsgBox ("introws = " & intRows & vbLf & "intcols = " & intCols) With Selection Set rngAbove = .Cells(1, 1).Offset(-1 It still runs a little slow. Is there a way to speed it up? Thanks Excel Programming Discussions Columns.EntireColumn.Hidden (1) Cells.EntireColumn.Hidden (1) ActiveSheet.Columns.Count (1) Cells.EntireRow.Hidden (1) ActiveSheet.Rows.Count (1) Rows.EntireRow.Hidden (1) Selection.Columns.Count (1) Cells.Columns.Hidden (1) Providing a description of what the code is supposed to