VBA - Dynamic file selection using File Dialogs and Import Data from selected Excel files
If you have any questions please feel free to comment below the video or email me directly at vbaa2z.team@gmail.com and I will try and come back as soon as possible. Please do not forget to leave a like and subscribe to our channel. Thanks for your support.
OptionExplicitPublicPriorCalcModeAsVariant'https://docs.microsoft.com/en-us/office/vba/api/office.filedialog'https://docs.microsoft.com/en-us/office/vba/api/excel.application.filedialog'MsoFileDialogType can be one of these constants:'msoFileDialogFilePicker. Allows user to select a file.'msoFileDialogFolderPicker. Allows user to select a folder.'msoFileDialogOpen. Allows user to open a file.'msoFileDialogSaveAs. Allows user to save a file.Subselect_import_code()'-----------------------------'Thanks for downloading the code. 'Please visit our channel for a quick explainer on this code.'Feel free to update the code as per your need and also share with your friends.'Channel: Youtube.com/vbaa2z'Download free codes from http://vbaa2z.blogspot.com'Author: L Pamai (vbaa2z.team@gmail.com)'-----------------------------DimvfdAsOffice.FileDialogDimvfd_fileAsVariantDimcurFileNameAsStringDimdestination_wbAsWorkbookSetvfd=Application.FileDialog(msoFileDialogFilePicker)Withvfd.AllowMultiSelect=True.InitialFileName="D:\VBAA2Z Demo\FileDialog\ds files\".Filters.Clear.Filters.Add"All Files","*.xls*".ShowDebug.Print.SelectedItems.CountIf.SelectedItems.Count<>0ThenTurnOnSpeedTrueSetdestination_wb=Workbooks.AddForEachvfd_fileIn.SelectedItemsDebug.PrintTrim(vfd_file)curFileName=vfd_fileDebug.Print"Import status: "&get_data(curFileName,destination_wb)NextElseMsgBox"File not selected"EndIfEndWithdestination_wb.SaveAs"D:\VBAA2Z Demo\FileDialog\ds files\masterWorkbook.xlsx",xlOpenXMLWorkbookSetdestination_wb=NothingTurnOnSpeedFalseEndSubFunctionget_data(wb_path$,xWbAsWorkbook)AsBooleanDimdslrAsLong,new_lrAsLong,paste_des_rowAsLongDimcopyrangeAsRangeDimtagRngAsRangeDimheaderCopiedCntAsLongDimwbAsWorkbookSetwb=Workbooks.Open(wb_path,False,True)Withwbdslr=.Sheets(2).Range("A"&Rows.Count).End(xlUp).RowIfheaderCopiedCnt=0ThenSetcopyrange=.Sheets(2).Range("A1:K"&dslr)headerCopiedCnt=headerCopiedCnt+1ElseSetcopyrange=.Sheets(2).Range("A2:K"&dslr)EndIfcopyrange.CopyEndWithWithxWbpaste_des_row=.Sheets(1).Range("A"&Rows.Count).End(xlUp).Row+1.Sheets(1).Range("B"&paste_des_row).PasteSpecialPaste:=xlPasteValuesAndNumberFormatsnew_lr=.Sheets(1).Range("B"&Rows.Count).End(xlUp).RowSettagRng=.Sheets(1).Range(Range(Cells(paste_des_row,1),Cells(new_lr,1)).Address)tagRng.Value=wb.NameApplication.CutCopyMode=FalseSettagRng=NothingEndWithSetcopyrange=Nothingwb.CloseFalseSetwb=Nothingget_data=TrueEndFunctionPublicFunctionTurnOnSpeed(xAsBoolean)Ifx=TrueThenWithApplicationPriorCalcMode=Application.Calculation.ScreenUpdating=False.DisplayAlerts=False.EnableEvents=False.Cursor=xlWait.Calculation=xlCalculationManualEndWithElseIfx=FalseThenWithApplication.ScreenUpdating=True.DisplayAlerts=True.EnableEvents=True.StatusBar=False.Cursor=xlDefault.Calculation=PriorCalcModeEndWithEndIfEndFunction
Option Explicit
'please visit for our channel for more tutorials -- > https://www.youtube.com/vbaa2z'find connectionstring string for any db_ at -- > http://connectionstring.com/PublicConst Joinx$ = " IN '"PublicFunction dbCon_Str() AsString'-----------------------------'Thanks for downloading the code. 'Please visit our channel for a quick explainer on this code.'Feel free to update the code as per your need and also share with your friends.'Channel: Youtube.com/vbaa2z'Download free codes from http://vbaa2z.blogspot.com'Subscribe channel: youtube.com/vbaa2z'Author: L Pamai (vbaa2z.team@gmail.com)'-----------------------------
dbCon_Str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath & _
"C:\My Documents\bulk_loader\finDB.accdb" & ";Jet OLEDB:Database Password=;"EndFunctionFunction xl_ext() AsString
xl_ext$ = Joinx$ & ThisWorkbook.FullName & "' 'Excel 8.0;'"EndFunctionFunction connection_center(sql$) AsLong'youtube.com/vbaa2zDim aff_rc AsLong, cn As ADODB.Connection
OnErrorGoTo err_hndler
Set cn = New ADODB.Connection
With cn
.Open dbCon_Str
.CursorLocation = adUseClient
.Execute (sql$), aff_rc
EndWith
connection_center = aff_rc
closeCon:
IfCBool(cn.State And adStateOpen) = TrueThen cn.Close
Set cn = NothingExitFunction
err_hndler:
connection_center = False
Debug.Print Err.Description & Now()
GoTo closeCon
EndFunctionSub bulk_upload()
'-----------------------------'Thanks for downloading the code. 'Please visit our channel for a quick explainer on this code.'Feel free to update the code as per your need and also share with your friends.'Channel: Youtube.com/vbaa2z'Download free codes from http://vbaa2z.blogspot.com'Author: L Pamai (vbaa2z.team@gmail.com)'-----------------------------Dim mysql AsString
Debug.Print Now()
mysql = "INSERT into DailyT SELECT * FROM [LoadSh$] " & xl_ext & "WHERE (((LoadSh$.T_type) = 'Inflow') AND ((LoadSh$.Amount) > 80000));"
Debug.Print connection_center(mysql)
EndSub
VBA to check if User has access to Network/Directory.
Option Explicit
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on how to use this code.
'Feel free to update the code as per your need and also share with your friends.
'Download free codes from http://vbaa2z.blogspot.com
'Support our channel: youtube.com/vbaa2z
'Author: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------
Debug.Print FolderIsWriteable("D:\")
Function FolderIsWriteable(sFolder As String) As Boolean
On Error Resume Next
FolderIsWriteable = (GetAttr(sFolder) And vbReadOnly) <> 1
Exit Function
End Function
Hello friends, all relevant materials for this topic/tutorial can be downloaded from here. Please support us by subscribing to our channel and sharing them with your friends.
If you have any questions/feedback/tutorial request, please you can email me directly vbaa2z.team@gmail.com or comment on YouTube Video (blog comments are not actively monitored).
OptionCompareDatabaseOptionExplicitPrivateConstTIME_ZONE_ID_STANDARDAsLong=1PrivateConstTIME_ZONE_ID_DAYLIGHT&=2DimdteStartAsDate,dteFinishAsDateDimdteStoppedAsDate,dteElapsedAsDateDimboolStopPressedAsBoolean,boolResetPressedAsBooleanPrivateTypeSYSTEMTIMEwyearAsIntegerwmonthAsIntegerwdayofweekAsIntegerwhourAsIntegerwminuteAsIntegerwsecondAsIntegerwmillisecondsAsIntegerEndTypePrivateTypeTIME_ZONE_INFORMATIONbiasAsLongStandardname(1To63)AsBytestandarddateAsSYSTEMTIMEstandardbiasAsLongDaylightname(0To63)AsBytedaylightdateAsSYSTEMTIMEdaylightbiasAsLongEndTypePrivateDeclareFunctionGetTimeZoneInformationLib"kernel32"(IpTimeZoneInformationAsTIME_ZONE_INFORMATION)AsLongPublicresetMeAsBooleanPublicmyValAsVariantPublicFunctionmytime()AsString'-----------------------------'Thanks for downloading the code. 'Please visit our channel for a quick explainer on how to use this code.'Feel free to update the code as per your need and also share with your friends.'Download free codes from http://vbaa2z.blogspot.com'Support our channel: youtube.com/vbaa2z'Author: L Pamai (vbaa2z.team@gmail.com)'-----------------------------DimtziAsTIME_ZONE_INFORMATIONDimgmtAsDateDimdwbiasAsLongDimtmpAsStringSelectCaseGetTimeZoneInformation(tzi)CaseTIME_ZONE_ID_DAYLIGHTdwbias=tzi.bias+tzi.daylightbiasCaseElsedwbias=tzi.bias+tzi.standardbiasEndSelectgmt=DateAdd("n",dwbias,Now+TimeSerial(5,30,0))tmp=Format$(gmt,"MM/DD/YYYY HH:MM:SS AM/PM")mytime=tmpEndFunction
Below code example will Terminate firefox browser. To use this code to end other processes load your Windows task Manager and locate the process name and replace firefox.exe with your process name.
Option Explicit
Sub test_TerminateProcess()
TerminateProcess ("firefox.exe")
End Sub
Function TerminateProcess(app_exe As String)
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on this code.
'Feel free to update the code as per your need and also share with your friends.
'Channel: Youtube.com/vbaa2z
'Download free codes from http://vbaa2z.blogspot.com
'Autor: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------
Dim Process As Object
For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & app_exe & "'")
Process.Terminate
Next
End Function
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on this code.
'Feel free to update the code as per your need and also share with your friends.
'Channel: Youtube.com/vbaa2z
'Download free codes from http://vbaa2z.blogspot.com
'Autor: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------
Public Const con1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\path\FinData2016.accdb;Jet OLEDB:Database Password=yourPassword;"
Short video series will teach you how to integrate Excel and Access Database to create a business solution / automation. In detail and easy to follow tutorial you’ll learn how to use VBA to connect to tb, table, add, sync, update, delete records / data….have fun!
Are you receiving "Not a Valid Password - Run-time Error 3031" Excel ADO VBA Error Message even when correct password are updated / passed in the argument?
You will encounter this error if you’ve have done MS office/Access upgrade from 2007 to 2010/2013/2016. Existing Code and databases should work.
If you’re creating new database and setting a password or changing the password of existing database using new office (MS ACCESS) you’ll face this issue when connecting via VBA ADO.
Follow below steps to troubleshoot this issue:
1.Fire up Access application (any access file)
2.Click on File and then Options
3.Click on Client Settings
4.Go to Advanced section and check "use legacy encryption (good for reverse compatibility and multi-user database)"
5.Restart Access (by closing all access database currently)
6.Set new password
7.Run your ADO Code to connect to database. It should be working now.
'-----------------------------
'Please visit our channel for a quick explainer on this code.
'Feel free to update the code as per your need and also share with your friends.
'Channel: Youtube.com/vbaa2z
'Download free codes from http://vbaa2z.blogspot.com
'Autor: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------
You can use the below VBA codeto list specified files types from Directory. Below demo sample code will list all .mp3 files in folder/directory "D:\Music\5 Seconds Of Summer\" If you want to list other files type for example .xlsm files change "*.mp3" to "*.xlsm", or to list all files irrespective change it "*.mp3" to "*.*". Please share or comment below if you have any questions.
Const vDir As String = "D:\Music\5 Seconds Of Summer\"
Const vPttrn As String = "*.mp3"
Dim vFile As String
Sub test_list_mp3_files()
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on this code.
'Feel free to update the code as per your need and also share with your friends.
'Channel: Youtube.com/vbaa2z
'Download free codes from http://vbaa2z.blogspot.com
'Autor: L Pamai (vbaa2z.team@gmail.com)
'-----------------------------
vFile = Dir(vDir & vPttrn, vbNormal)
Do While Len(vFile) > 0
Debug.Print vFile
vFile = Dir
Loop
End Sub