Home > Software engineering >  VBA read the file
VBA read the file

Time:10-19

Use Excel VBA operation

I'd like to add a button in excel, click on the button appears select the file dialog box, select the file, can excel file will be selected including fixed column shows the excel file to me, ask ace to give directions.

CodePudding user response:

 Option Explicit 

Sub Main ()

'Declare a variable as a FileDialog object.
Dim fd As FileDialog

'Create a FileDialog object as a File Picker dialog box.
The Set of fd=Application. FileDialog (msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. My the path is a String,
'the variable must be a Variant because For Each... Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As the Variant

'the Use of a With... End With block to reference the FileDialog object.
With fd

'Use the Show method to display the File Picker dialog box and return the user' s action.
'The user pressed The action button.
If the Show=1 Then

'Step through each string in the FileDialogSelectedItems collections.
For Each vrtSelectedItem In. SelectedItems

'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions provides that want to work with this path.
'This example simple displays the path in a message box.
MsgBox "The path is:" & amp; VrtSelectedItem

Next vrtSelectedItem
'The user pressed The Cancel.
The Else
End the If
End With

'Set the object variable to Nothing.
The Set of fd=Nothing

End Sub
  •  Tags:  
  • VBA
  • Related