Run-time error 1004

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
781
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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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,215,239
Messages
6,123,818
Members
449,127
Latest member
Cyko

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