Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Excel 2003 VBA guru please!

  1. #1
    Join Date
    Feb 2007
    Location
    Brisbane
    Posts
    4,497
    Total Downloaded
    0

    Excel 2003 VBA guru please!

    This is a problem that happens on a couple of excel tools we use. Tried before to fix it but have never found a satisfactory, long term fix.

    For example:

    Excel 2003 .xls file where 340 rows and 12 columns are used, 1.6MB in size. Basically its a list of job numbers, names, order numbers, etc. It has a number of VBA subs to hide rows, sort, add and delete rows.

    When the file gets to ~340 rows and we try to add a row using the macro below, Excel locks up. Eventually (after 10 minutes or so) it will come up with the following:

    "Excel cannot complete this task with available resources......:

    and then
    "Run time error '-2147417848 (800 10 108)':
    Automation error
    The object invoked has disconnected from its clients"

    This occurs on multiple PCs running Vista business and Win 7, versions of Excel from 2003 to current.

    Sub InsertRow()
    'Inserts a row at the bottom of the sheet.
    ActiveSheet.Unprotect
    n = Range("EndRow").Row 'Find the row number of the row named EndRow.
    ActiveSheet.Range(Cells(n - 1, 1), Cells(n - 1, 19)).Copy 'Copy the last row.
    ActiveSheet.Range(Cells(n, 1), Cells(n, 12)).Insert Shift:=xlShiftDown 'Insert
    ActiveSheet.Paste Range(Cells(n, 1), Cells(n, 19)) 'Paste
    ActiveSheet.Range(Cells(n, 1), Cells(n, 12)).ClearContents 'Clear the data in the new row.
    Cells(n, 1).Activate
    ActiveSheet.Protect
    End Sub

    Any and all help would be appreciated.

  2. #2
    mikehzz Guest
    Mate there is nothing wrong with that code. I have run it to automatically insert 2000 rows in a sheet I just set up with no issues. I would suggest that there is a problem somewhere in your sheet. Does your sheet only have 340 rows or are there many more but hidden? The error type smacks of some sort of memory or data corruption. Cheers

  3. #3
    mikehzz Guest
    Also, is there any reason you are copying, pasting and clearing? Removing those lines might bypass the error if it is data related.

  4. #4
    Join Date
    Feb 2007
    Location
    Perth
    Posts
    3,918
    Total Downloaded
    0
    The code you posted seems to serve only to move the address of range 'End Row' down by 1 row.

    For example if the named range 'End Row' was initially located on row 17 then your code performs the following steps

    1 Copy to clipboard range (A16:S16)
    2 Inset new row at range (A17:L17) and move existing cells down
    3 Paste clipboard (ie data from range A16:S16) to range (A17:S17)
    4 Clear range (A17:L17)
    5 Activate cell (A17)

    The net effect is:

    1 The data on row 16 is unchanged
    2 You now have an empty row on row 17
    3 The named range 'End Row' is now on row 18.

    This could be all achieved a little more simply. It looks like your 'copy' and 'paste' steps are redundant. You immediately clear what you copied.The only other comment I would make is change all instances of 'ActiveSheet' to refer specifically to the sheet in question. ie

    Worksheets("Sheet1").Range(Cells(n - 1, 1), Cells(n - 1, 19)).Copy

    instead of

    ActiveSheet.Range(Cells(n - 1, 1), Cells(n - 1, 19)).Copy

    Making a call to 'ActiveSheet' can cause confusion if for whatever reason the 'active sheet' ceases to be the 'active sheet'. This is something like making an 'unqualified call'. VBA does not specifically know which 'instance' of a sheet is now the 'active sheet'.

    All I can think of.
    2024 RRS on the road
    2011 D4 3.0 in the drive way
    1999 D2 V8, in heaven
    1984 RRC, in hell

  5. #5
    Join Date
    Feb 2007
    Location
    Brisbane
    Posts
    4,497
    Total Downloaded
    0
    Quote Originally Posted by mikehzz View Post
    Also, is there any reason you are copying, pasting and clearing? Removing those lines might bypass the error if it is data related.
    Only that by doing this I can create a new row with the same format.

  6. #6
    Join Date
    Feb 2007
    Location
    Brisbane
    Posts
    4,497
    Total Downloaded
    0
    Quote Originally Posted by Ferret View Post
    The code you posted seems to serve only to move the address of range 'End Row' down by 1 row.

    For example if the named range 'End Row' was initially located on row 17 then your code performs the following steps

    1 Copy to clipboard range (A16:S16)
    2 Inset new row at range (A17:L17) and move existing cells down
    3 Paste clipboard (ie data from range A16:S16) to range (A17:S17)
    4 Clear range (A17:L17)
    5 Activate cell (A17)

    The net effect is:

    1 The data on row 16 is unchanged
    2 You now have an empty row on row 17
    3 The named range 'End Row' is now on row 18.

    This could be all achieved a little more simply. It looks like your 'copy' and 'paste' steps are redundant. You immediately clear what you copied.The only other comment I would make is change all instances of 'ActiveSheet' to refer specifically to the sheet in question. ie

    Worksheets("Sheet1").Range(Cells(n - 1, 1), Cells(n - 1, 19)).Copy

    instead of

    ActiveSheet.Range(Cells(n - 1, 1), Cells(n - 1, 19)).Copy

    Making a call to 'ActiveSheet' can cause confusion if for whatever reason the 'active sheet' ceases to be the 'active sheet'. This is something like making an 'unqualified call'. VBA does not specifically know which 'instance' of a sheet is now the 'active sheet'.

    All I can think of.
    Thanks. Basically all I'm doing is finding the last row, copy, insert it and clear it to create a new row with the same format.

    I'll change the "activesheet" references.

    Thanks.

  7. #7
    mikehzz Guest
    Are any of the cells being copied containing formulae or links to cells elsewhere in the workbook perhaps on other sheets? If the links are somehow broken on some of them it may cause a problem.

  8. #8
    Join Date
    Feb 2007
    Location
    Brisbane
    Posts
    4,497
    Total Downloaded
    0
    Quote Originally Posted by mikehzz View Post
    Mate there is nothing wrong with that code. I have run it to automatically insert 2000 rows in a sheet I just set up with no issues. I would suggest that there is a problem somewhere in your sheet. Does your sheet only have 340 rows or are there many more but hidden? The error type smacks of some sort of memory or data corruption. Cheers
    There's 340 formated rows at the moment. InsertRow just makes it 341, etc.

    No hidden rows and the sheet the standard 65,000 blank rows.

  9. #9
    Join Date
    Feb 2007
    Location
    Brisbane
    Posts
    4,497
    Total Downloaded
    0
    Quote Originally Posted by mikehzz View Post
    Are any of the cells being copied containing formulae or links to cells elsewhere in the workbook perhaps on other sheets? If the links are somehow broken on some of them it may cause a problem.
    No links, no formulae.

  10. #10
    Join Date
    Feb 2007
    Location
    Perth
    Posts
    3,918
    Total Downloaded
    0
    Quote Originally Posted by WhiteD3 View Post
    No links, no formulae.
    Any 'events'
    2024 RRS on the road
    2011 D4 3.0 in the drive way
    1999 D2 V8, in heaven
    1984 RRC, in hell

Page 1 of 2 12 LastLast

Bookmarks

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 AULRO.com ONLY!
Search All the Web!