我希望你们可以帮助我。我不太了解vb编码,只知道一些极端的基础知识。
我试图将我的destfile设为= Application.FileDialog,但我不太清楚如何去做。 我知道如何为我当前的代码创建默认路径目标,但我宁愿浏览保存为框。 偶然的任何帮助?
这是我目前的代码。
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
答案 0 :(得分:1)
您必须包含Microsoft Office 14.0对象库。
然后,您将能够创建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
上述代码允许用户选择文件并返回文件的地址。
如果您希望用户仅选择代码类似的文件夹,但会更改几行。
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
这适用于Excel宏中的VBA。