VBA to filter, copy and paste in separate workbook

Iron_Man

New Member
Joined
Aug 26, 2014
Messages
25
Hi all,

I've adapted some code from an old thread on this forum and was hoping for some help to tweak the logic.

I have a file "CT Raw Data.xlsx" where my raw data is stored on a worksheet called "Raw Data", from cell A2 to column O (infinite number of lines).

In column A, I have a value which helps me decide which sheet of separate file "CT.xlsx" the data should be copied in (ie if a line has a value in column A of "Location 1", it should be copied in sheet "Location 1" of CT, etc). Noting that this macro is to be run on a daily basis and not all locations are always present (ie we could have Location 1, 2 and 3 on one day and just 1 and 2 on the next day").

The sheets on CT.xlsx all have the same format and data should be copied in the first blank row after B4. If I happen to click the button twice, am ok to have the values copied twice.

VBA Code:
Sub FilterMini()
Sheets(2).Activate
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrow2 = Workbooks("CT.xlsx").Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row + 1
With ActiveSheet.Range("B2:O" & Lastrow)
.AutoFilter Field:=1, Criteria1:="Location A", Operator:=xlFilterValues
.SpecialCells(xlCellTypeVisible).Copy Workbooks("CT.xlsx").Worksheets("Location A").Range("B5:O" & Lastrow2)

End With
ActiveSheet.AutoFilterMode = False
End Sub


1. For a reason that I ignore, if I happen to run the code twice the data that just got pasted disappears or copies an incorrect title value from the destination file onto the destination file.

2. Any suggestion on how to iterate through different filters and copy to more sheets in the CT.xlsx workbook without writing the same code 6 times?

Do you have any thoughts on how to achieve this?

Thanks in advance,
IM
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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