+ Reply to Thread
Results 1 to 15 of 15

Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

  1. #1
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Hi

    Can anyone please tell me how to get around this error? I am trying to insert a column after all columns which contain comments. I have name the sheet 'Report' within VBA.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Hi,

    If you have multiple discontiguous rows in the same column(s) with comments, that will create an overlapping range, which is why you can't perform the insert. You should be able to correct that using Intersect
    Please Login or Register  to view this content.
    Last edited by xlnitwit; 09-12-2017 at 11:14 AM.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    You could just use 1 line.
    Please Login or Register  to view this content.
    I am going to assume that you have merged cells and that is why you are getting the error

  4. #4
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Quote Originally Posted by xlnitwit View Post
    Hi,

    If you have multiple rows in the same column(s) with comments, that will create an overlapping range, which is why you can't perform the insert. You should be able to correct that using Intersect
    Please Login or Register  to view this content.
    Ah, that makes sense, thanks.

    I have also misinterpreted the use of shift:=xlToRight.

    What I would like to do is insert a new column to the right of all columns which contain comments - what's the easiest way to do that?

    Thanks in advance
    Last edited by stevoDE; 09-12-2017 at 11:22 AM.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    I think this should achieve that
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    That does something crazy and crashes Excel I'm afraid.

    I think I can use offset(,1) to insert columns after the columns with comments. The issue I have is that the above intersect function groups sequential columns together if they contain comments. For example, if columns A & B both contain comments and I try to insert a column, it will insert a single column before column A only. Is there any way to select all columns within the intersect range individually? i.e. the equivalent of holding down CTRL and selecting the columns with comments?

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Amended code- I had forgotten that the insert would create an endless loop using a For Each loop
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Nope, that doesn't work either I'm afraid.

    I have attached a before and after s/s to hopefully make things easier. Just to confirm, I would like to add a new column after every column which contains comments and the columns which contain comments may change.
    Attached Files Attached Files

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    I just ran my last code against that sample, and the result is exactly the same as your After sheet.

  10. #10
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Yes, it does! Apologies, not sure what I done first time around.

    One final thing. Would you mind explaining in words what the 'For each' and 'For' loops are doing here please?

    Many thanks for all your help

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    The CommentsColumns range is made up of several distinct areas (adjacent columns count as one area). The For Each loop loops through each area, and then the For loop loops backward from the rightmost column of each area and inserts a new column to the right of each column. It has to loop backwards to avoid the original issue where inserting a column increased the size of the area and caused an infinite loop.

  12. #12
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Many thanks.

    Apologies again, but a couple of other questions for you.

    I am stepping through this using F8 and whilst I can understand what the code is doing, I can't for the life of me work out the logic of the order of the columns/areas it looks at to add columns? For example, in the s/s I uploaded, the code adds a column after column S? Why does it start there as opposed to adding a column after column C or even W? If I keep stepping through, there are other illogical jumps over columns with comments as well. It eventually captures all columns with comments but I'm confused by the logic.

    And what is the purpose of shift:=xlToRight?

    The reason I ask is that if I run the code through without it, the end result is the same?
    Last edited by stevoDE; 09-13-2017 at 10:14 AM.

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    The first cells with comments in, reading left to right and top to bottom, are in row 3 columns R and S. This is therefore the first area obtained by specialcells.

    The Shift parameter doesn't make a difference when working with entire columns but does if you are only inserting- or deleting- parts of a row or column.

  14. #14
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    Gotcha!

    Thanks a million for all your help & patience, very much appreciated.

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Runtime 1004 Error - Cannot Use that Command On Overlapping Sections

    My pleasure- it's always nice to come across someone who wants to understand the code rather than blindly implementing it!

    Thank you for the rep too.

+ 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. Runtime Error 1004 Help
    By Rocky Mateer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-07-2015, 09:54 AM
  2. Runtime error '1004': Paste special method of range class error.
    By Daryl Zer0 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-05-2014, 01:44 AM
  3. [SOLVED] VBA Error: Runtime Error 1004: AutoFilter method of Range class failed
    By jl22stac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2013, 07:27 PM
  4. [SOLVED] Range error in code, runs alone but not inside my full program, giving runtime error 1004
    By charizzardd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2012, 03:34 PM
  5. [SOLVED] Cannot use that command on overlapping sections
    By tlinton in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-09-2012, 04:59 PM
  6. simple column delete, overlapping sections error?
    By bauerbach in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-02-2012, 02:51 PM
  7. Excel 2003 Macro Error - Runtime error 1004
    By Cow in forum Excel General
    Replies: 2
    Last Post: 06-07-2005, 09:05 AM

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