How to select the autofilter pasted range?

anirudh2012

New Member
Joined
Nov 16, 2017
Messages
14


I have the following macro to autofilter and then paste it into another excel. Now I want to change some data for the new pasted range.
How can I select the range as shown in my comment: because it is a for loop and every time the data will be different


<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">For i =2To lastRow
bCell
= wb.Sheets("User1").Range("C"& i).Value

wb2
.Sheets("SubWPRole").Range("G1").AutoFilter Field:=7, Criteria1:=bCell
wb2
.Sheets("SubWPRole").AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Copy wb2.Sheets("Sub").Range("A"& wb2.Sheets("Sub").Rows.Count).End(xlUp).Offset(1)

' this is the line I am referring to
Set newrange = wb2.Sheets("Sub").AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible)

ForEach cell In newrange
wb2
.Sheets("Sub").Range("B"& cell.Row).Value = wb.Sheets("User1").Range("B"& i).Value
Next cell</code>


<tbody>
</tbody>
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I would recommend setting a variable to the top leftmost cell that you want to paste to, before you do the copy/paste

Set myCell = wbs.sheets("Sub").Range("A" & wb2.sheets("Sub").rows.count).end(xlup).offset(1)

then, change your copy/paste to:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">wb2.Sheets("SubWPRole").AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Copy mycell

And for the next line, calculate the new last row on the Sub sheet (replace #ofColumns with the actual number of columns):
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Set newrange = mycell.resize(,<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;"><code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;"><code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">#ofColumns </code></code></code>).offset(<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">wb2.Sheets("Sub").Range("A"& wb2.Sheets("Sub").Rows.Count).End(xlUp)-mycell.row)</code>
</code>

</code>
note: I haven't tested this - will do so and correct if there's any issues.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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