Run-time Error on Finding Next Blank Cell in Named Range

Marhier

Board Regular
Joined
Feb 21, 2017
Messages
128
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Good afternoon.
I've written a bit of code that copies the cell you've got selected, switches over to a schedule and then pastes that value in the next available cell in a named range of cells.

My code is as follows:
Code:
Sub CopytoSchucoSchedule()
    Dim FindBlank As Range
    Application.ScreenUpdating = False
    ActiveSheet.Select
    Selection.Copy
    With SchucoSchedule
        .Visible = -1
        .Select
    Set FindBlank = Range("SchucoDescriptionText").Find(What:="", lookat:=xlWhole)
    FindBlank.Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    End With
    With SchucoSchedule
        .Visible = 2
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub


When running this code, I'm getting the following error:

"Run-time error '1004':
Application-defined or object-defined error"


By stepping through (F8), it seems to fail at the following line of code:
Code:
Set FindBlank = Range("SchucoDescriptionText").Find(What:="", lookat:=xlWhole)


If someone could provide a solution to this, it would be greatly appreciated.
Thank you.
Regards
Martin
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I don't see anything wrong with that line. If there are no blank cells in the named range you would get a run-time error 91, not 1004. Where is the variable "SchucoSchedule" defined?
 
Upvote 0
Hi JoeMo, thanks for the reply.
"SchucoSchedule" is just the hardcode name of the worksheet I'm pasting the information to... In that worksheet, I have the named range of cells "SchucoDescriptionText" where I want it to find the next available blank cell.

Thank you.
Regards
Martin
 
Upvote 0
Maybe check if the found range was not nothing before using it?

e.g.
Code:
'Find methods, https://www.excelcampus.com/vba/find-last-row-column-cell/
Sub tFind()
  Dim r As Range
  Set r = Range("A10:C10").SpecialCells(xlCellTypeBlanks)
  If Not r Is Nothing Then Debug.Print r(1).Address
End Sub
 
Upvote 0
Thanks Kenneth.
I changed my code as you suggested to the following:
Code:
Sub CopytoSchucoSchedule()
    Dim r As Range
    Application.ScreenUpdating = False
    ActiveSheet.Select
    Selection.Copy
    With SchucoSchedule
        .Visible = -1
        .Select
    Set r = Range("SchucoDescriptionText").SpecialCells(xlCellTypeBlanks)
    If Not r Is Nothing Then Debug.Print r(1).Address
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    End With
    With SchucoSchedule
        .Visible = 2
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub


Still getting the following error:

"Run-time error '1004':
Application-defined or object-defined error"

Thank you.
Regards
Martin
 
Upvote 0
Thanks Kenneth.
I changed my code as you suggested to the following:
Code:
Sub CopytoSchucoSchedule()
    Dim r As Range
    Application.ScreenUpdating = False
    ActiveSheet.Select
    Selection.Copy
    With SchucoSchedule
        .Visible = -1
        .Select
    Set r = Range("SchucoDescriptionText").SpecialCells(xlCellTypeBlanks)
    If Not r Is Nothing Then Debug.Print r(1).Address
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    End With
    With SchucoSchedule
        .Visible = 2
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub


Still getting the following error:

"Run-time error '1004':
Application-defined or object-defined error"

Thank you.
Regards
Martin
What line is highlighted when you get the error?
 
Upvote 0
Code:
Set r = Range("SchucoDescriptionText").SpecialCells(xlCellTypeBlanks)
 
Upvote 0
I should also mention that due to other functions I have running in this document, this particular piece of code has to sit within a worksheet and not a module.
My original code works fine in a module, just not in a worksheet.


Regards
Martin
 
Upvote 0
Code:
Set r = Range("SchucoDescriptionText").SpecialCells(xlCellTypeBlanks)
That error arises b/c there are no truly blank cells in the named range "SchucoDescriptionText". Kenneth neglected to handle this error in his code.
 
Upvote 0
Hi Joe.
I'm not sure I understand what you mean.
I can assure you the cells within the named range "SchucoDescriptionText" are blank to start with, but want the code to copy across the selection from one sheet to the next available blank cell in that named range.

As I mentioned before, the following code works fine when in a 'Module', but not within a worksheet - which for this particular supplier - needs to stay in the worksheet:

Code:
Sub CopytoSchucoSchedule()
    Dim FindBlank As Range
    Application.ScreenUpdating = False
    ActiveSheet.Select
    Selection.Copy
    With SchucoSchedule
        .Visible = -1
        .Select
    Set FindBlank = Range("SchucoDescriptionText").Find(What:="", lookat:=xlWhole)
    FindBlank.Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    End With
    With SchucoSchedule
        .Visible = 2
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

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