Run-time error 1004

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
786
Office Version
  1. 365
Hi,

I have the code below in outlook 2016 to open an excel file it has been working fine, the last two days is giving me the run-time error 1004 "Activate method of Range class failed" and highlight the following line of code:

VBA Code:
  xExcelRange.Activate

here the complete code:

VBA Code:
Public Sub OpenVendorInvoiceTracking()
  Dim xExcelFile As String
    Dim xExcelApp As Excel.Application
    Dim xWb As Excel.Workbook
    Dim xWs As Excel.Worksheet
    Dim xExcelRange As Excel.Range
    xExcelFile = "\\mtlnas01\share\Accountant Files\AP Team\Jose\NCL_XLSM\2020 Accounts Payable Checklist - NCL.xlsm"
    Set xExcelApp = CreateObject("Excel.Application")
    Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
    Set xWs = xWb.Sheets(1)
    xWs.Activate
    Set xExcelRange = xWs.Range("A1")
    xExcelRange.Activate
    xExcelApp.Visible = True
    xWb.Windows(1).WindowState = xlMaximized

Thank you,
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Is cell A1 hidden or protected?
 
Upvote 0
Check that you don't have any hidden or very hidden sheets.
 
Upvote 0
Are you doing in Excel VBA, or some other application's VBA?
I ask because I noticed your variable declarations are a bit curious:
VBA Code:
   Dim xExcelApp As Excel.Application
    Dim xWb As Excel.Workbook
    Dim xWs As Excel.Worksheet
    Dim xExcelRange As Excel.Range
It usually isn't necessary to preface Worksbook, Worksheet, and Range with "Excel." if you are working from Excel VBA.
 
Upvote 0
Trying placing this line just above the line causing the error, and let us know what it returns when you run the code again:
VBA Code:
MsgBox xWb.Sheets(1).Name
The value that it returns, is that sheet visible?
Is there anything special about cell A1 on that sheet (merged, protected, etc)?
 
Upvote 0
Trying placing this line just above the line causing the error, and let us know what it returns when you run the code again:
VBA Code:
MsgBox xWb.Sheets(1).Name
The value that it returns, is that sheet visible?
Is there anything special about cell A1 on that sheet (merged, protected, etc)?

Hi,

I did

it returned

Jul

which one of the sheets that is hidden been like that alway never had an issue with it.

thanks.
 
Upvote 0
I did

it returned

Jul

which one of the sheets that is hidden been like that alway never had an issue with it.
Well there is your problem right there. It is trying to activate a hidden sheet, which it cannot do.
You need to be careful about using references like "Sheets(1)".

If you want to select the first visible worksheet, you can do something like this:
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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