Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何根据条件将工作簿从一个文件夹移动到另一个文件夹?_Vba_Excel - Fatal编程技术网

Vba 如何根据条件将工作簿从一个文件夹移动到另一个文件夹?

Vba 如何根据条件将工作簿从一个文件夹移动到另一个文件夹?,vba,excel,Vba,Excel,有一个就绪脚本,用于计算选定文件夹中工作簿中的行数。如果任何工作簿中的行数超过1,则会将此工作簿复制并保存到另一个文件夹中 Sub OpenFiles() Dim MyFolder As String Dim MyFile As String Dim TargetWB As Workbook MyFolder = GetFolder("C:\Users\user\Desktop") MyFile = Dir(MyFolder & "*.

有一个就绪脚本,用于计算选定文件夹中工作簿中的行数。如果任何工作簿中的行数超过1,则会将此工作簿复制并保存到另一个文件夹中

    Sub OpenFiles()
    Dim MyFolder As String
    Dim MyFile As String
    Dim TargetWB As Workbook

    MyFolder = GetFolder("C:\Users\user\Desktop") 
    MyFile = Dir(MyFolder & "*.*")

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Do While MyFile <> ""
        Set TargetWB = Workbooks.Open(Filename:=MyFolder & "\" & MyFile & "*.*")
        With TargetWB
            If CountUsedRows(TargetWB) > 1 Then
                .SaveAs "C:\Users\user\Desktop\vba\" & MyFile
            End If
            .Close
        End With
    MyFile = Dir
    Loop

    'Workbooks.Close savechanges:=False

   Shell "explorer.exe C:\Users\user\Desktop\vba", vbMaximizedFocus 
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    End Sub


Function GetFolder(strPath As String) As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function

Function CountUsedRows(Wbk As Workbook) As Long
    Dim WS As Worksheet
    Set WS = Wbk.Sheets(1)
    CountUsedRows = WS.Range("A" & Rows.Count).End(xlUp).Row 
End Function
子OpenFiles()
将MyFolder设置为字符串
将MyFile设置为字符串
将TargetWB设置为工作簿
MyFolder=GetFolder(“C:\Users\user\Desktop”)
MyFile=Dir(MyFolder&“***”)
Application.ScreenUpdating=False
Application.DisplayAlerts=False
当我的文件“”时执行此操作
设置TargetWB=Workbooks.Open(文件名:=MyFolder&“\”&MyFile&“***”)
与TargetWB
如果CountUsedRows(TargetWeb)>1,则
.SaveAs“C:\Users\user\Desktop\vba\”&MyFile
如果结束
.结束
以
MyFile=Dir
环
'工作簿.Close保存更改:=False
Shell“explorer.exe C:\Users\user\Desktop\vba”,vbMaximizedFocus
Application.DisplayAlerts=True
Application.ScreenUpdating=True
端接头
函数GetFolder(strPath作为字符串)作为字符串
Dim fldr As FILE对话框
以字符串形式显示
设置fldr=Application.FileDialog(msoFileDialogFolderPicker)
与fldr
.Title=“选择一个文件夹”
.AllowMultiSelect=False
.InitialFileName=strPath
如果.Show-1,则转到下一个代码
sItem=.SelectedItems(1)
以
下一个代码:
GetFolder=sItem
设置fldr=无
端函数
函数CountUsedRows(Wbk作为工作簿)的长度
将WS设置为工作表
设置WS=Wbk.Sheets(1)
CountUsedRows=WS.Range(“A”&Rows.Count).End(xlUp).Row
端函数
如果工作簿包含多行,是否可以将其移动到另一个文件夹,而不是处理它


是否可以使用类似于:
workbook.Close savechanges:=False的方法在行计数后关闭所选的工作簿?谢谢

您可以使用
FileSystemObject
对象的
MoveFile
方法轻松移动文件。若要在早期绑定中使用此类型,请在VBA项目中添加对Microsoft Sripting Runtime的引用。

1)正如我看到的那样,您在计算行数后总是关闭每个工作簿:
。关闭
。2) 您可以使用
SaveAs
(您已经有了它)保存到另一个文件夹中,然后使用
Kill“oldPath”
从以前的文件夹中删除文件。实际上,所有工作簿都保持打开状态,如果我只有3本工作簿,这不是问题,但如果有10本或20本,则会出现问题。我还应该关闭一个源文件夹,从中获取行计数工作簿。请尝试使用
。关闭SaveChanges:=False
我已放置。关闭SaveChanges:=False代替。关闭,但文件仍保持打开状态,我从中选择文件的文件夹也保持打开状态