在你的帮助下,我设法创造了一些我想做的事情。非常感谢!!!
Private Sub DoStuff()
Application.DisplayAlerts = False
'Create New Workbook
Dim Count As Integer
CurrentFile = "June_Files_macros_new.xlsm"
NewFile = "Train" & CStr(Cells(2, 13)) & "_" & CStr(Cells(2, 3)) & ".xls"
Workbooks.Add
'Save New Workbook
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & NewFile
'Select top row of data and insert into spreadsheed!!!!!
Workbooks(CurrentFile).Worksheets("Sheet1").Rows(2).Copy
Workbooks(NewFile).Worksheets("Sheet1").Rows(2).PasteSpecial xlPasteValues
Count = 3
For i = 3 To 12802
'if Date and Train Number are equal, Then copy and paste the i th row
'else, save new file, create another new file, save
If Cells(i, 3) = Cells(i - 1, 3) And Cells(i, 13) = Cells(i - 1, 13) Then
Workbooks(CurrentFile).Worksheets("Sheet1").Rows(i).Copy
Workbooks(NewFile).Worksheets("Sheet1").Rows(Count).PasteSpecial xlPasteValues
Count = Count + 1
Else: Workbooks(CurrentFile).Worksheets("Sheet1").Rows(1).Copy
Workbooks(NewFile).Worksheets("Sheet1").Rows(1).PasteSpecial xlPasteValues
Workbooks(NewFile).SaveAs ThisWorkbook.Path & "\" & "Train" & CStr(Cells(i - 1, 13)) & "_" & CStr(Cells(i - 1, 3)) & ".xls"
Workbooks(NewFile).Close
Workbooks.Add
NewFile = "Train" & CStr(Cells(i, 13)) & "_" & CStr(Cells(i, 3)) & ".xls"
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & NewFile
Workbooks(CurrentFile).Worksheets("Sheet1").Rows(i).Copy
Workbooks(NewFile).Worksheets("Sheet1").Rows(2).PasteSpecial xlPasteValues
Count = 3
End If
Next i
Workbooks(CurrentFile).Worksheets("Sheet1").Rows(1).Copy
Workbooks(NewFile).Worksheets("Sheet1").Rows(1).PasteSpecial xlPasteValues
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & NewFile
Workbooks(NewFile).Close