Moving data from one workbook to another based on criteria

Exceldud

New Member
Joined
Jan 30, 2022
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
I am trying to move data from one workbook to another based on value selected in a drop down list. The code uses the selection from the list and filters the data and then moves to another workbook. The below code was working earlier but now it gives a Autofilter method of range class failed on the line marked below. Please help.

Sub moveData()
Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht, shtButtons, CustName, PathOnly, destSht, newFile As String
Dim newBook As Excel.Workbook
Dim Workbk As Excel.Workbook
PathOnly = ThisWorkbook.Path
newFile = "SalesDashboard.xlsm"
'Specify sheet name in which the data is stored
sht = "Sales"
shtButton = "Buttons"
'Specify destination sheet name
destSht = "SalesOrder"
Set newBook = Workbooks.Open(PathOnly & "\" & newFile)
newBook.Activate
newBook.Worksheets(destSht).Activate
Selection.Delete Shift:=xlUp
'Workbook where VBA code resides
Set Workbk = ThisWorkbook
Workbk.Activate
With Workbk.Sheets(shtButton)
Set x = .Range("H6")
End With
'Filter based on Customer Name in Column B
last = Workbk.Sheets(sht).Cells(Rows.Count, "B").End(xlUp).Row
With Workbk.Sheets(sht)
Set rng = .Range("A1:T" & last)
End With
With rng
.AutoFilter ' THIS IS WHERE THE CODE FAILS
.AutoFilter Field:=2, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy
newBook.Activate
newBook.Worksheets(destSht).Activate
Worksheets(destSht).Range("A1").Select
ActiveSheet.Paste
End With
ActiveWorkbook.SaveAs Filename:=(PathOnly & "\" & x & "_Dashboard_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xlsm")
' Turn off filter
Workbk.Sheets(sht).AutoFilterMode = False
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,213,528
Messages
6,114,154
Members
448,553
Latest member
slaytonpa

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