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.
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.
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.
You could just use 1 line.
I am going to assume that you have merged cells and that is why you are getting the errorPlease Login or Register to view this content.
Last edited by stevoDE; 09-12-2017 at 11:22 AM.
I think this should achieve that
Please Login or Register to view this content.
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?
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.
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.
I just ran my last code against that sample, and the result is exactly the same as your After sheet.
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
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.
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.
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.
Gotcha!
Thanks a million for all your help & patience, very much appreciated.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks