我是新的VBA所以大家多多包涵。我試圖將工作表從指定的文件位置複製到主整合工作簿中。我想阻止將複製的工作表複製到合併工作簿中。例如,如果表1已經被複制到主賬戶工作簿,我不想重新複製它,當命令已經跑了。以下是我到目前爲止的代碼。工作表複製到另一個工作簿,而不復制工作表
Private Sub CommandButton1_Click()
Dim directory As String
Dim fileName As String
Dim sheet As Worksheet
Dim total As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
directory = "c:\test\"
' edit directory
fileName = Dir(directory & "*.xl??")
Do While fileName <> ""
Workbooks.Open (directory & fileName)
For Each sheet In Workbooks(fileName).Worksheets
total = Workbooks("test import.xlsm").Worksheets.Count
Workbooks(fileName).Worksheets(sheet.Name).Copy _
after:=Workbooks("test import.xlsm").Worksheets(total)
Next sheet
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
感謝您的評論。餘取代的工作表(sheet.Name)用工作表(指標),然而,如果我運行命令一次以上,它將重新拷貝的片材,並添加一個(2)或(3)片材名稱之後。有沒有一種方法,以防止它在所有複製表,如果它已經被複制? –