Run-time error 1004

josros60

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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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