• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Trouble with Workbook Function on my code|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Trouble with Workbook Function on my code|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosTrouble with Workbook Function on m…
sp_PrintTopic sp_TopicIcon
Trouble with Workbook Function on my code
Avatar
Kenneth McMillan
USA
Member
Members

Dashboards

Power Query
Level 0
Forum Posts: 29
Member Since:
November 15, 2017
sp_UserOfflineSmall Offline
1
October 26, 2018 - 2:08 am
sp_Permalink sp_Print

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

sp_AnswersTopicSeeAnswer See Answer
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
October 26, 2018 - 12:31 pm
sp_Permalink sp_Print

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

sp_AnswersTopicAnswer
Answers Post
Avatar
Kenneth McMillan
USA
Member
Members

Dashboards

Power Query
Level 0
Forum Posts: 29
Member Since:
November 15, 2017
sp_UserOfflineSmall Offline
3
October 26, 2018 - 10:35 pm
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
October 30, 2018 - 8:10 pm
sp_Permalink sp_Print

No worries Ken, glad to help.

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Sonja Mason, Denise Lloyd
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Raj Mattoo
Mark Luke
terimeri dooriyan
Jack Aston
AndyC
Denise Lloyd
michael serna
mashal sana
Tiffany Kang
Leah Gillmore
Forum Stats:
Groups: 3
Forums: 24
Topics: 6219
Posts: 27276

 

Member Stats:
Guest Posters: 49
Members: 31903
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.