Copying en entire selection when it is AUTOFILTERED

alexenvalencia

New Member
Joined
Mar 25, 2011
Messages
11
My original question:

I have a block of data which I want to select in order to copy it.

It begins in column A and runs to column AU. It begins in row 2 and runs down a number of rows (typically 2000 rows but it varies) until there is no data in the rows.

Column C is the KEY and has data all the way till the last row so we should use this to determine how far down we need to make the selection.

Roberts excellent solution:

Code:
[/FONT]
[FONT=Tahoma]Sub Macro1()[/FONT]
 
[FONT=Tahoma]Dim rngMyDataRange As Range[/FONT]
 
[FONT=Tahoma]Set rngMyDataRange = Range("A2:AU" & Range("C" & Rows.Count).End(xlUp).Row)[/FONT]
 
[FONT=Tahoma]End Sub[/FONT]
[FONT=Tahoma]






</PRE>
A problem remains: The block of data is often autofiltered by the user with the filter being applied to COLUMN C (the key column). I need to copy the ENTIRE block of data to a new sheet, while not removing the user defined autofilter as he doesnt want to lose his filter settings (a valid option is to remove the filter, remember the settings, execute the copy and reapply the filter, but there must surely be a simpler solution).

Robert suggested:

Code:
[FONT=Tahoma]Sub Macro1()[/FONT]
 
[FONT=Tahoma]   Dim strSourceTab As String, _[/FONT]
[FONT=Tahoma]       strDestinTab As String[/FONT]
[FONT=Tahoma]   Dim lngLastRow As Long[/FONT]
 
[FONT=Tahoma]   'Source tab name (change to suit)[/FONT]
[FONT=Tahoma]   strSourceTab = "Sheet1"[/FONT]
[FONT=Tahoma]   'Destination (output) tab name (change to suit)[/FONT]
[FONT=Tahoma]   strDestinTab = "Sheet2"[/FONT]
 
[FONT=Tahoma]   'http://excelvbamacro.com/how-to-find-the-last-row-that-contain-data-in-excel.html[/FONT]
[FONT=Tahoma]   lngLastRow = Sheets(strSourceTab).UsedRange.Row - 1 + Sheets(strSourceTab).UsedRange.Rows.Count[/FONT]
 
[FONT=Tahoma]   With Sheets(strDestinTab).Range("A2:AU" & lngLastRow)[/FONT]
[FONT=Tahoma]       .Formula = "=" & strSourceTab & "!A2"[/FONT]
[FONT=Tahoma]       .Value = .Value 'Converts the above formula (link) range to values.  Comment out / delete if not needed.[/FONT]
[FONT=Tahoma]   End With[/FONT]
 
[FONT=Tahoma]End Sub[/FONT]





</PRE>
Which is a nice attempt but unfortunately I need to simulate the effects of a plain sourcesheet.copy, destinationsheet.paste effect.

Does anyone have any ideas? Is it possible to modify roberts code to have the desired effect or is there an even simpler solution, such as a variation on the .copy method that actually includes underlying filtered rows?

Many thanks,

Alex
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Can you explain what the shortfall is in that solution?
 
Upvote 0
Hi Glenn

The problem with the second solution is that in cell A1 of the destination sheet, it has the formula "=source!A1" etc for all the cells, with no formating. I need to reproduce the effects of a copy and paste operation as executed via the right click menu, ie bring values, formulas and formatting across.
 
Last edited:
Upvote 0
I need to simulate the effects of a plain sourcesheet.copy, destinationsheet.paste effect
What effects are you talking about, that aren't produced by the solution given?
 
Upvote 0
Glenn - if you try the proposed solution you will see what I mean.

Copy a selection on one sheets (right click menu) and then paste it to a new sheet. Not paste special, use regular paste.

Now try roberts code. You will see that each cell is populated with a link to the equivalent cell in the original sheet. This is undesirable because if we change a value in the original sheet we will affect the destination sheet too.
 
Upvote 0
Glenn - if you try the proposed solution you will see what I mean.

Copy a selection on one sheets (right click menu) and then paste it to a new sheet. Not paste special, use regular paste.

Now try roberts code. You will see that each cell is populated with a link to the equivalent cell in the original sheet. This is undesirable because if we change a value in the original sheet we will affect the destination sheet too.
no, the part of the code that does .Value=.Value converts the links to values ... unless I'm reading that incorrectly.
 
Upvote 0
I need the equivalent of a paste.formula followed by a paste.formats

(ie a standard right and click paste and copy from one sheet to another)
 
Upvote 0
So, it's the formats that you want copied? Do the formats vary by row? Or will the format from any visible row in the original data do?

And I'd say that you need the equivalent of paste.values and paste.formats, rather than formulas.

And you haven't said whether the code actually converts the link formulas to values, as I stated ... is that right or not?
 
Upvote 0
Before I look at this ... can you tell me what you are aiming to do overall? Why do you want a duplicate copy of cells in another sheet? Why can't you copy the sheet object itself?
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,691
Members
452,938
Latest member
babeneker

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