Obtenir l'location et le nom du file pour Excel VBA

Je crée un programme VBA qui copyra une colonne d'un file à l'autre.

Le code actuel fonctionne, mais je souhaite le changer à l'endroit où une invite apparaîtra et requestr à l'user l'location du file et le nom / l'extension. Cette input sera imscope comme location de file pour la fonction Workbooks.Open et va de là.

Comment puis-je créer une invite pour requestr à l'user de saisir l'location du file et le nom du file Excel souhaité, et de le faire entrer dans la fonction Workbooks.Open?

Code:

Sub Macro1() Dim wb1 As Workbook Dim wb2 As Workbook MsgBox "Now converting data from Incident Data to Specific Data " 'Set it to be the file location, name, and extension of the Call Data CSV Set wb1 = Workbooks.Open("Z:\xxxx\Call Data - Copy.csv") 'Set it to be the file location of the Working File Set wb2 = Workbooks.Open("Z:\xxxx\Working File.xlsx") wb1.Worksheets(1).Columns("E").Copy wb2.Worksheets(1).Columns("A") wb1.Worksheets(1).Columns("I").Copy wb2.Worksheets(1).Columns("Q") wb1.Worksheets(1).Columns("AE").Copy wb2.Worksheets(1).Columns("R") wb1.Worksheets(1).Columns("BD").Copy wb2.Worksheets(1).Columns("F") wb2.Close SaveCahnges:=True wb1.Close SaveChanges:=True End Sub 

J'irais avec FileDialog pour sélectionner un file d'input:

 Dim fDialog As FileDialog, result As Integer Set fDialog = Application.FileDialog(msoFileDialogFilePicker) fDialog.AllowMultiSelect = False fDialog.title = "Select a file" fDialog.InitialFileName = "C:\" fDialog.Filters.Clear fDialog.Filters.Add "Excel files", "*.xlsx" 'Show the dialog. -1 means a file has been successfully selected If fDialog.Show = -1 Then Debug.Print fDialog.SelectedItems(1) End If 

Pour sauvegarder, vous pouvez vous référer à cette publication

MODIFIER:

Pour l'utiliser dans Workbooks.Open vous juste faire quelque chose comme ce qui suit:

 Dim fname As Ssortingng If fDialog.Show = -1 Then fname=fDialog.SelectedItems(1) Else MsgBox("Filename selection error") Exit Sub End If Set wb1 = Workbooks.Open(fname)