简体   繁体   English

Excel VBA:将数据/文件保存为另一种没有宏的格式

[英]Excel VBA: Saving data/file to another format without macro

I have this userform 我有这个用户表格

用户表格

It is fully fuctioning but I want to add a feature that is sending the file as .xlsx or .txt so I can remove the macro from the file. 这是完全有用的功能,但是我想添加一个功能,以.xlsx或.txt格式发送文件,以便可以从文件中删除宏。

I searched the internet for days and come up to a procedure that I need to make a 3 process to save it to another format. 我在互联网上搜索了几天,然后想出一个程序,我需要进行3个过程以将其保存为另一种格式。 The process I come up is listed below: 我列出的过程如下:

1. .SaveCopyAs 1. .SaveCopyAs

Copy(Filename).(Same format) 复制(文件名)。(相同格式)

  1. .Open

    Existing File 现有档案

  2. .SaveAs

    (FileName).(Any format) (文件名)。(任何格式)

And delete the SaveCopyAs file to avoid redundancy Or catch the temporary file to SaveAs another file format? 并删除SaveCopyAs文件以避免冗余,还是将临时文件捕获为SaveAs另一种文件格式? Every input should be save after clicking ok button and to overwrite the existing file. 单击确定按钮后,应保存每个输入并覆盖现有文件。

Can someone tell me if I'm making the right approach to my problem? 有人可以告诉我我对问题的解决方法是否正确? Thanks. 谢谢。

I have had a similar issue, my solution involved copying the Sheets into a new workbook then renaming them accordingly and saving that workbook with a specific name: 我遇到了类似的问题,我的解决方案涉及将工作表复制到新工作簿中,然后相应地重命名工作表并使用特定名称保存该工作簿:

ans = MsgBox("Would you like to export the Report?", vbYesNo)
    Select Case ans
    Case vbYes
        FPath = "C:\...\...\... Daily Report"
        FName = "Report" & ".xlsx"

        Set NewBook = Workbooks.Add

        ThisWorkbook.Sheets("Sheet2").Copy Before:=NewBook.Sheets(1)
        ThisWorkbook.Sheets("Sheet3").Copy Before:=NewBook.Sheets(1)
        Application.DisplayAlerts = False
        'NewBook.Sheets("Sheet1").Delete to delete the first empty sheet on the new workbook

        If Dir(FPath & "\" & FName) <> "" Then
            MsgBox "File " & FPath & "\" & FName & " already exists"
        Else
            NewBook.SaveAs Filename:=FPath & "\" & FName
        End If

        NewBook.Close False
        CurrentReport = FPath & "\" & FName
    Case vbNo
        Exit Sub
    End Select

Or to append the data to a text file, each time the user clicks "OK": 或每次用户单击“确定”将数据附加到文本文件时:

Sub VBA_to_append_existing_text_file()
Dim strFile_Path As String
strFile_Path = "C:\temp\test.txt" ‘Change as per your test folder and exiting file path to append it.
Open strFile_Path For Append As #1
Write #1, "This is my sample text" ' add entered data here
Close #1
End Sub 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM