Public Module Program Sub LoopAllExcelFilesInFolder() 'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them 'SOURCE: www.TheSpreadsheetGuru.com Dim wb As Workbook Dim myPath As String Dim myFile As String Dim myExtension As String Dim FldrPicker As FileDialog Dim J As Integer 'Optimize Macro Speed Application.DisplayAlerts = False Application.ScreenUpdating = False Application.EnableEvents = False Application.Calculation = xlCalculationManual 'Retrieve Target Folder Path From User Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker) With FldrPicker .Title = "Select A Target Folder" .AllowMultiSelect = False If .Show <> -1 Then GoTo NextCode myPath = .SelectedItems(1) & "\" End With 'In Case of Cancel NextCode: myPath = myPath If myPath = "" Then GoTo ResetSettings 'Target File Extension (must include wildcard "*") myExtension = "*.xls*" 'Target Path with Ending Extention myFile = Dir(myPath & myExtension) 'Loop through each Excel file in folder Do While myFile <> "" 'Set variable equal to opened workbook Set wb = Workbooks.Open(Filename:=myPath & myFile) 'Ensure Workbook has opened before moving on to next line of code DoEvents 'Copy paste data from DPR OGWlastrow = Cells(3, 21) + 3 Processuptimelastrow = OGWlastrow + 10 BGCWIPuptimelastrow = OGWlastrow + 379 ThisWorkbook.Sheets("Oil & Gas Forecast").Range("X" & OGWlastrow & ":AJ" & (OGWlastrow + 1)).FillDown ThisWorkbook.Sheets("Oil & Gas Forecast").Cells(OGWlastrow + 1, 26).Value = wb.Worksheets(1).Cells(18, 9) ThisWorkbook.Sheets("Oil & Gas Forecast").Cells(OGWlastrow + 1, 31).Value = wb.Worksheets(1).Cells(22, 9) ThisWorkbook.Sheets("Oil & Gas Forecast").Cells(OGWlastrow + 1, 32).Value = wb.Worksheets(1).Cells(101, 9) / 1000000 ThisWorkbook.Sheets("Oil & Gas Forecast").Cells(OGWlastrow + 1, 33).Value = wb.Worksheets(1).Cells(104, 9) / 1000000 ThisWorkbook.Sheets("Oil & Gas Forecast").Cells(OGWlastrow + 1, 34).Value = wb.Worksheets(1).Cells(102, 9) / 1000000 + wb.Worksheets(1).Cells(103, 9) / 1000000 ThisWorkbook.Sheets("Water Injection Forecast").Range("C" & OGWlastrow & ":L" & (OGWlastrow + 1)).FillDown ThisWorkbook.Sheets("Water Injection Forecast").Cells(OGWlastrow + 1, 11).Value = wb.Worksheets(1).Cells(24, 9).Value ThisWorkbook.Sheets("Process Uptime").Range("A" & Processuptimelastrow & ":E" & (Processuptimelastrow + 1)).FillDown ThisWorkbook.Sheets("Process Uptime").Cells(Processuptimelastrow + 1, 2).Value = 24 ThisWorkbook.Sheets("WI Uptime").Range("A" & BGCWIPuptimelastrow - 365 & ":O" & (BGCWIPuptimelastrow - 364)).FillDown ThisWorkbook.Sheets("WI Uptime").Cells(BGCWIPuptimelastrow - 364, 3).Value = wb.Worksheets(1).Cells(395, 4).Value ThisWorkbook.Sheets("WI Uptime").Cells(BGCWIPuptimelastrow - 364, 4).Value = wb.Worksheets(1).Cells(396, 4).Value ThisWorkbook.Sheets("WI Uptime").Cells(BGCWIPuptimelastrow - 364, 5).Value = wb.Worksheets(1).Cells(397, 4).Value ThisWorkbook.Sheets("WI Uptime").Cells(BGCWIPuptimelastrow - 364, 6).Value = wb.Worksheets(1).Cells(398, 4).Value ThisWorkbook.Sheets("WI Uptime").Cells(BGCWIPuptimelastrow - 364, 7).Value = wb.Worksheets(1).Cells(399, 4).Value ThisWorkbook.Sheets("WI Uptime").Cells(BGCWIPuptimelastrow - 364, 8).Value = wb.Worksheets(1).Cells(400, 4).Value ThisWorkbook.Sheets("BGC Uptime").Range("A" & BGCWIPuptimelastrow & ":M" & (BGCWIPuptimelastrow + 1)).FillDown ThisWorkbook.Sheets("BGC Uptime").Cells(BGCWIPuptimelastrow + 1, 3).Value = wb.Worksheets(1).Cells(352, 4).Value ThisWorkbook.Sheets("BGC Uptime").Cells(BGCWIPuptimelastrow + 1, 4).Value = wb.Worksheets(1).Cells(353, 4).Value ThisWorkbook.Sheets("BGC Uptime").Cells(BGCWIPuptimelastrow + 1, 5).Value = wb.Worksheets(1).Cells(354, 4).Value ThisWorkbook.Sheets("BGC Uptime").Cells(BGCWIPuptimelastrow + 1, 6).Value = wb.Worksheets(1).Cells(355, 4).Value Application.Calculation = xlCalculationAutomatic Application.Calculation = xlCalculationManual 'Save and Close Workbook wb.Close SaveChanges:=True 'Ensure Workbook has closed before moving on to next line of code DoEvents 'Get next file name myFile = Dir Loop 'Message Box when tasks are completed MsgBox "Task Complete!" ResetSettings: 'Reset Macro Optimization Settings Application.DisplayAlerts = True Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub End Module
Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net
. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub