Open a workbook using FileDialog and manipulate it in Excel VBA
Thankyou Frank.i got the idea.Here is the working code.
Option ExplicitPrivate Sub CommandButton1_Click() Dim directory As String, fileName As String, sheet As Worksheet, total As Integer Dim fd As Office.FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd .AllowMultiSelect = False .Title = "Please select the file." .Filters.Clear .Filters.Add "Excel 2003", "*.xls?" If .Show = True Then fileName = Dir(.SelectedItems(1)) End If End With Application.ScreenUpdating = False Application.DisplayAlerts = False Workbooks.Open (fileName) For Each sheet In Workbooks(fileName).Worksheets total = Workbooks("import-sheets.xlsm").Worksheets.Count Workbooks(fileName).Worksheets(sheet.Name).Copy _ after:=Workbooks("import-sheets.xlsm").Worksheets(total) Next sheet Workbooks(fileName).Close Application.ScreenUpdating = True Application.DisplayAlerts = TrueEnd Sub
Unless I misunderstand your question, you can just open a file read only.Here is a simply example, without any checks.
To get the file path from the user use this function:
Private Function get_user_specified_filepath() As String 'or use the other code example here. Dim fd As Office.FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) fd.AllowMultiSelect = False fd.Title = "Please select the file." get_user_specified_filepath = fd.SelectedItems(1)End Function
Then just open the file read only and assign it to a variable:
dim wb as workbookset wb = Workbooks.Open(get_user_specified_filepath(), ReadOnly:=True)