在vba中剪切并过去excel文件

时间:2016-06-15 12:22:47

标签: excel-vba vba excel

使用下面的代码,我可以创建excel的副本,但我想将特定文件从一个位置移动到另一个位置。请在下面的代码中告知所有更改的内容。

myFileNameDir = Sheet1.Range("V4").Value & TextBox38.Text & ".xlsx"
Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0
Set ws1 = Worksheets("sheet1")
ws1.Activate
ws1.SaveAs Sheet1.Range("V3").Value & TextBox3.Text & ".xlsx"

2 个答案:

答案 0 :(得分:0)

尝试:

ws1.SaveAs "C:\yourpath\" & Sheet1.Range("V3").Value & TextBox3.Text & ".xlsm"

答案 1 :(得分:0)

此代码将移动您的文件而无需打开它:

Sub test()

Dim mySourceFileName As String
Dim myTargetFileName As String

mySourceFileName = Sheet1.Range("V4").Value & TextBox38.Text & ".xlsx"
myTargetFileName = Sheet1.Range("V3").Value & TextBox3.Text & ".xlsx"

MsgBox "File Copied:" & CopyFile(mySourceFileName, myTargetFileName, True), vbOKOnly

End Sub

Function CopyFile(FromFile As String, ToFile As String, Overwrite As Boolean) As Boolean

    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next
    oFSO.CopyFile FromFile, ToFile, Overwrite
    CopyFile = (Err.Number = 0)
    Err.Clear

End Function

要移动文件,请使用:

Sub test()

Dim mySourceFileName As String
Dim myTargetFileName As String

mySourceFileName = Sheet1.Range("V4").Value & TextBox38.Text & ".xlsx"
myTargetFileName = Sheet1.Range("V3").Value & TextBox3.Text & ".xlsx"

MsgBox "File Moved:" & MoveFile(mySourceFileName, myTargetFileName), vbOKOnly

End Sub

Public Function MoveFile(FromFile As String, ToFile As String) As Boolean

    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next
    oFSO.MoveFile FromFile, ToFile
    MoveFile = (Err.Number = 0)
    Err.Clear

End Function