【发布时间】:2014-12-17 21:47:30
【问题描述】:
我希望你们能帮助我。我对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
【问题讨论】:
-
请在代码前加4个空格,这样看起来不错。 (我尝试编辑您的问题,但在某处犯了错误)。
-
是的,我最初是使用 bbcode 发布的,然后才弄清楚。谢谢。
-
这是旧的 VB 代码,而不是 Excel VBA(至少十年左右没有)。也许在这里搜索
[vba] FileDialog会给您提供使用它的示例,然后您可以更新它以实际使用 Excel 对 CSV 文件的内置 SaveAs 支持。
标签: vba excel filedialog