+ Reply to Thread
Results 1 to 16 of 16

Code to return 2nd highest value from each tab of another workbook to current workbook

  1. #1
    Registered User
    Join Date
    03-01-2017
    Location
    State College, PA
    MS-Off Ver
    2013
    Posts
    7

    Code to return 2nd highest value from each tab of another workbook to current workbook

    Hello! I'm extremely new to macros and codes and have only run a few successfully. I apologize for any glaring mistakes! This is also a repost since my original post seems to be locked (I forgot to utilize code tags the first time around).

    I'm trying to come up with a code to return the 2nd highest value from the "D" column of each tab of another workbook to my current workbook. Please see my code below. I pieced it together from other codes that I've used in the past and another code that I found online. I keep getting errors left and right when I try to alter the code. When I try to use the one below, I get a "Compile Error: Type Mismatch" popup. Can anyone help me fix this code/come up with a better one? Thank you!


    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    Quote Originally Posted by craftyocelot View Post
    I'm trying to come up with a code to return the 2nd highest value from the "D" column of each tab of another workbook to my current workbook.
    Try this. It's another way of getting the second highest value.

    Please Login or Register  to view this content.
    Replace your_workbook.xls with the name of the workbook you want to retrieve the second highest value in Column D from. (You should probably use the full path.)
    Replace your_sheetname with the name of the first sheet in the workbook, that you need the value from.

    The second highest value should then be listed in Cell D3.

    This worked for me with my test data. If that works for you, you can duplicate it for the other sheets in that same workbook, and list those values wherever you want.

  3. #3
    Registered User
    Join Date
    03-01-2017
    Location
    State College, PA
    MS-Off Ver
    2013
    Posts
    7

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    Thank you! Is there a way to have it automatically draw the data from every sheet in each workbook when I run the macro? I have years of data to go through and luckily they're all in the same format. However each year has its own workbook and each month within each workbook has its own sheet.

  4. #4
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    I'm about to step out the door so this will be quick.

    The answer to your question is most certainly. If you have a whole bunch of workbooks, and a bunch of sheets within each workbook, then yes, your original idea of looping through each worksheet in each of the workbooks is best. Otherwise you'd have to hard-code everything. (Hardcode all of the workbook names and hard-code all of the worksheet names.) You really don't want to do it that way.

    A double next FOR NEXT loop should work, something like:
    Please Login or Register  to view this content.
    A FOR EACH type of loop works too. (That's often just a matter of preference.)

  5. #5
    Registered User
    Join Date
    03-01-2017
    Location
    State College, PA
    MS-Off Ver
    2013
    Posts
    7

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    I'm out of the office right now but will definitely try this when I can and report back. Thank you so much!

  6. #6
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    I can post the entire code when I get home this evening, assuming no one else beats me to it.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    Try this for starters.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    bakerman, that looks very good. Good work. It's also probably a few lines less code than what I would have come up with. Hopefully that will work for crafty.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    However each year has its own workbook and each month within each workbook has its own sheet.
    I wonder if it would be handy to have the year (workbook name?) and month (sheet name?) in cells beside each 2nd highest value?
    I don't have to wonder however about bakerman2 being able to change his code slightly to accommodate that if indeed wanted!!!!!!!

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    @ Ed_Collins

    Thanks for rep+.

    @ jolivanes

    You are toooooooo good. It warms my heart.
    It had also crossed my mind for adding that. We'll see what OP has to say.

  11. #11
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    Isn't it a little early in the morning there to have a warm heart?
    Saying that, it depends on where you're at. Could be a warm pub on the Keyserlei.
    BTW, a reputation was added. That was long overdue

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    A warm place in front of my oven is more likely.

    Thanks for kind words and rep+.

  13. #13
    Registered User
    Join Date
    03-01-2017
    Location
    State College, PA
    MS-Off Ver
    2013
    Posts
    7

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    Apologies for the extremely delayed response. I was away for a conference and just remembered I had this project on the back burner.

    I just tried to run the macro but nothing seemed to have happened. I changed the file path for mydir to that of the file that I'm trying to extract the data from, saved, tried to run the macro in my main workbook, and nothing. No error box. Nothing. Again I'm really new at this so please feel free to explain it to me like I'm 5. >.>

    Thank you!

    Also gave Rep+ for the effort. :]

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    So if I iunderstand correctly it's just 1 file you want to open and from that file check column D from every tab in it and list all these values in current workbook Column ??

  15. #15
    Registered User
    Join Date
    03-01-2017
    Location
    State College, PA
    MS-Off Ver
    2013
    Posts
    7

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    Sorry for the jumbled parameters! I have seven years of data to go through so I have seven files. Each year's file/workbook has twelve sheets (one for each month of that fiscal year). I also just realized the column doesn't matter for this so really I just need to obtain the second highest number found in each sheet.

    My current workbook has a sheet for each fiscal year I'm reviewing. I was hoping for a macro that would grab the second highest number from each sheet in a fiscal year workbook and spit it out into the sheet that I have open. I figured I would just have to switch out the fiscal year file name in mydir for each fiscal year file but I may just be misunderstanding something very simple. :[

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Code to return 2nd highest value from each tab of another workbook to current workbook

    After reading your last post I stand by my code from Post#7. It does exactly what you require.
    Please Login or Register  to view this content.
    Just make sure that
    1. Adjust path to correct directory.
    2. Workbook for results isn't in the same directory as fiscal year books.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Code to return the 2nd highest value from each tab of another workbook to current workbook
    By craftyocelot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2017, 09:57 AM
  2. In Current workbook- copy sheets from other workbooks and save in current workbook
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-23-2016, 08:41 PM
  3. VBA code to copy data from current workbook to another workbook and send email
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-05-2015, 06:47 AM
  4. [SOLVED] Opening new workbook and pasting values into current workbook - code issues
    By Stew1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2014, 07:56 AM
  5. Replies: 9
    Last Post: 07-27-2012, 06:16 AM
  6. Code to open a workbook , copy info and return it to another workbook using a cell v.
    By england79m in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-31-2008, 11:22 AM
  7. Place code from current workbook to new workbook
    By Qaspec in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-08-2005, 11:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1