Excel另存为框

像帕尔

我希望你们能帮助我。我对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
阿克斯·格林德

您必须包括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的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章