简体   繁体   English

Excel VBA - 导出到 CSV

[英]Excel VBA - Export to CSV

Was after some input that I have so far had trouble figuring out on my own...是在一些输入之后,我至今无法自己弄清楚......

  1. If I wanted the location (ie C:\Users\SB\Documents\CSV Uploads) to be stored in another sheet (LOOKUP DATA), in cell "C13" (Defined Name: FOLDERLOCATION) and used instead of having it in the code, can this be done?如果我希望将位置(即 C:\Users\SB\Documents\CSV Uploads)存储在另一张表(查找数据)中,在单元格“C13”(定义名称:FOLDERLOCATION)中使用,而不是在代码中使用它,这可以做到吗?

  2. While the below works to export the sheet to a CSV file to the folder I have specified, the file ends up being a lot larger than I expected.虽然下面可以将工作表导出到 CSV 文件到我指定的文件夹中,但该文件最终比我预期的要大得多。 The file ends up being over 9mb, The weird thing is if I open, then save the file again and close.文件最终超过 9mb,奇怪的是如果我打开,然后再次保存文件并关闭。 it drops down to around 38kb?它下降到大约38kb? Any ideas what I am doing wrong here?有什么想法我在这里做错了吗?

Thanks in advance, I look forward to seeing what you experts think!提前致谢,期待各位专家的意见!

Sub EXPORTCSV()

Dim Path As String
Dim filename As String

    Sheets("UPLOAD").Visible = True
    Sheets("UPLOAD").Copy
    ActiveWorkbook.SaveAs ("C:\Users\SB\Documents\CSV Uploads\UPLOAD - IB " & Format(Now(), "YYYYMMDD - hh_mm_ss AMPM") & ".csv") _
        , FileFormat:=xlCSV, CreateBackup:=False
 
    ActiveWorkbook.Close

End Sub

With regards to your point 1, yes, you can use a cell to store the root path.关于您的第 1 点,是的,您可以使用单元格来存储根路径。 I have rewritten some of your code for clarity, but if you want to keep the same structure that you already have, just replace the ActiveWorkbook.SaveAs ("C:\Users\SB\Documents\CSV Uploads\UPLOAD - IB " & Format(Now(), "YYYYMMDD - hh_mm_ss AMPM") & ".csv"), FileFormat:=xlCSV, CreateBackup:=False为了清楚起见,我重写了您的一些代码,但是如果您想保持与已有的结构相同的结构,只需替换ActiveWorkbook.SaveAs ("C:\Users\SB\Documents\CSV Uploads\UPLOAD - IB " & Format(Now(), "YYYYMMDD - hh_mm_ss AMPM") & ".csv"), FileFormat:=xlCSV, CreateBackup:=False

with

ActiveWorkbook.SaveAs (ActiveWorkbook.Sheets("UPLOAD").Range("C13").Value & Format(Now(), "YYYYMMDD - hh_mm_ss AMPM") & ".csv"), FileFormat:=xlCSV, CreateBackup:=False

A few other notes:其他一些注意事项:

  • Using ThisWorkbook rather than ActiveWorkbook is safer because it will always refer to the workbook that the VBA code is residing in rather than whichever workbook happens to be active at the time.使用ThisWorkbook而不是ActiveWorkbook更安全,因为它将始终引用 VBA 代码所在的工作簿,而不是当时恰好处于活动状态的任何工作簿。
  • Be careful with the Workbook.Close method, especially since there is no confirmation to close.小心使用Workbook.Close方法,尤其是在没有确认关闭的情况下。 You could easily lose your work, and since CSV files don't save VBA code, it would be even worse.您很容易丢失您的工作,并且由于 CSV 文件不保存 VBA 代码,情况会更糟。
Private Sub EXPORTCSV_MOD()

    ' Parameters of the file path
    Dim Path As String, Filename As String, Extension As String
    
    Path = ThisWorkbook.Sheets("UPLOAD").Range("C13").Value
    Filename = Format(Now(), "YYYYMMDD - hh_mm_ss AMPM")
    Extension = ".csv"
    
    ' Assemble the full file path
    Dim FullPath As String
    FullPath = Path & Application.PathSeparator & Filename & Extension

    ' Save and close the workbook
    ThisWorkbook.SaveAs Filename:=FullPath, FileFormat:=xlCSV, CreateBackup:=False
 
    ThisWorkbook.Close

End Sub

Thx @TehDrunkSailor, with a slight tweak using your logic, this resolved my code, you legend,.谢谢@TehDrunkSailor,使用您的逻辑稍作调整,这解决了我的代码,您的传奇。 The reason I am using Active and not This is because I am saving the copied sheet into a new workbook, not the workbook I have been working in.我使用 Active 而不是这是因为我将复制的工作表保存到新工作簿中,而不是我一直在使用的工作簿。

Sub EXPORTCSV()

Dim Path As String
Dim filename As String
'The UPLOAD sheet was very hidden
    Sheets("UPLOAD").Visible = True
'Copy to a new workbook
    Sheets("UPLOAD").Copy
'Save the new workbook using data stored in the original workbook
    ActiveWorkbook.SaveAs (ThisWorkbook.Sheets("LOOKUP DATA").Range("C13").Value & "UPLOAD - IB  " _
        & Format(Now(), "YYYYMMDD - hh_mm_ss AMPM") & ".csv"), FileFormat:=xlCSV, CreateBackup:=False

'Close the new workbook
    ActiveWorkbook.Close

End Sub

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

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