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,
 
Thanks.

I checked the link I am not that good in VBA don't how to modify it that i don't get that error, any help please?
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi

I checked the link but I am not that good in vba and don't know how to use that sample to modify the my code, any suggestion please how to, much appreciated.

thanks.
 
Upvote 0
Then we need you to explain to us how we can determine which sheet the codes needs to activate.
How many sheets are there?
How many sheets are hidden/unhidden?
How can we identify which sheet the code should run against?
 
Upvote 0
Hi

There are 14 sheets all together (Jan-Dec, Summary) that are hidden plus one more sheet call Contents (Like Table of Index) which should be one to activate because from that one I select which sheet to open.

Thank you.
 
Upvote 0
Does the sheet you need to run this against always have the same name?
If so, just change this line:
VBA Code:
Set xWs = xWb.Sheets(1)
to this:
VBA Code:
Set xWs = xWb.Sheets("Contents (Like Table of Index)")
putting whatever the sheet name is in between the doublt-quotes.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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