Results 1 to 3 of 3

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

Threaded View

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

    Code to return the 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!

    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!


    Sub Test()
    
      Dim WS As Worksheet
      Dim Source As Range, Dest As Range, rng As Range, cell As Range
      Dim highestValue As Double, secondHighestValue As Double
      
      Set rng = "D:D"
      highestValue = 0
      secondHighestValue = 0
    
      'Set the first cell
      Set Dest = ThisWorkbook.ActiveSheet.Range("D3")
     
      'Visit each sheet
      For Each WS In Workbooks("CAMPUS SHUTTLE TRACKING 2010-2011.xls").Worksheets
      'Find Highest Value
      For Each cell In rng
      
      Next cell
      
      If cell.Value > highestValue Then highestValue = cell.Value
      
      'Find Second Highest Value
      
       For Each cell In rng
      
      Next cell
      
      If cell.Value > secondHighestValue And cell.Value < highestValue Then secondHighestValue = cell.Value
      
        'Write the value into there
        Dest = cell.Value
        'Refer to the next cell below
        Set Dest = Dest.Offset(1)
      Next
    End Sub
    Last edited by craftyocelot; 03-02-2017 at 09:57 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. 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
  2. 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
  3. [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
  4. [SOLVED] Code to detect previous active workbook instead of current active workbook
    By kosherboy in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-25-2014, 01:58 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. [SOLVED] 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