Error 1004 no cells found

Arcadian

Board Regular
Joined
Jul 27, 2004
Messages
111
Hi, I've searched for a sollution but it doesn't work, or I'm not good at fixing it :)

Anyway, I have a bit of VBA code that at some point cycles through a method (or function, or sub) several times. In that method I have the following line of code:

VBA Code:
NextFree = Worksheets("Gebouw_eisen_nieuw").Range("E1:E" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row

I use this to find the first empty cell in column E after which several rows of data are copied from one sheet to the other (not shown in code here).
The number of rows are different with every cycle. It can be 7, or 26 or 15, etc.

It works fine for about the first three or four times it cycles through. After that I get the Error 1004 No cells found error, and NextFree is 0.
I've tried using On Error Resume Next and that does something, but it just starts pasting to row 1 and below with every following cylcle, when it should be pasting to row 70, or 65, or 81 or whatever the first empty row is.

Below is the full code. I hope someone can help.

VBA Code:
Private Sub CommandButton1_Click()
    kopie_relevante_eisen CheckBox1
    kopie_relevante_eisen CheckBox2
    kopie_relevante_eisen CheckBox3
    kopie_relevante_eisen CheckBox4
    kopie_relevante_eisen CheckBox5
    kopie_relevante_eisen CheckBox6
    kopie_relevante_eisen CheckBox7
   
    Application.ScreenUpdating = True
    UserForm4.Hide

End Sub

VBA Code:
Public Sub kopie_relevante_eisen(cb As MSForms.CheckBox)
    Dim c As Range
    Dim NextFree As Long
    Dim t As String
   
    If cb.Value = True And cb.Tag = 0 Then
        t = cb.Caption

        'This works for the first 3 or 4 cycles, probably depending on the total number of rows copied, after that Error 1004
        NextFree = Worksheets("Gebouw_eisen_nieuw").Range("E1:E" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
       
            For Each c In Worksheets("Eisen").Range("D2:D184")
                If c.Value = t Then
                   
                    With Worksheets("Gebouw_eisen_nieuw").Range("E" & NextFree)
                        .Value = c.Offset(0, -2).Value
                        .Offset(0, -1).Value = t
                        .Offset(0, 1).Value = c.Offset(0, 2).Value
                        .Offset(0, -2).Value = c.Offset(0, -1).Value
                    End With
                    NextFree = NextFree + 1
                End If
            Next c
           
        End If
       
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Is there ever a time when Column E is Empty?
 
Upvote 0
This is how I find the next Empty Cell
VBA Code:
Sub NextEmptyRow()
Dim wb As Workbook, sht As Worksheet, lRow As Integer
Set wb = ThisWorkbook: Set sht = wb.Sheets(1) 'Change Sheets() to suit your needs
lRow = sht.Columns(1).Rows(sht.Rows.Count).End(xlUp).Row 'Change Columns() to suit your needs
If lRow = 1 And IsEmpty(sht.Columns(1).Cells(lRow)) Then 'Change Coumns() to suit your needs
    lRow = 1
Else
    lRow = lRow + 1
End If
End Sub
 
Upvote 0
Solution
This is how I find the next Empty Cell
VBA Code:
Sub NextEmptyRow()
Dim wb As Workbook, sht As Worksheet, lRow As Integer
Set wb = ThisWorkbook: Set sht = wb.Sheets(1) 'Change Sheets() to suit your needs
lRow = sht.Columns(1).Rows(sht.Rows.Count).End(xlUp).Row 'Change Columns() to suit your needs
If lRow = 1 And IsEmpty(sht.Columns(1).Cells(lRow)) Then 'Change Coumns() to suit your needs
    lRow = 1
Else
    lRow = lRow + 1
End If
End Sub
This works perfectly. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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