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
 
Click the formula tab then click Name Manager, find SchucoDescriptionText. What is in the Scope column?

Also what do the 2 formula below give you?
=ROWS(SchucoDescriptionText)*COLUMNS(SchucoDescriptionText)
=COUNTA(SchucoDescriptionText)
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If the named range is not in the sheet where you run the code, be sure to prefix the Range with the Worksheet. e.g.
Code:
'Sheet1 code, range named SchucoDescriptionText is in Sheet2
Sub Test()
  Debug.Print Sheet2.Range("SchucoDescriptionText").Address(external:=True)
End Sub

Select and Activate don't work in Sheet code per se. Seems like I got it to work once but it is best to be more explicit. My usual practice is to avoid Select and Activate and such when possible. If you want to use such, do it in a Module, not Sheet code. You can then call the routine from a Sub if needed.

If needed, I can show you how to add Error checks if you use the SpecialCells() method.
 
Last edited:
Upvote 0
Hi Joe.

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:
Guess I missed that point. See Kenneth's post (#12) about qualifying the sheet your named range is in.
 
Last edited:
Upvote 0
Ah right, I understand what you mean!
No, my code wasn't in the sheet the range was in... Doing what Kenneth suggested and qualifying the sheet name worked a treat.

Just amended my original code to the following:

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

Thank you to everyone who contributed.
This has helped my understanding massively.
I'm slowly getting there.

Have a good day all.
Regards
Martin
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
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