+ Reply to Thread
Results 1 to 8 of 8

Find lowest value in a common column between two worksheets and then name the worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2013
    Location
    Greenfield, IN
    MS-Off Ver
    Excel 2007
    Posts
    70

    Find lowest value in a common column between two worksheets and then name the worksheets

    I have two worksheets on one workbook currently listed as Sheet1 & Sheet2

    I have the same column (which is T) on each sheet which is a date (6/6/2013) value

    I would like a VBA script which will look at both sheets to figure out which sheet has the newest create date.
    I'm hoping that this will tell me which of the two sheets contains the newest data!

    Next I would like the script to name the newer of the worksheets (New Data) and of coruse the other worksheet (Old Data)

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Find lowest value in a common column between two worksheets and then name the workshee

    id have thought you could simply use a formula to identify the sheet and the date!
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    06-17-2013
    Location
    Greenfield, IN
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Find lowest value in a common column between two worksheets and then name the workshee

    I have a VBA code that is automating everything in the back end.

    I know this is very simple but I am a VBA idiot.

    I know it's something like:
    finding the lowest value in sheet1 row T and assigning it to a value
    finding the lowest value in sheet2 row T and assigning it to a SECOND value
    Compare value 1 and value 2 and based on the lowest, changing the worksheet name

    I know what I think NEEDS to happen... I have tried a few times but I can't get past the first line SUB change_ws_name ()

  4. #4
    Registered User
    Join Date
    06-17-2013
    Location
    Greenfield, IN
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Find lowest value in a common column between two worksheets and then name the workshee

    I hate that I am sooo stupid with this!

    So far I have:

    Dim sht1 As Range
    Dim sht2 As Range
    Dim sValue1 As Variant
    Dim sValue2 As Variant
         
    With Sheets("Sheet1")
        sht1 = .Range("T2:T")
    and I am ALREADY getting an error!

  5. #5
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Find lowest value in a common column between two worksheets and then name the workshee

    tests all of col t sheet1 and sheet2 for max and renames sheet1 and sheet2 only if they both exist
    Sub testd()
    'tests all of col t sheet1 and sheet2 for max and renames sheet1 and sheet2 only if they both exist
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        On Error Resume Next
        Set ws2 = Sheets("sheet2")
        Set ws1 = Sheets("sheet1")
        If Err.Number <> 0 Then
            MsgBox "sheet1 or sheet2 do not exist exiting macro"
            
    Exit Sub
    End If
        
       
        
    
    Set myrange1 = Worksheets("Sheet1").Range("t:t") 'change here if you just want to check 1 cell code could be a lot shorter if it is one cell only tho
    Set myrange2 = Worksheets("Sheet2").Range("t:t")  'and here eg Range("t1") or range("t2:t1000") 
    firstws = WorksheetFunction.Max(myrange1)
    secondws = WorksheetFunction.Max(myrange2)
    
    
    If firstws = secondws Then
    MsgBox ("max date is same on both ws.  Exiting macro")
    Exit Sub
    End If
    If firstws > secondws Then
    Sheets("Sheet1").Name = "data"
    Sheets("Sheet2").Name = "old data"
    End If
    If firstws < secondws Then
    Sheets("Sheet1").Name = "old data"
    Sheets("Sheet2").Name = "data"
    End If
        
      
    End Sub
    Last edited by martindwilson; 07-13-2013 at 12:07 PM.

  6. #6
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Find lowest value in a common column between two worksheets and then name the workshee

    well the code i gave you does what i think you want not what you asked for !
    the lowest value is the oldest date the highest will be the newest date

  7. #7
    Registered User
    Join Date
    06-17-2013
    Location
    Greenfield, IN
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Find lowest value in a common column between two worksheets and then name the workshee

    I must have been posting as you posted that code.

    Is there a good book to learn VBA for the total new developer.
    I am a complete idiot right now when it comes to this!

    I can't wait to try it out and will let you know what I find!

    THANK YOU!!!!

  8. #8
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Find lowest value in a common column between two worksheets and then name the workshee

    no idea! i just had a look and patched it together with a few google searches i'm sure there is much better code!

+ 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. Find common id in worksheets and eliminate unnecessary lines of data
    By Therese in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-09-2013, 07:51 AM
  2. [SOLVED] Combining several worksheets with one common column (primary key)
    By MelindaCapri in forum Excel General
    Replies: 1
    Last Post: 05-09-2013, 03:05 PM
  3. [SOLVED] Find lowest value in corresponding cells on multiple worksheets. Return value & location.
    By WORKINGBEE in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-21-2013, 01:33 PM
  4. Replies: 2
    Last Post: 02-19-2013, 06:48 PM
  5. Replies: 5
    Last Post: 04-17-2012, 12:28 PM

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