【发布时间】:2018-10-06 01:41:48
【问题描述】:
我正在尝试创建一个循环遍历文件夹中所有非 txt 文件的宏,打开它们,从打开的文件中复制选择,然后将其粘贴到带有宏的文件中的特定工作表(取决于正在复制哪个文件)。我已经让前两个部分工作,但我无法让复制部分工作。它不断复制应该是粘贴文件的文件。知道我做错了什么吗?
Private Sub CommandButton1_Click()
Dim Path As String
Dim File As String
Dim PasteFile As String
Dim Month As String
Dim FY As String
Month = "feb"
FY = "18"
PasteFile = ThisWorkbook.Name
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Path = "[file path]"
file = Dir(Path)
Do While file <> "*.txt" Or file <> ""
If file = "[file name]" & Month & FY & ".xlsx" Then
Workbooks.Open Path & file
Workbooks(file).Worksheets("Sheet1").Activate
Range(Cells(1, 1), Cells(5, 5)).Copy
Workbooks(myFile).Worksheets("Sheet1").Activate
Cells(10, 3).PasteSpecial xlPasteValues
End If
File = Dir()
Loop
End Sub
【问题讨论】: