Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 39

Thread: test if more than one year

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    test if more than one year

    I was wondering if it is at all possible to test for multiple years in two different ways

    1) If I have multiple dates in a column I would like to test if there are multiple years (ex: 2005 and 2006)
    2) If I use the dates in a pivottable I would like to test the pivottable column field of date for more than one year. Same as above

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I'm not sure on the Pivot Table portion, but for the first one, I would think a formula could do it. Say you have your dates in cells A1:A100:

    =if(year(max(A1:A100))<>year(min(A1:A100)),"Multiple years","Same year")

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    If your data is in column A and they are all dates, you could use something like this...


    =IF(SUMPRODUCT(--(YEAR(A2:A10)=2006))=COUNT(A2:A10),"Yes","No")


    Of course you would need to define the year (2006 in this example) and it assumes a range of only A2:A10.

    Is the year defined here, or are you looking for any difference?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by firefytr
    =IF(SUMPRODUCT(--(YEAR(A2:A10)=2006))=COUNT(A2:A10),"Yes","No")

    Of course you would need to define the year (2006 in this example) and it assumes a range of only A2:A10.

    Is the year defined here, or are you looking for any difference?
    Use the year of the first cell in the range

    =IF(SUMPRODUCT(--(YEAR(A2:A10)=YEAR(A2)))=COUNT(A2:A10),"Yes","No")

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Can I use that formula in VBA?

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Sure

    [vba]Range("B10").Formula = " =IF(SUMPRODUCT(--(YEAR(A2:A10)=YEAR(A2)))=COUNT(A2:A10),""Yes"",""No"")"[/vba]

    You may want to go to FormulaR1C1 if you want to set the ranges via code. In that case it would look like (for the same thing):
    [vba]Range("B10").Formula = "=IF(SUMPRODUCT(--(YEAR(R[-8]C[-1]:RC[-1])=YEAR(R[-8]C[-1])))=COUNT(R[-8]C[-1]:RC[-1]),""Yes"",""No"")"
    [/vba]

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I had thought of that Bob, but alas did not use it as the OP was (I didn't think) very specific. Of course MSFT still did not put in an ISDATE() function, which totally baffles my mind.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by firefytr
    I had thought of that Bob, but alas did not use it as the OP was (I didn't think) very specific. Of course MSFT still did not put in an ISDATE() function, which totally baffles my mind.
    Aah, I see what you mean. He could have meant multiple year dates, of soecifically 2005 and 2006, rather than multiple year dates.

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Exactly. Ambiguity at its best.

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If it were the former, what should be done if there were 2004 and 2006? Don't answer, I am sure it doesn't matter.

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I just assumed returning a cell value that said "multiple years" vs "single year" and figured the op would adjust as necessary.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    So did I, but you know that Zack is a bit odd!

  13. #13
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Well, no argument there.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  14. #14
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Quote Originally Posted by Ken Puls
    Sure

    [vba]Range("B10").Formula = " =IF(SUMPRODUCT(--(YEAR(A2:A10)=YEAR(A2)))=COUNT(A2:A10),""Yes"",""No"")"[/vba]

    You may want to go to FormulaR1C1 if you want to set the ranges via code. In that case it would look like (for the same thing):
    [vba]Range("B10").Formula = "=IF(SUMPRODUCT(--(YEAR(R[-8]C[-1]:RC[-1])=YEAR(R[-8]C[-1])))=COUNT(R[-8]C[-1]:RC[-1]),""Yes"",""No"")"
    [/vba]

    HTH,
    I like this it seems it will do what I am looking for. However is it possible, to have adjust to the size of the column. Since the column will always be different lengths.

  15. #15
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Sure can. Can you tell us what column your data is in, and what row it starts in?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  16. #16
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Nevermind,

    Again, assuming column A with data starting in row 2:

    [vba]With ActiveSheet
    .Range("B10").Formula = _
    "=IF(SUMPRODUCT(--(YEAR(A2:A" & .Cells(.Rows.Count, 1).End(xlUp).Row & _
    ")=YEAR(A2)))=COUNT(A2:A" & .Cells(.Rows.Count, 1).End(xlUp).Row & "),""Yes"",""No"")"
    End With[/vba]
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  17. #17
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by xld
    So did I, but you know that Zack is a bit odd!
    Hmm, could you elaborate a little on that Bob? You lost me at Zack...

  18. #18
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Ehrm... he said odd, not dense.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  19. #19
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Would this work as a VBA statement? I am trying to use it to say if more than 1 year then group the date by year. The rest I already have, I just need to figure out this last piece of the puzzle.

    [vba]IF(SUMPRODUCT(--(YEAR(A2:A" & .Cells(.Rows.Count, 1).End(xlUp).Row & ")=YEAR(A2)))
    _ =COUNT(A2:A" & .Cells(.Rows.Count, 1).End(xlUp).Row &") > 1 then[/vba]

  20. #20
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    It should, as long as you have it in a With statement that contains the sheet, as that is how you have it referenced. The logic is sufficient though. I know a good way to find out... test it!

Posting Permissions

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