0
有一個就緒腳本,用於對選定文件夾中工作簿中的行數進行計數。如果任何工作簿中的行數大於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
是否有可能move
一個Worbook到另一個文件夾insted的情況下,它包含超過1行應對它。
是否有可能使用類似於:Workbooks.Close savechanges:=False
以便在行計數後關閉所選工作簿?謝謝!
1)因爲我看到你在計算行數後關閉每個工作簿:'.Close'。 2)你可以使用'SaveAs'(你已經擁有它)保存在另一個文件夾中,而不是'Kill「oldPath」'從原來的文件夾中刪除文件 –
實際上所有的工作簿保持打開狀態,如果我只有3個工作簿,但10或20它變得有問題。我也應該關閉一個源文件夾,我從那裏拿行工作簿的行數 – Ale
嘗試使用'.Close SaveChanges:= False' –