VBA code stuck (extract data from worksheet)

chingg1011

New Member
Joined
Oct 8, 2021
Messages
18
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Hi Expert,

VBA purpose: daily run data from an excel worksheet to extract top 20 loan data into new worksheet for accumulative base.
Problem encountered: below code was stuck from error code 91 in yellow line, but can be successful in another user's note book.
Help; can any expert help to solve out below problem for running smooth this vba task ?

1716095619683.png
1716095585706.png

1716095649480.png


'Dennis Mak, last update on May 2024


Sub Extract_DailyLoan()
'Excel VBA code to loop through files in a folder with Excel VBA




Dim MyFolder As String, MyFile As String, workbookname As String


'Opens a file dialog box for user to select a folder
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
MyFolder = .SelectedItems(1)
Err.Clear
End With

'stops screen updating, calculations, events, and statsu bar updates to help code run faster
'you'll be opening and closing many files so this will prevent your screen from displaying that

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'This section will loop through and open each file in the folder you selected
'and then close that file before opening the next file

MyFile = Dir(MyFolder & "\", vbReadOnly)

Do While MyFile <> ""
DoEvents
On Error GoTo 0
Set OtherWorkbook = Workbooks.Open(MyFolder & "\" & MyFile, UpdateLinks:=0, ReadOnly:=1)
OtherWorkbook.worksheets("sheet1").Activate
OtherWorkbook.worksheets("sheet1").Select


'start doing the main job of copying data from daily loan table file
Range("A1:AX12909").Select
Range("A2").Activate
Application.CutCopyMode = False
Selection.AutoFilter
Selection.End(xlUp).Select
Columns("Z:Z").Select
ActiveWorkbook.worksheets("sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.worksheets("sheet1").AutoFilter.Sort.SortFields.Add2 Key:= _
Range("Z1:Z12909"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal

With ActiveWorkbook.worksheets("sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.AutoFilter
Selection.End(xlToLeft).Select
Range("A2:AX21").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Loan table top 20").Select
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues



Workbooks(MyFile).Close SaveChanges:=False
MyFile = Dir

Loop

'turns settings back on that you turned off before looping folders

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Your code needs a bit of a rewrite / cleanup but for a quick fix try replacing the first:
VBA Code:
Selection.AutoFilter
with this
VBA Code:
If Sheet1.AutoFilterMode = False Then Selection.AutoFilter
 
Upvote 0

Forum statistics

Threads
1,224,799
Messages
6,181,040
Members
453,014
Latest member
Chris258

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