Count blank cells to avoid run-time error 1004

psi

New Member
Joined
Sep 16, 2012
Messages
5
I got a macro connected to a button that select empty cells within a range and fill them with the value above. The code looks like this:

Code:
Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Select
Selection.Resize(, 4).Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"

Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Select
Selection.Resize(, 4).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues

If you press the button twice you will get run-time error 1004 becuase Excel can't find any empty cells.

I want a function that counts blank cells before running the macro. If there are no blanks I don't want to execute the macro. That is to avoid run-time error 1004.

Can you guys help me out?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try

Rich (BB code):
Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Select
Selection.Resize(, 4).Select
If WorksheetFunction.CountBlank(Selection) = 0 Then Exit Sub
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"

Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Select
Selection.Resize(, 4).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
 
Upvote 0
Welcome to the board.

Try

Code:
Sub test()
With Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Resize(, 4)
    If Application.CountA(.Cells) > 0 Then
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End If
End With
End Sub
 
Upvote 0
scrap this ... my answer was wrong :(


So was mine, CountA should be changed to CountBlank
Code:
Sub test()
With Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Resize(, 4)
    If Application.CountBlank(.Cells) > 0 Then
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End If
End With
End Sub
 
Upvote 0
So was mine, ...

I wrote this one:
Code:
Range("A1:A10").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Value = ActiveCell.Offset(-1, 0).Value

but all it did was input the value of the first "one cell up" in all the empty spaces. And in the end - the same 1004 on the second press :)
 
Upvote 0
Try

Rich (BB code):
Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Select
Selection.Resize(, 4).Select
If WorksheetFunction.CountBlank(Selection) = 0 Then Exit Sub
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"

Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Select
Selection.Resize(, 4).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues

That did the trick!

I had something similar going on but I messed it up somewhere.

Thanks for all the replies guys! Much appreciated!
 
Upvote 0
Code:
Sub test()
With Range("A1:A10").SpecialCells(xlCellTypeConstants, 23).Resize(, 4)
    If Application.CountBlank(.Cells) > 0 Then
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End If
End With
End Sub

It seems like your code is better than my original code. I think I might use it instead. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top