简体   繁体   English

我可以使用VBA仅导出工作表中的范围吗?

[英]Can I export only a range within a sheet using VBA?

I have only just started learning VBA and have built a small application that exports a particular sheet. 我刚开始学习VBA并构建了一个导出特定工作表的小应用程序。 I want to go one step further and only export a range from that sheet. 我想更进一步,只从该表中导出一个范围。

I've tried various different coding scenarios but to no avail. 我尝试了各种不同的编码方案,但无济于事。

Sub saveSheetToCSV()

    Dim myCSVFileName As String
    Dim tempWB As Workbook

    myCSVFileName = ThisWorkbook.Path & "\" & "EXPORT_" & VBA.Format(VBA.Now, "ddMMyyyy") & ".csv"

    ThisWorkbook.Sheets("Data").Activate

    ActiveSheet.Copy
    Set tempWB = ActiveWorkbook

    With tempWB
    .SaveAs FileName:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
    .Close
    End With
End Sub

Can anyone please confirm what code I would use to just export a range within a sheet? 任何人都可以确认我会用什么代码来导出工作表中的范围? I'm trying to export just the range A2:D24 in the sheet called "Data", rather than the whole sheet as what the current code above does. 我试图在名为“数据”的工作表中导出范围A2:D24,而不是像上面的当前代码那样导出整个工作表。 Thanks. 谢谢。

Copy Range to new Sheet in new Workbook. 将范围复制到新工作簿中的新工作表。 Save As. 另存为。 Close. 关。

    Dim myCSVFileName As String
    Dim tempWB As Workbook

    myCSVFileName = ThisWorkbook.Path & "\" & "EXPORT_" & VBA.Format(VBA.Now, "ddMMyyyy") & ".csv"
    'Create new Workbook.
    Set tempWB = Workbooks.Add
    'Copy Range
    ThisWrokbook.Sheets("SheetNameWithRange").Range("A1:Z40").Copy    
    With tempWB
        'Paste Range
        .Worksheets(1).Range("A1").Paste 'Or Paste as Values to not retain Links/Formulas
        'Save and Close
        .SaveAs FileName:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
        .Close
    End With

暂无
暂无

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

相关问题 使用 VBA 将 Excel 工作表的(部分)范围导出为 CSV - Export Range of (Part of) an Excel Sheet to CSV with VBA 如何使用Excel VBA将一张纸中的值分配给隐藏的纸中? (并跳过范围内的列?) - How to assign values from one sheet into hidden sheet using Excel VBA? (and skip a column within the range?) 将工作表导出到XLS而不复制VBA-使用复制已有代码,需要帮助按范围复制 - Export sheet to XLS without copying VBA - have existing code using copy, need help copying by range VBA 脚本用于格式化列范围内的单元格仅格式化工作簿中的第一个工作表 - VBA script to format cells within a column range only formats the first sheet in the workbook 使用VBA,如何在定义的范围内搜索多个字符串? - Using VBA, how can I search for multiple strings within a defined range? 使用VBA将导入的XML工作表导出到excel - Export the imported XML sheet into excel using vba excel vba-使用自动过滤器-无法将过滤后的范围传递给子项,它会不断传递整个图纸范围 - excel vba - Using autofilter - can't pass the filtered range to a sub, it keeps passing the entire sheet range Excel VBA使用范围选择整个工作表 - Excel VBA select entire sheet using Range 在VBA中使用sheet()。Range()。Value进行列引用 - Column referencing in VBA using sheet().Range().Value 使用 VBA 间接引用其他工作表中的范围 - Indirect reference to range in other sheet using VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM