Sub CopyFilterPaste() error

Gtasios4

Board Regular
Joined
Apr 21, 2022
Messages
80
Office Version
  1. 2021
Platform
  1. Windows
Hi all,

I have a problem with my VBA code below. The point is in the assigned macro in the below xlsm the user to get filtered data from a source xlsx based on today's date, once it press it.

The source workbook will be updated everyday thus the range will be for all G column.
I want to open in hidden mode the source --> filtering the results of column G (today's date) and pasting the results (if found) in the destination xlsm A2 : ... .

1675958599160.png

VBA Code:
Sub CopyFilterPaste()

'Declare variables for the source and destination workbook
Dim sourceWB As Workbook, destWB As Workbook

'Open the source workbook in hidden mode
Set sourceWB = Workbooks.Open("\\argsrv\Users-Data\sales\iq2857\deliveries demo\Deliveries 2022.xlsx", UpdateLinks:=0, ReadOnly:=True, Visible:=False)

'Open the destination workbook
Set destWB = Workbooks.Open("\\argsrv\Users-Data\sales\iq2857\deliveries demo\Test Deliveries.xlsm")

'Filter the source workbook based on today's date in column G
With sourceWB.Sheets("Deliveries")
    .Range("A:G").AutoFilter Field:=7, Criteria1:="=" & Format(Date, "DD/MM/YYYY")
End With

'Determine if there are any visible cells after the filter is applied
If sourceWB.Sheets("Deliveries").Range("A1:G2814").SpecialCells(xlCellTypeVisible).Count = 1 Then
    MsgBox "No deliveries for today", vbOKOnly
    sourceWB.Close SaveChanges:=False
    destWB.Close SaveChanges:=False
    Exit Sub
End If

'Copy the filtered data
sourceWB.Sheets("Deliveries").Range("A2:G100").SpecialCells(xlCellTypeVisible).Copy

'Paste the filtered data into the destination workbook
destWB.Sheets("Today's Deliveries").Range("A2").PasteSpecial xlPasteValues

'Close the source workbook
sourceWB.Close SaveChanges:=False

'Close the destination workbook
destWB.Close SaveChanges:=True

End Sub

The errors that I get:

1675958828693.png


1675958872049.png


Any help would be much appreciated
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
.Visible is not a parameter of Workbooks.Open method
I think the usual method is to open the file, then it becomes the active window, then
ActiveWindow.Visible = False
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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