PDA

View Full Version : VBA Browse multiple workbooks how to short the code....



malleshg24
09-22-2019, 01:10 PM
Hi Team,


I need your help plz assist, I am using below code to browse Multiple required Inputworkbooks,
is there a way to shorten this code, by creating single function.


Thanks in Advance !


Sub GetWorkbook1()
Set myfile = Application.FileDialog(msoFileDialogOpen)
With myfile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
FileSelected = .SelectedItems(1)
End With


ActiveSheet.Range("b4") = FileSelected
End Sub


Regards,
mg

malleshg24
09-22-2019, 01:12 PM
Currently I am using above code multiple times for browsing each workbook.

Paul_Hossler
09-22-2019, 01:22 PM
Easiest thing would be to make it modular and use a sub for all 4 files



Option Explicit


Private Sub GetAnyWorkbook(r As Range)
With Application.FileDialog(msoFileDialogOpen)
.Title = "Choose File"
.AllowMultiSelect = False

If .Show <> -1 Then Exit Sub


r.Cells(1, 1).Value = .SelectedItems(1)
End With


End Sub




Sub GetWorkbook1()
Call GetAnyWorkbook(ActiveSheet.Range("B4"))
End Sub

Sub GetWorkbook2()
Call GetAnyWorkbook(ActiveSheet.Range("B6"))
End Sub

Sub GetWorkbook3()
Call GetAnyWorkbook(ActiveSheet.Range("B8"))
End Sub

Sub GetWorkbook4()
Call GetAnyWorkbook(ActiveSheet.Range("B10"))
End Sub

malleshg24
09-23-2019, 10:23 AM
Hi Paul,
Thanks once again, it worked. :thumb

Regards,
mg