Run time error 1004 - Autofill method of range class failed.

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Please can some help me?

I have the following code that works fine until I try it towards the end of the range.

Code:
If Not Intersect(Target, Range("D23:BM23")) Is Nothing Then   
 If Target.Value = "Y" Then
    Target.Offset(, 1) = "C"
    Target.Offset(, 1).AutoFill Range(Target.Offset(, 1), "BM" & Target.Row)
End If
End If

So basically it is for a monthly check to be carried out at work. Once the check has been done that cell is entered with a Y and the rest of the range automatically enter a C and then it doesn't need to be touched again until the next month.

But if the check hasn't been done before column BL which is the day shift on the last day of the month, it comes up with the error in the title.

When I end the error message the code has done what it should of done, so does anyone know why the error message is popping up?

Any help would be much appreciated.

Thanks

Dan
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Ok can you paste the entire code you are using back here and ill take a look.
 
Upvote 0
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)



    Dim sCurrentMonth As String
    Dim dCurrentTime As Double
    Dim iCurrentDate As Integer
    Dim rng As Range
    Dim rTbl As Range
    Dim rTbl2 As Range
    Dim rTbl3 As Range
    Dim rFound As Range
    Dim bComplete As Boolean
    Dim rng2 As Range, c As Range
    
         
'exit if more than one cell is selected
If Target.CountLarge > 1 Then Exit Sub
              
              
              
'-----DOESNT ALLOW THE DELETE OR BACKSPACE KEY TO WORK
  If Not Intersect(Target, Range("A1:BM5,A6:C24,D21:BM22")) Is Nothing Then
   SetOnKey xlOn
  Else
    SetOnKey xlOff
  End If




    'reset form closed flag
    bClosedUserForm1 = False
    
    'get current month and set its shift row
    sCurrentMonth = Format(Date, "mmmm")
    'get current home row
    Set rFound = Cells.Find(What:=sCurrentMonth, LookIn:=xlFormulas, LookAt:=xlPart)
    If Not rFound Is Nothing Then
        nCurrentShiftRow = rFound.Row + iRowsBelowMonth
    Else
        MsgBox "Current month not found."
        Exit Sub
    End If


    'get current system time
    dCurrentTime = TimeValue(Now)


    'get current day number
    iCurrentDate = Day(Date)


    'get current day column - because the date columns are merged it returns the first column of the merge
    Set rTbl = Range("D4:BM4")
    Set rFound = rTbl.Find(What:=iCurrentDate, LookIn:=xlFormulas, LookAt:=xlWhole)
    If Not rFound Is Nothing Then
        'get shift "D" = 06:00 to 18:00 or "N" = 18:00 to 06:00
'Original Line>>>>>>If Not dCurrentTime > 0.75 And Not dCurrentTime < 0.25 Then
        If Not dCurrentTime > 0.73 And Not dCurrentTime < 0.23 Then
            nTargetColumn = rFound.Column
        Else
            If dCurrentTime <= 0.23 Then
                nTargetColumn = rFound.Column - 1 'must be midnight to 06:00 on previous date "N"
            ElseIf dCurrentTime > 0.73 Then
                nTargetColumn = rFound.Column + 1 'must be 18:00 to midnight on current date "N"
            End If
        End If
    Else
        MsgBox "Current date not found.", vbInformation, "Palletiser Operator"
        Exit Sub
    End If


    'ensure only current date column and rows 5 to 19 can be processed
    If Target.Column = nTargetColumn Then
        If Not Target.Row > nCurrentShiftRow + iNumOfCheckRows + 1 And Not Target.Row < nCurrentShiftRow + 1 Then


            Application.EnableEvents = False


            'unprotect sheet to clear cell colours caused by previous missing entry
            UnprotectTheActiveSheet
            Set rTbl2 = Range(Cells(nCurrentShiftRow, nTargetColumn), Cells(nCurrentShiftRow + iNumOfCheckRows, nTargetColumn))
            rTbl2.Interior.Color = xlNone
            'ensure any remaining colours are cleared if shift changes
            Set rTbl3 = Range(Cells(nCurrentShiftRow, nTargetColumn - 1), Cells(nCurrentShiftRow + iNumOfCheckRows, nTargetColumn - 1))
            rTbl3.Interior.Color = xlNone


            'check 'Initials' row has been selected
            If Target.Row = nCurrentShiftRow + iNumOfCheckRows + 1 Then


                'initialise flag
                bComplete = True


                'check all required current shift cells have been completed
                'if incomplete, identify cell, send user message and exit safely
                For Each rng In rTbl2
                    If rng = "" Then
                        Cells(nCurrentShiftRow, nTargetColumn).Select
                        Cells(nCurrentShiftRow, nTargetColumn).Interior.Color = RGB(155, 194, 230)
                        rng.Interior.Color = RGB(255, 0, 0)
                        ProtectTheActiveSheet
                        Application.EnableEvents = True
                        MsgBox "Please complete where highlighted.", vbInformation, "Palletiser Operator"
                        bComplete = False
                        rng.Select
                        Exit Sub
                    End If
                Next rng


                'call initials form if all required entries are complete
                If bComplete Then Call DisplayUserForm1ForSheetsThatNeedInitialsFromUserForm(Target)


                'reprotect wsheet
                ProtectTheActiveSheet
            End If


            Application.EnableEvents = True
        End If
    Else
        'if user selects a non current date when initials form is not loaded
        If Not bClosedUserForm1 Then
            ActiveWindow.ScrollRow = nCurrentShiftRow - 2
            Cells(nCurrentShiftRow, nTargetColumn).Select
            UnprotectTheActiveSheet
            Cells(nCurrentShiftRow, nTargetColumn).Interior.Color = RGB(255, 0, 255)
            ProtectTheActiveSheet
            MsgBox "Please select the current date and shift column indicated.", vbInformation, "Palletiser Operator"
            ActiveCell.Offset(1, 0).Select
            
        End If
    End If
                Application.EnableEvents = True
                
                                                         
              
'----- IF Y ENTEREND IN THE MONTHLY CHECK THEN THE REST OF THE MONTH FILL WITH C
Set rng2 = Intersect(Target, Range("D23:BL23"))


If Not rng2 Is Nothing Then
    For Each c In rng2
        If UCase(c.Value) = "Y" Then
            Application.EnableEvents = False
                Range(Cells(23, c.Column + 1), Cells(23, "BM")) = "C"
            Application.EnableEvents = True
        End If
    Next
End If

End Sub
 
Upvote 0
You need to do the entering of the Y as a worksheet change event. The trigger for your macro is just moving to the cell you are about to enter the Y into. It hasnt yet got a Y so nothing happens. Cut that bit of code out and put it into a worksheet change and it will work.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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