【问题标题】:Excel save as boxExcel另存为框
【发布时间】: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


【解决方案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。

【讨论】:

  • 是的,我已经知道那个特定的命令,但是感谢您的帮助。我正在尝试在该框的相同上下文中使用另存为命令。当您转到文件>另存为时,您看到的视图类型基本相同。不是文件>打开。不确定 application.filedialog 是否是运行我想要的正确命令。因此请教你们。
  • 没关系,查看应用程序语法,找到了我需要的正确命令。 msoFileDialogSaveAs。谢谢你们的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 2021-01-23
  • 2020-12-15
  • 1970-01-01
  • 2011-12-21
  • 2010-10-26
  • 2015-11-17
相关资源
最近更新 更多