Filtering from one workbook, pasting into another

jalrs

Active Member
Joined
Apr 6, 2022
Messages
300
Office Version
  1. 365
Platform
  1. Windows
Hello guys,

I'm new to VBA and therefore i need some help. I would be glad if i found it here.
So, my problem is: I runned a code after doing my search here, but the code misses on something, after i did my changes and debug it. Im filtering from a sheet called "Stock Trânsito" in Workbook1 to paste on a sheet called "pendentes" on a workbook called "STTApoioSP". When i run the code, instead of pasting into cell "A2" it pastes into cell "A21" and brings a value that doesnt match the criteria filter.

My code:

Sub filterAPOIOSP()

ThisWorkbook.Worksheets("Stock Trânsito").Activate

Dim lastrow As Long, lastrow2 As Long

lastrow = Cells(Rows.Count, "A").End(xlUp).Row
lastrow2 = Workbooks("STTApoioSP.xlsm").Sheets("pendentes").Cells(Rows.Count, "A").End(xlUp).Row

With ActiveSheet.Range("A6:AV" & lastrow)
.AutoFilter Field:=46, Criteria1:="Apoio SP", Operator:=xlFilterValues
.SpecialCells(xlCellTypeVisible).Copy Workbooks("STTApoioSP.xlsm").Worksheets("pendentes").Range("A2" & lastrow2)

End With

ActiveSheet.AutoFilterMode = False

End Sub


Additional questions:
1- If i want to add criteria2, how should i add it into the code?
2- how can i add a specific column from my workbook to this new one? i mean, where do i write it on the code?

thanks in advance!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try changing .Range("A2" & lastrow2) to .Range("A" & lastrow2)
 
Upvote 0
Try changing .Range("A2" & lastrow2) to .Range("A" & lastrow2)
it worked indeed, but i lost all the headers on row 1 so not quite there yet. i tried "A1" instead and it pasted on cell "A11"

anything else that might work?
thanks for your time!
 
Upvote 0
it worked indeed, but i lost all the headers on row 1 so not quite there yet. i tried "A1" instead and it pasted on cell "A11"

anything else that might work?
thanks for your time!
You're copying to the last row in the sheet "pendentes" that your code finds data - which may be the header row. Try setting the row where you actually want the data to be pasted by .End(xlUp).Row + 1
 
Upvote 0
You're copying to the last row in the sheet "pendentes" that your code finds data - which may be the header row. Try setting the row where you actually want the data to be pasted by .End(xlUp).Row + 1
i mean i removed the "& lastrow2" from the .Range("A" & lastrow2) and simply rewrote it by .Range("a2") and it worked.

should i have any concerns regarding this? plus, could u help me on additional questions 1, 2?
Thanks again for your time!
 
Upvote 0
i mean i removed the "& lastrow2" from the .Range("A" & lastrow2) and simply rewrote it by .Range("a2") and it worked.

should i have any concerns regarding this? plus, could u help me on additional questions 1, 2?
Thanks again for your time!
I'm away from my laptop right now, could you possibly post a sample of your data & I'll be happy to look your issues, although I suspect someone else will be able to help you before I do.
 
Upvote 0
I'm away from my laptop right now, could you possibly post a sample of your data & I'll be happy to look your issues, although I suspect someone else will be able to help you before I do.
i appreciate the help, however the data is sensitive so i cant really post a sample of it.
ill try to explain better what i want regarding 1 and 2

for 1) i want to also copy BH column from the worksheet "Stock Trânsito" on first workbook and paste it on column AW on sheet "pendentes" from the new workbook. my problem is i have set the copy range from A6 to AV [With activerange ("A6:AV" & lastrow)] and i dont know how to include the column BH starting on cell BH6 to copy it for cell AW2 on new workbook.

for 2) my first criteria of filtering is the logistics responsible, in this case its called "Apoio SP", the second criteria i want to apply is "in transit" which corresponds to the stock status. to sum up, i want to filter the data that concerns to "Apoio SP" and it is "in transit". field of stock status is 46 if it helps.

Thank you for your patience!!
 
Upvote 0
You could still provide a sample of your layout with bogus information in it - II really just want to get a grip on the layout of your sheet. I've provided a sample below to show what I mean. Based on your description, I'm assuming that your headers on the "Stock Trânsito" sheet are on row 6. Therefore, your sheet would look something like this:

STTApoioSP.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBH
1assume nothing up here
2
3
4
5
6Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26Header27Header28Header29Header30Header31Header32Header33Header34Header35Header36Header37Header38Header39Header40Header41Header42Header43Header44Header45Header46Header47Header48Header49Header50Header51Header52Header53Header54Header55Header56Header57Header58Header59Header60
7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxxxxxxxxxxxxbh
11xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxxxxxxxxxxxxbh
12xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxxxxxxxxxxxxbh
13xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxxxxxxxxxxxxbh
14xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbh
15xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbh
16xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
17
Stock Trânsito


If this is what your sheet looks like, then the following code will address your original post, as well as your additional question #2.

VBA Code:
Option Explicit
Sub jalrs()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Stock Trânsito")
    Set ws2 = Worksheets("pendentes")
    
    Dim lr1 As Long, lr2 As Long
    lr1 = ws1.Cells(Rows.Count, 1).End(3).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(3).Row + 1
    
    With ws1.Range("A6:AV" & lr1)
        .AutoFilter 46, "Apoio SP"
        .Offset(1).Copy ws2.Cells(lr2, 1)
        With ws1.Range("BH7:BH" & lr1)
           .Copy ws2.Cells(2, 49)
        End With
        .AutoFilter
    End With
End Sub

Here's your "pendentes" sheet begore running the code:

STTApoioSP.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAW
1header1header2header3header4header5header6header7header8header9header10header11header12header13header14header15header16header17header18header19header20header21header22header23header24header25header26header27header28header29header30header31header32header33header34header35header36header37header38header39header40header41header42header43header44header45header46header47header48header49
2
3
4
5
6
pendentes


And after:

STTApoioSP.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAW
1header1header2header3header4header5header6header7header8header9header10header11header12header13header14header15header16header17header18header19header20header21header22header23header24header25header26header27header28header29header30header31header32header33header34header35header36header37header38header39header40header41header42header43header44header45header46header47header48header49
2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxbh
3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxbh
4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxbh
5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxApoio SPxxbh
6
7
pendentes


As far as your additional question #1 is concerned - yes, no problem, just let me know which column you want the second criteria applied to.
 
Upvote 0
Hello Kevin!

Sorry, didnt really think about the bogus information layout, since im new dont really know how things work here yet. but i'll get there.

Yes thats like it except that "Stock Transito" (here shown as 1st Screenshot" is in a different workbook (Named "AnaliseST.xlsm") and "pendentes" is in STTApoioSP.xlsm like you provide on 2nd and 3rd screenshots.

As for second criteria, it is applied to column AU on 1st workbook and, so column #47. criteria here is "in transit"

Thank you!!!!
 
Upvote 0
Hello Kevin!

Sorry, didnt really think about the bogus information layout, since im new dont really know how things work here yet. but i'll get there.

Yes thats like it except that "Stock Transito" (here shown as 1st Screenshot" is in a different workbook (Named "AnaliseST.xlsm") and "pendentes" is in STTApoioSP.xlsm like you provide on 2nd and 3rd screenshots.

As for second criteria, it is applied to column AU on 1st workbook and, so column #47. criteria here is "in transit"

Thank you!!!!

might be stupid to ask but, does values keep the same format when copied and when pasted?

ill try the code when i get to work, ~3h. but just by that first glance, ye defo looks like it
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

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