Excel保存为框

我希望你们能帮助我 我不是很了解vb编码,只是一些极端的基础知识。

我试图让我的destfile = Application.FileDialog,但我不太清楚如何去做。 我知道如何使默认path目标到我当前的代码,但我宁愿浏览保存为框。 任何偶然的帮助?

这是我目前的代码。

Sub QuoteCommaExport() ' Dimension all variables. Dim DestFile As String Dim FileNum As Integer Dim ColumnCount As Integer Dim RowCount As Integer ' Prompt user for destination file name. DestFile = InputBox("Enter the destination filename" _ & Chr(10) & "(with complete path):", "Quote-Comma Exporter") ' Obtain next free file handle number. FileNum = FreeFile() ' Turn error checking off. On Error Resume Next ' Attempt to open destination file for output. Open DestFile For Output As #FileNum ' If an error occurs report it and end. If Err <> 0 Then MsgBox "Cannot open filename " & DestFile End End If ' Turn error checking on. On Error GoTo 0 ' Loop for each row in selection. For RowCount = 1 To Selection.Rows.Count ' Loop for each column in selection. For ColumnCount = 1 To Selection.Columns.Count ' Write current cell's text to file with quotation marks. Print #FileNum, StrConv("""" & Selection.Cells(RowCount, _ ColumnCount).Text & """", 1); ' Check if cell is in last column. If ColumnCount = Selection.Columns.Count Then ' If so, then write a blank line. Print #FileNum, Else ' Otherwise, write a comma. Print #FileNum, ","; End If ' Start next iteration of ColumnCount loop. Next ColumnCount ' Start next iteration of RowCount loop. Next RowCount ' Close destination file. Close #FileNum End Sub 

您必须包含Microsoft Office 14.0对象库。

那么你将有能力创build一个Application.FileDialog框。

一个fileDialog的代码看起来像这样:

 Dim fDialog Dim fLocation Dim varFile as variant Set fDialog = Application.FileDialog(msoFileDialogFilePicker) With fDialog .Filters.Add "Excel", "*.xlsx, *.xlsb, *.xlsm, *.xls", 1 .AllowMultiSelect = False .Title = "Please select the file you want to use" If .Show = True Then ImportCK = MsgBox("Are you you want to use this file?", vbYesNo + vbQuestion, "Saving...") If ImportCK = vbYes Then For Each varfile In .SelectedItems fLocation = varfile Next End If Else MsgBox "You clicked Cancel.", vbExclamation + vbOKOnly, "Canceled" 'Do something on cancel End If End With 

上面的代码允许用户select一个文件并返回文件的地址。

如果你想让用户只select一个文件夹,代码是类似的,但是几行就被改变了。

 Dim fDialog Dim fLocation Dim varFile as variant 'Create a folder picker Set fDialog = Application.FileDialog(msoFileDialogFolderPicker) With fDialog .Title = "Please select the folder you want to use" If .Show = True Then ImportCK = MsgBox("Are you you want to use this folder?", vbYesNo + vbQuestion, "Saving...") If ImportCK = vbYes Then For Each varfile In .SelectedItems fLocation = varfile Next End If Else MsgBox "You clicked Cancel.", vbExclamation + vbOKOnly, "Canceled" 'Do something on cancel End If End With 

这是Excelmacros中的VBA。