gpt4 book ai didi

excel - 从单元格引用中获取文件名和路径

转载 作者:行者123 更新时间:2023-12-04 20:27:24 25 4
gpt4 key购买 nike

我有以下代码,它将工作表导出为 .CSV 文件。它将使用当前文件名和当前位置创建具有 .CSV 格式的新文件。

想要以某种方式对其进行修改,它应该从单元格 N15 中选择要保存的文件名,并且它应该保存在预定义的路径中。

Sub ExportAsCSV()

Dim MyFileName As String
Dim CurrentWB As Workbook, TempWB As Workbook

Set CurrentWB = ActiveWorkbook
ActiveWorkbook.ActiveSheet.UsedRange.Copy

Set TempWB = Application.Workbooks.Add(1)
With TempWB.Sheets(1).Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
End With

Dim Change below to "- 4" to become compatible with .xls files
MyFileName = CurrentWB.Path & "\" & Left(CurrentWB.Name,
Len(CurrentWB.Name) - 5) & ".csv"

Application.DisplayAlerts = False
TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV,
CreateBackup:=False, Local:=True
TempWB.Close SaveChanges:=False
Application.DisplayAlerts = True
End Sub

最佳答案

只需更改文件名变量中的值

Sub ExportAsCSV()

Dim MyFileName As String
Dim CurrentWB As Workbook, TempWB As Workbook

Set CurrentWB = ActiveWorkbook
ActiveWorkbook.ActiveSheet.UsedRange.Copy

Set TempWB = Application.Workbooks.Add(1)
With TempWB.Sheets(1).Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
End With

Dim Change below to "- 4" to become compatible with .xls files
MyFileName = CurrentWB.Path & "\" & Range("N15").value2

Application.DisplayAlerts = False
TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV,
CreateBackup:=False, Local:=True
TempWB.Close SaveChanges:=False
Application.DisplayAlerts = True
End Sub

关于excel - 从单元格引用中获取文件名和路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57557313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com