Forum

Trouble with Workbo...
 
Notifications
Clear all

Trouble with Workbook Function on my code

4 Posts
2 Users
0 Reactions
64 Views
(@kwesmc1)
Posts: 31
Trusted Member
Topic starter
 

I have a sheet at work that I am trying to access another workbook I open from Outlook, so I have 2 workbooks open, my main Workbook is where the code resides, and the other open book I open from an email Excel file. Both books are open before I run the code.

I am trying to access the open book I open from an email, the copy a certain range, go back to my original workbook and paste it in a  named range  specified in an input box, and then close the book I opened from email.
I open the workbook where the code is first and then I open the attachment, then I go back to where the code resides and run by button. There are six named ranges in my main workbook  which means there will be six email attachments.
I will input the name of each of the six sheets with the store name of each attachment and the six named ranges are each store name, ie... Riverside; Knox; Bloomfield and three other store names. There is no other name in any of the files other than the street name.
The problem I'm having is this "Workbooks(2).Activate" does not activate the workbook I open from my email attachment to copy the range selected. I know it is something I'm doing, but I'm getting really frustrated.
Can you please tell me what I am doing wrong with my code? 
I would sincerely appreciate any help with this.

Sub CopyRange()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
    
 
 
 Dim wbkOtherWorkbook As Workbook
  
   Dim wbMyBook As Workbook
   Set wbMyBook = ActiveWorkbook
    Set objOutlook = GetObject(, "Outlook.Application")
   
    Dim MySelection As Range
    Set MySelection = Application.InputBox(prompt:="Select a Store Name ie... Riverside", Type:=8)
    MySelection.Select
    Workbooks(1).Activate

Workbooks(2).Activate
Sheets(1).Range("A:K").Copy
Workbooks(1).Activate
     Selection.PasteSpecial xlPasteValuesAndNumberFormats
          Dim Wb As Workbook
Workbooks(2).Close savechanges:=False
   
    Range("B5").Select
Application.ScreenUpdating = False
Application.DisplayAlerts = False
 
   End Sub

Thanks,
Ken Mc

 
Posted : 26/10/2018 12:08 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Ken,

Are you sure the workbook from email is Workbook(2)?

When you specify the workbook by number you have to be sure it is what you think it is.

To test, I opened a workbook from my hard drive, then opened a wb which was an attachment to an Outlook email.  Then went back to my first workbook and ran your code.

Workbooks(1) is my PERSONAL.XLSB, Workbooks(2) is the wb I opened from the hard drive and Workbook(3) is the wb I opened from Outlook.

Do you have other workbooks open, maybe a PERSONAL.XLSB?

If you go into the VBA editor, how many workbooks does it show?

Once you have your 2 workbooks open, try running this to see what Workbooks(2) is

  Sub Listwb()

     Dim count As Long
     count = 1

          Do While count < 4

               Debug.Print Workbooks(count).Name

               Workbooks(count).Activate
               count = count + 1

          Loop

  End Sub

 

The other thing it could be - is editing enabled on the workbook you open from Outlook?  If you open it and don't click the 'Enable editing' warning, the workbook won't appear in the Workbooks() collection.

 

Can I also make a few observations which will help you write better code.  I don't know if you've sent me just some of your code so some of what I say you may just discard.

objOutlook is not declared, I'm using Option Explicit so I don't try to use variables I haven't explicitly declared.  

Also, once you set objOutlook you don't use it again. Not a programming error, but why create it if it's not used?

This bit of code doesn't do anything

    Set MySelection = Application.InputBox(prompt:="Select a Store Name ie... Riverside", Type:=8)
    MySelection.Select

Once someone has entered a store name, and you've selected it, nothing else happens to that selected range.

 

The Workbooks(1).Activate statement is redundant because right after it you activate Workbooks(2)

     Workbooks(1).Activate

     Workbooks(2).Activate

 

Do you need to select this?

     Range("B5").Select

 

You've turned off ScreenUpdating and DisplayAlerts at the top but not turned them back on again at the bottom of the code.

I also find it useful to declare all your variables at the top of the sub, not as you go along through the code.

Please see the attached wb and see if my amended code works for you.

Cheers

Phil

 
Posted : 26/10/2018 10:31 pm
(@kwesmc1)
Posts: 31
Trusted Member
Topic starter
 

Thanks very much Phil. I really appreciate what you sent.
Yes, I have a lot to learn and thanks so much for setting me straight on better coding. It really helped!
Warmest Regards,
Ken Mc

 
Posted : 27/10/2018 8:35 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

No worries Ken, glad to help.

Regards

Phil

 
Posted : 31/10/2018 6:10 am
Share: