Selection.Insert Shift:=xlToRight Acting Odd

Bill_Biggs

Well-known Member
Joined
Feb 6, 2007
Messages
1,216
I have a macro that processes one or more sheets of data. At one point it finds a certain column, copies the occupied cells in the column, selects column A, and inserts the copied column here.

Here is that portion of the macro:

Cells.Find(What:="CpuFNode*:MemClkFreq", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Select
Range(Selection, Selection.End(xlDown)).Copy
Columns("A:A").Select
Selection.Insert Shift:=xlToRight

The first time through, the macro performs flawlessly. But the second time through, the new column is pasted all the way down the column, using all 65000 rows and not just the 20-150 cells that it did microseconds before.

Has anyone seen odd behavior like this before? Is there something I can do to remedy it?

Thanks,

Bill Biggs
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The second time through there is no other cells below the cell containing "CpuFNode" and so the selection.end(xlDown) goes all the way to the bottom of the worksheet......

That appears to be the problem. Maybe you need a command something like:

If activecell.offset(1,0) = "" then activecell.offset(-1,0).activate


Though this would get into trouble at the top of the row...
Philby

EDIT: No, I've gotten confused. But for some reason the select to end command is goign to the bottom. I just can't tell why. How many instances of the "CPU" thing are there?
 
Upvote 0
Range(Selection, Selection.End(xlDown)).Copy

I think this line is the issue. If the next time you find the search item it's the only thing in the column, Excel will select down to the end of the column (not the end of the data).

Are you trying to copy a set number of cells across? If so, you could use

Code:
 Selection.Resize(25,1).Copy
To copy 25 rows... adjust as required.

Denis
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,185
Members
448,554
Latest member
Gleisner2

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