gpt4 book ai didi

excel - 使用单元格引用导出 XLS 文件

转载 作者:行者123 更新时间:2023-12-04 22:18:11 25 4
gpt4 key购买 nike

我有以下代码旨在将工作表复制到新位置。

Sub XLSSave()

Sheets("Group Import").Copy
Cells.Copy
Cells.PasteSpecial xlPasteValues
ActiveWorkbook.SaveAs Filename:=Sheets("Group Import").Range("B22")
ActiveWorkbook.Close False

End Sub
单元格 K67 是沿线的文件路径
"C\Folder1\Folder2\Folder3\YYYY\MM\DD"
“C:\Folder1\Folder2\Folder3\YYYY\MM\DD”。
路径“C:”设置正确,我在问题上打错了。
我原本打算只连接单元格 B22 中的地址,因为它需要是动态的。
它正在将 Excel 文件导出为乱码。

最佳答案

导出工作表
轻松

Option Explicit

Sub XLSSaveEasy()

Application.ScreenUpdating = False

Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("Group Import")
sws.Copy

Dim dws As Worksheet: Set dws = ActiveWorkbook.Worksheets(1)
dws.UsedRange.Value = dws.UsedRange.Value

Application.DisplayAlerts = False
dws.Parent.SaveAs sws.Range("B22").Value, xlOpenXMLWorkbook
Application.DisplayAlerts = True

dws.Parent.Close False

Application.ScreenUpdating = True

End Sub
没那么容易
Sub XLSSave()

Const swsName As String = "Group Import"
Const swsFilePathCell As String = "B22"

Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets(swsName)

Dim FilePath As String: FilePath = sws.Range(swsFilePathCell).Value
Dim FolderPath As String
FolderPath = Left(FilePath, InStrRev(FilePath, "\") - 1)

If Dir(FolderPath, vbDirectory) <> "" Then

Application.ScreenUpdating = False

sws.Copy

Dim dws As Worksheet: Set dws = ActiveWorkbook.Worksheets(1)
dws.UsedRange.Value = dws.UsedRange.Value

Application.DisplayAlerts = False
dws.Parent.SaveAs FilePath, xlOpenXMLWorkbook
Application.DisplayAlerts = True

dws.Parent.Close False

Application.ScreenUpdating = True

MsgBox "Backup of worksheet '" & swsName & "' created as '" _
& FilePath & "'.", vbInformation, "Success"

Else

MsgBox "The Folder '" & FolderPath & "' does not exist.", _
vbCritical, "Fail"

End If

End Sub

关于excel - 使用单元格引用导出 XLS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66732483/

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