How to program a command Button to prompt the user to select a file and export it in Excel

 Question:

 

I am completely new to macros and VBA.  want help creating a command button (ActiveX control) to prompt the user to select an Excel workbook and export it to a tab specified in the current workbook. Please help.

Thanks and regards, Amey ..


 Answers:
 

You can use this code to select the file.
findPath = Application.GetOpenFilename( _
        Title:="Select file", _
        filefilter:="Excel files (*.xlsx), *.xlsx, Excel files 97-93 (*.xls), *.xls", _
        MultiSelect:=False)   
If (findPath = "false") Then
    MsgBox ("an error occurred. cancel operation....")
    End
End If

Workbooks.Open Filename:=findPath
WB = ActiveWorkbook.Name
Then to import the content to a tab
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets(1) '1 is the first one. you can do a loop
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
Later select the worksheet you want and paste it. And close it
Workbooks(WB).Close SaveChanges:=False


Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net