Maximum number of cells selected at one time

shipman01

New Member
Joined
May 11, 2005
Messages
21
I'm writing what should be a very simple little macro that copies data from one worksheet to another. All the values being copied are in col W at various locations. There are 51 cells that need to be selected, all non-adjacent. I was hoping to be able to grab these values all at one time, copy them, go to another sheet, paste.

However, I can't seem to get the cells selected. The line is simple: Range("W47,W95,W140,W185,W230,W275, ....) etc - 51 values in all

When I test this line, I get a runtime error:

Method range of object global failed

Is there a maximum number of non-adjacent cells that can be selected at one time? Or should I create a variable for these cells? Or is there a better way all together?

Thanks so much!!

Mary
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This seems very inefficent, and difficult to maintain..
What if the cells change for some reason in the future?

Is there any pattern to the cells you want copied?
Like if the cell in Column X = "copy me", then copy the cell in column W of the same row?
Anything like that?
 
Upvote 0
I believe the maximun string length for an argument to the Range function is 255 or there abouts. I agree with jonmo1... "This seems very inefficent, and difficult to maintain". Except for the first cell, all the other cells seem to be 45 rows apart... does that pattern continue for all your 51 cells? If so, we can give you a simple loop to replace your long Range command. Otherwise, you may have to set up an array of your cell addresses and iterate that.
 
Upvote 0
No, unfortunately there is no pattern. I have confirmed with the requester that the report being referenced will not change (but we all know how that goes).

This is a large report that is broken into sections with blank rows separating the sections. For each section, Col W has two cells at the top with text, then many empty cells, then a value at the end of the section.

I'm considering saving the report file with a temporary name, then deleting the rows with col W has a blank or non-numeric cell, and then taking the resulting rows of numbers as my range to copy. This would be an easy fix and (I think) cleaner. And in this way if a location does change, the macro will still work.

I could also take the cells one at a time, moving through Col W looking for numeric values, but that process of finding, copying, going to another sheet, pasting, moving back, and continuing would have to repeat 51 times, so that doesn't seem so efficient either. It would be nice if there were a way to select Col w and then copy all numeric cells ... and there may be a way, but I don't know it.

Do you think either of these approaches makes more sense? I'm open to suggestions.

Mayr
 
Upvote 0
No, unfortunately there is no pattern. I have confirmed with the requester that the report being referenced will not change (but we all know how that goes).

This is a large report that is broken into sections with blank rows separating the sections. For each section, Col W has two cells at the top with text, then many empty cells, then a value at the end of the section.

I'm considering saving the report file with a temporary name, then deleting the rows with col W has a blank or non-numeric cell, and then taking the resulting rows of numbers as my range to copy. This would be an easy fix and (I think) cleaner. And in this way if a location does change, the macro will still work.

I could also take the cells one at a time, moving through Col W looking for numeric values, but that process of finding, copying, going to another sheet, pasting, moving back, and continuing would have to repeat 51 times, so that doesn't seem so efficient either. It would be nice if there were a way to select Col w and then copy all numeric cells ... and there may be a way, but I don't know it.

Do you think either of these approaches makes more sense? I'm open to suggestions.

Mayr

There is..

Take a look at the specialcells method...

Something like

Range("W:W").SpecialCells(xlCellTypeConstants, 1).Copy
 
Upvote 0
I'm considering saving the report file with a temporary name, then deleting the rows with col W has a blank or non-numeric cell, and then taking the resulting rows of numbers as my range to copy.
If all you want to do is copy the rows where the cell in Column W contains a number, you can do that with one line of code (provided there will not be more than 8192 such numeric rows total). Let's say you wanted to copy them to Sheet2, then this line of code should do that for you...

Code:
Columns("W").SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow.Copy Worksheets("Sheet2").Range("A1")
 
Upvote 0
Thanks....that would be handy. Unfortunately, the paste location varies depending on the date, as this routine will run daily until a month's worth of data is accumulated.

The SpecialCells method works like a charm, though. This was the only piece that was giving me an issue.

Thanks again!

Mary
 
Upvote 0
Thanks....that would be handy. Unfortunately, the paste location varies depending on the date, as this routine will run daily until a month's worth of data is accumulated.

The SpecialCells method works like a charm, though. This was the only piece that was giving me an issue.
I'm not clear... do you still have a problem or do you see the way around the issue your raise in the first paragraph? I would need to know more about your structure, but there is nothing magical about the Range("A1") I used... you can do the copy to any row you want. Let's say you wanted to put the new data right after any existing data on Sheet2, then something like this should work...

Code:
LastUsedRow = Worksheets("Sheet2").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
Columns("W").SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow.Copy Worksheets("Sheet2").Cells(LastUsedRow + 1, 1)
 
Upvote 0
Thank you .... I've got it now. Macro is finished and works beautifully. Thanks to each of you for all the help.

Mary
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top