Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 42

Thread: 2007 compatibility

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    2007 compatibility

    I came across this problem, which I think may be quite common in opening pre 2007 workbooks from Excel 2007

    [VBA]LastRow = Workbooks("Test.xls").Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
    [/VBA]
    In this case, Rows.Count can take the value of 1048576 from my 2007 workbook, which causes an error as it does not exist in Test.

    Fully qualify the Range and the Rows.count by changing to
    [VBA]
    Sub Test()
    With Workbooks("Test.xls").Sheets(1)
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Many thanks for the info. I'm upgrading today and any anomilies like this will be very helpful to anyone upgrading.

    Please continue this sticky!

    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Contributor CaptRon's Avatar
    Joined
    Mar 2007
    Location
    Austin, Texas
    Posts
    141
    Location
    Discovered this characteristic about Excel 2007 shortly after our office converted to Office 2007. Thought I'd share it.

    The workbooks I had built in Excel 2003, when converted to .xlsm, would not enable macros and wouldn't offer the user the option to do so. I researched the issue online and someone mentioned that the worksheets and workbooks could not be protected or Excel 2007 won't enable macros.

    After monkeying around with Excel 2007 for a while, I discovered that Excel 2007 doesn't care if the worksheets are protected, with or without a password, but it WILL NOT enable macros in a workbook that is password protected on open. Period! Won't give you the option to enable either, even if you place the workbook in a "trusted location." The workbook can be protected when opened, just not password protected.

    Once the workbook is open, you can apply password protection to the workbook itself and everything works OK because the macros have already been enabled. I am now making sure that workbook protection is removed on close and then reset by the user (click this button to show sheets) after its opened.

    FWIW.

    Ron
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes.

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I just had a FileSearch issue raise it's ugly head for me as well. I know that it doesn't work in 2007 any more, but due to a brain cramp, I tried testing a routine in 2007 that used this method.

    Removing it is one thing, but they could at least make it throw an error... As it is right now, the routine just silently ends, without even giving the chance to do anything about it. Not very cool at all.
    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!





  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Wasn't that stupid? This is what we get from one dumb a$$ progger - ASSUMING! I throw curses at that person..

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I figured it actually came about by committee. In my experience there usually seems to be one of those beyond the most ridiculous decisions...
    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
    VBAX Contributor CaptRon's Avatar
    Joined
    Mar 2007
    Location
    Austin, Texas
    Posts
    141
    Location

    Save routines for both Excel 2003 and Excel 2007

    Xld helped me with an issue that deals with the file format and syntax variances between Excel 2007 and earlier versions.

    I created a workbook in Excel 2003 for distribution within our organization. Only about 10% of the agency at this point has migrated to Office 2007. The workbook creates a duplicate of itself and I wanted that duplicate to save in the Excel file format resident on the user's machine.

    When the portion of code was added to accommodate the save routine for Excel 2007, it was not recognized by Excel 2003 and was then treated as an undeclared variable.

    The solution is found at:

    http://vbaexpress.com/forum/showthread.php?t=20220

    Ron
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes.

  8. #8
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Does Worksheet.UsedRange.Rows.Count cause any issues?
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Oorang
    Does Worksheet.UsedRange.Rows.Count cause any issues?
    No it won't. It is unlikely that UsedRange would be all rows, and even if it were it would not be a problem as Count is a Long, so it caters for 1M+ rows easily.

    However, Worksheet.Cells.Count is a different matter. We are talking 17Billion+ cells here and that overflows Count significantly. Becuase of this, 2007 as an extra property, CountLarge, which returns the answer.

    Interestingly, type

    ?TypeName(Activesheet.Rows.Count)

    in the immediate window, and it tells you it is a long. Type

    ?TypeName(Activesheet.Cells.CountLarge)

    and you get a Compile Error.
    Last edited by Bob Phillips; 06-19-2008 at 10:28 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    XLD >> That is interesting.

    MDMackillop >> Why aren't we just using something like this?

    [VBA]Public Function GetBottomRow(ByRef ofWorksheet As Excel.Worksheet) As Long
    Const lngOffset_c As Long = 1
    Dim rngUsed As Excel.Range
    Set rngUsed = ofWorksheet.UsedRange
    GetBottomRow = rngUsed.Rows.Count + rngUsed.Row - lngOffset_c
    Set rngUsed = Nothing
    End Function[/VBA]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why use

    [vba]

    GetBottomRow = rngUsed.Rows.Count + rngUsed.Row - lngOffset_c
    [/vba]

    isn't rngUsed.Row always 1, so you could just use

    Why use

    [vba]

    GetBottomRow = rngUsed.Rows.Count
    [/vba]

    but usedrange has the leftover bug, which spoils it anyway.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    lol Well mostly because it's not always "1". Create some test data in a worksheet and then insert several empty rows above it. The start of the used range will be the first row with data.

    I know, I know... "Who in their right mind would that?"

    hehe I can only say I have seen it done

    (It's usually the same people who think that color is great way to show if an account has been paid or not.)
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Okay, but you can do it a lot simpler notwithstanding the limitations of UsedRange

    [vba]

    Function GetBottomRow(ByVal sh As Worksheet) As Long

    With sh
    GetBottomRow = .UsedRange(.UsedRange.Count).Row
    End With
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Looks like a winner to me
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    ... and if Column B data is longer than Column A data?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Then it returns the last row of B ...
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  17. #17
    VBAX Regular markmrw's Avatar
    Joined
    Oct 2008
    Location
    UK
    Posts
    35
    Location

    2007 - TintAndShade

    I discovered this after my work PC was upgraded to 2007, while other PC's in the company were still running 2003

    [VBA]
    .TintAndShade = 0
    [/VBA]

    I used the record macro button. to change some formats on a couple of sheets, and this line of code runs fine with 2007,
    but any earlier versions, break and explode causing many of my colleagues much distress and anguish.

    thoought this might be handy for all.

    Mark

  18. #18
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Since interest seems to be increasing, here is a comprehensive list of all Adds and Changes to Excel Object Model since 2003:
    http://msdn.microsoft.com/en-us/library/bb149069.aspx

    As a side note, if you are going to use 2007 methods/properties in a mixed shop it might be beneficial to call them in a wrapper. In this way you will work as expected in either version. Here is a simple example of how to do this:

    [vba]Sub Test()
    ColorRange Selection, Excel.Application.version, 6
    End Sub
    Sub ColorRange(rng As Excel.Range, version As Double, ParamArray args() As Variant)
    With rng.Interior
    .colorIndex = 6
    .Pattern = xlSolid
    If version >= 12# Then
    'Because the property name is stored in a string this will still compile.
    'And it will only get called if the correct version is in use.
    CallByName rng.Interior, "TintAndShade", VbLet, 0
    End If
    End With
    End Sub
    [/vba]

    Edit:
    Just occured to me you could put version inside the wrapper instead of passing it as a parameter. (I know... duh)
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  19. #19
    VBAX Regular markmrw's Avatar
    Joined
    Oct 2008
    Location
    UK
    Posts
    35
    Location
    Thank you oorang

    Very helpful, bookmarked
    I keep finding many little things, if i find any not listed i will post.
    Now that christmas is over, back to the grind stone i suppose.

    Thanks
    Mark
    -MRW-

  20. #20
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Ok, so the last row discussion came up over on MrExcel... And I think this function from Rorya may account for more vagaries than the "usedrange" approach we were discussing.

    [VBA]
    Public Function LastRowInSheet(wks As Worksheet) As Long
    'From Rorya on MrExcel:
    'http://www.mrexcel.com/forum/showthread.php?p=1793851&posted=1#post17938521
    ' Returns the number of the last row with data anywhere in it
    LastRowInSheet = 1
    On Error Resume Next
    With wks.UsedRange
    LastRowInSheet = .Cells.Find("*", .Cells(1), SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
    End With
    End Function
    [/VBA]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

Posting Permissions

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