简体   繁体   English

使用宏将数据文件导入新选项卡时,如何提示用户选择文件?

[英]How do I prompt the user to select the file, when using macro to import a data file into a new tab?

I have a macro that is currently creates a new sheet, and imports another excel file into this new sheet. 我有一个宏,当前正在创建一个新表,并将另一个Excel文件导入到该新表中。

Data from this sheet is than pulled into other areas of the workbook. 然后,此工作表中的数据将被拉入工作簿的其他区域。

The file that is being imported will constantly have a different file name. 导入的文件将始终具有不同的文件名。 How do I adjust the below code to prompt the user to select the file? 如何调整以下代码以提示用户选择文件? (The directory will not change). (目录不会更改)。

Sub ImportDemand() Sheets.Add 子ImportDemand()表格。添加

Sheets(2).Select
Sheets(2).Name = "ImportedDemand"
Range("E42").Select
With ActiveSheet.QueryTables.Add(Connection:=Array( _
    "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=\\Folder\ImportFile_2011.04.05.xls;Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System d" _
    , _
    "atabase="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;" _
    , _
    "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create Sys" _
    , _
    "tem Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Repli" _
    , "ca Repair=False;Jet OLEDB:SFP=False"), Destination:=Range("A1"))
    .CommandType = xlCmdTable
    .CommandText = Array("_All_Demand$")
    .Name = "ImportFile_2011.04.05"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .SourceDataFile = _
    "\\Folder\ImportFile_2011.04.05.xls"
    .Refresh BackgroundQuery:=False
End With

End Sub 结束子

You can use GetOpenFilename : 您可以使用GetOpenFilename

.SourceDataFile = Application.GetOpenFilename("Excel workbooks (*.xls), *.xls")

Another option is the FileDialog object. 另一个选项是FileDialog对象。 It offers more flexibility. 它提供了更大的灵活性。

Dim fdgOpen As FileDialog
Set fdgOpen = Application.FileDialog(msoFileDialogOpen)
fdgOpen.Title = "Please open a data file..."
fdgOpen.InitialFileName = "C:\MyDocuments\MyDir\"
'Other settings...
fdgOpen.Show
.SourceDataFile = fdgOpen.SelectedItems(1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用宏导入数据文件时,如何提示用户选择文件和图纸? - How do I prompt the user to select the file and sheet, when using macro to import a data file? 如何让用户选择一个已经打开的 Excel 文件来导入数据,然后粘贴到他们选择的文件中? (VBA) - How do I have a user select an already open excel file to import data to, and paste into the file they choose? (VBA) 如何使用宏将文件导入工作簿中的新工作表 - how to import file using macro into a new sheet within a workbook 如何通过一个宏将特定数据从Excel文件导入到文本文件,以及如何将生成的文本文件中的数据导入到新的Excel文件中 - How to import specific data from Excel file to text file and import the data from generated text file into new Excel file by one macro 从文本中获取外部数据时,在 Excel 宏中提示输入文件 - Prompt for file in Excel Macro when getting external data from text 宏以提示用户选择 CSV 文件以导入到工作簿中的现有工作表 - Macro to prompt user to select CSV files for import into existing sheet in workbook 如何编写宏来打开excel文件并将数据粘贴到包含宏的文件中? - How do I write a macro to open an excel file and paste data into the file containing the macro? 提示用户选择多个文件并对所有文件执行相同的操作 - Prompt user to select multiple file and do the same action on the all files 如何通过 VBA 宏将 Pst 文件的结构复制到新的 pst 文件 - How do I copy structure of Pst file to new pst file thru VBA macro 选择新数据时在Excel Graph上运行宏 - Run macro on Excel Graph when I select new data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM