PC Review


Reply
Thread Tools Rate Thread

Copy Autofilter Source Workbook A result in Destination Workbook BSheet1

 
 
u473
Guest
Posts: n/a
 
      9th Sep 2008
I need to copy the result from Autofilter on Source Workbook A in
Destination Workbook B Sheet1
My VBA AutoFilter Code is in an external Menu Workbook
..
Sub Import()
Dim fso As Object
Dim Source As Object ' Source Folder path
Dim Dest As Object ' Destination Folder Path
Dim WBA as Object ' Source Workbook
Dim WBB as object ' Destination Workbook
Dim LastRow As Long, Dim Rng As Range
'
Set fso = CreateObject("Scripting.FileSystemObject")
Set Source = fso.GetFolder("P:\Invoices")
WBA = "A.xls"
WBB = "B.xls"
Workbooks.Open Filename:=WBA.Path
On Error GoTo 0
LastRow = Range("B65335").End(xlUp).Row: Range("B2").Select
ActiveSheet.Range("$A$2:$W$65535").AutoFilter Field:=2,
Criteria1:="=Open", Operator:=xlOr, Criteria2:="=Re-Submitted"
'
Set Rng = Range("B2").Resize(LastRow - 1)
Workbooks(WBA.Name).Close False
Workbooks.Open Filename:=WBB.Path
'Syntax problem here
Rng.Copy .....
Workbooks(WBB.Name).Close False
End Sub

Help appreciated
J.P.
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      9th Sep 2008
Untested, but it did compile:

Option Explicit
Sub Import2()

Dim WBA As Workbook
Dim WBB As Workbook
Dim LastRow As Long
Dim Rng As Range
Dim myPath As String
Dim RngToCopy As Range
Dim DestCell As Range

myPath = "P:\invoices"
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If
Set WBA = Nothing
On Error Resume Next
Set WBA = Workbooks.Open(myPath & "A.xls")
On Error GoTo 0

If WBA Is Nothing Then
MsgBox "WBA wasn't found!"
Exit Sub
End If
Set WBB = Nothing
On Error Resume Next
Set WBB = Workbooks.Open(myPath & "b.xls")
On Error GoTo 0
If WBB Is Nothing Then
MsgBox "WBb wasn't found!"
Exit Sub
End If

'change to the correct sheet name
With WBA.Worksheets("sheet1")
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
.AutoFilterMode = False
'just filter by column B
.Range("b1:b" & LastRow).AutoFilter _
field:=1, criteria:="Open", _
Operator:=xlOr, Criteria2:="Re-Submitted"

If .AutoFilter.Range.Columns(1) _
.SpecialCells(xlCellTypeVisible).Cells.Count = 1 Then
MsgBox "Only headers are visible"
Exit Sub
End If

With .AutoFilter.Range
Set RngToCopy = .Resize(.Rows.Count, 1).Offset(1, 0) _
.SpecialCells(xlCellTypeVisible).EntireRow
End With
End With

'change to the correct sheet name
With WBB.Worksheets("sheet2")
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set DestCell = .Cells(LastRow + 1, "A")
End With

RngToCopy.Copy _
Destination:=DestCell

'close the sending workbook without saving
WBA.Close savechanges:=False

'I would think you'd want to save your changes in WBB!
WBB.Close savechanges:=True

End Sub

Test it before you trust it!!!!


u473 wrote:
>
> I need to copy the result from Autofilter on Source Workbook A in
> Destination Workbook B Sheet1
> My VBA AutoFilter Code is in an external Menu Workbook
> .
> Sub Import()
> Dim fso As Object
> Dim Source As Object ' Source Folder path
> Dim Dest As Object ' Destination Folder Path
> Dim WBA as Object ' Source Workbook
> Dim WBB as object ' Destination Workbook
> Dim LastRow As Long, Dim Rng As Range
> '
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set Source = fso.GetFolder("P:\Invoices")
> WBA = "A.xls"
> WBB = "B.xls"
> Workbooks.Open Filename:=WBA.Path
> On Error GoTo 0
> LastRow = Range("B65335").End(xlUp).Row: Range("B2").Select
> ActiveSheet.Range("$A$2:$W$65535").AutoFilter Field:=2,
> Criteria1:="=Open", Operator:=xlOr, Criteria2:="=Re-Submitted"
> '
> Set Rng = Range("B2").Resize(LastRow - 1)
> Workbooks(WBA.Name).Close False
> Workbooks.Open Filename:=WBB.Path
> 'Syntax problem here
> Rng.Copy .....
> Workbooks(WBB.Name).Close False
> End Sub
>
> Help appreciated
> J.P.


--

Dave Peterson
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel-how to link source workbook to copy of destination workbook D Lynn Microsoft Excel Worksheet Functions 1 29th May 2008 05:36 PM
HELP: Use the autofilter result on one workbook to filter the next list on another workbook Kathy Houtami Microsoft Excel Programming 5 12th Sep 2007 01:15 AM
HELP: Use the autofilter result on one workbook to filter the next list on another workbook Kathy Houtami Microsoft Excel Worksheet Functions 6 11th Sep 2007 05:29 AM
How do I post values from source workbook to destination workbook =?Utf-8?B?UmFnaGF2ZW5kcmEgS3Vsa2Fybmk=?= Microsoft Excel Programming 1 4th May 2005 01:31 PM
how to copy succeeding rows from source workbook to destination w. =?Utf-8?B?Y2hyaXM=?= Microsoft Excel Programming 0 8th Mar 2005 01:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:13 AM.