还是上传一个附件另外发贴提问吧.我在一楼说的很清楚.本贴是学习.一是不要贴海量代码.二是不要发解决问题的贴.如果有问题有单独发贴提问
兰老师: 你好!本人由于工作的需要自学了VBA,就是没有明师指导,代码写得太长,功能却是非常简单!你可以帮我优化一下吗?代码是功能是自动将“6wk Plan-wk23 version 0.exe”表里面的生产计划更新到“生产计划跟踪表.exe”表中。谢谢!!! Sub Update() file1 = "生产计划跟踪表.xls" file1sheet1 = "Build Plan Summary" file1sheet2 = "Actually Build Qty" file2 = Cells(2, 3).Value + ".xls" file2sheet1 = Cells(3, 3).Value startdivide1 = "ISO NO." enddivide1 = "Subtotal" startdivide2 = Cells(4, 3).Value ActiveSheet.Unprotect Password:="hdd" Dim fristdayofweek As Date Wek1 = Weekday(Date, vbMonday) fristdayofweek = Date - Wek1 - 1 Week = Date Week = Format(Week, "ww") Cells(2, 7).Value = "WK" & Week Cells(4, 7).Value = fristdayofweek Application.DisplayAlerts = False Application.ScreenUpdating = False startrow1 = 4 Do Until Cells(startrow1, 3).Value = startdivide1 startrow1 = startrow1 + 1 If startrow1 > 1000 Then MsgBox "找不到file1的行起始点:" & startdivide1 & "!请找原因!" Stop Else End If Loop endrow1 = startrow1 + 1 Do Until Cells(endrow1, 2).Value = enddivide1 endrow1 = endrow1 + 1 If endrow1 > 1000 Then MsgBox "找不到file1的行终止点:" & enddivide1 & "!请找原因!" Stop Else End If Loop startcol1 = 6 Do Until Cells(4, startcol1).Value = fristdayofweek startcol1 = startcol1 + 1 If startcol1 > 1000 Then MsgBox "找不到file1的列起始点:" & fristdayofweek & "!请找原因!" Stop Else End If Loop ThisWorksheetName = ActiveSheet.Name Worksheets(ThisWorksheetName).Range(Cells(startrow1 + 1, startcol1), Cells(endrow1 - 1, startcol1)).ClearContents Worksheets(ThisWorksheetName).Range(Cells(startrow1 + 1, startcol1 + 2), Cells(endrow1 - 1, startcol1 + 2)).ClearContents Worksheets(ThisWorksheetName).Range(Cells(startrow1 + 1, startcol1 + 4), Cells(endrow1 - 1, startcol1 + 4)).ClearContents Worksheets(ThisWorksheetName).Range(Cells(startrow1 + 1, startcol1 + 6), Cells(endrow1 - 1, startcol1 + 6)).ClearContents Worksheets(ThisWorksheetName).Range(Cells(startrow1 + 1, startcol1 + 8), Cells(endrow1 - 1, startcol1 + 8)).ClearContents Worksheets(ThisWorksheetName).Range(Cells(startrow1 + 1, startcol1 + 10), Cells(endrow1 - 1, startcol1 + 10)).ClearContents Worksheets(ThisWorksheetName).Range(Cells(startrow1 + 1, startcol1 + 12), Cells(endrow1 - 1, startcol1 + 12)).ClearContents On Error Resume Next fName = Workbooks(file2).Name If Err.Number = 9 Then Else Windows(file2).Activate ActiveWorkbook.Save ActiveWorkbook.Close End If Application.Workbooks.Open ThisWorkbook.Path & "\" & file2 Sheets(file2sheet1).Select startrow2 = 25 Do Until Cells(startrow2, 1).Value = startdivide2 startrow2 = startrow2 + 1 If startrow2 > 1000 Then MsgBox "找不到file2的行起始点:" & startdivide2 & "!请找原因!" Stop Else End If Loop startcol2 = 6 Do Until Cells(8, startcol2).Value = fristdayofweek startcol2 = startcol2 + 1 If startcol2 > 1000 Then MsgBox "找不到file2的列起始点:" & fristdayofweek & "!请找原因!" Stop Else End If Loop For i = 1 To endrow1 - startrow1 - 1 Windows(file2).Activate checksn = Cells(startrow2 + i, 3).Value Windows(file1).Activate If Cells(startrow1 + i, 3).Value = checksn Then For j = 1 To 7 Windows(file2).Activate copydata = Cells(startrow2 + i, startcol2 + (j - 1)).Value Windows(file1).Activate Cells(startrow1 + i, startcol1 + (j - 1) * 2).Value = copydata Next j Else For i1 = 1 To endrow1 - startrow1 - 1 Windows(file2).Activate If Cells(startrow1 + i1, 3).Value = checksn Then For j = 1 To 7 Windows(file2).Activate copydata = Cells(startrow2 + i1, startcol2 + (j - 1)).Value Windows(file1).Activate Cells(startrow1 + i, startcol1 + (j - 1) * 2).Value = copydata Next j Exit For Else End If Next i1 End If Next i For j = 1 To 7 Windows(file2).Activate checksumdata = Cells(startrow2 + (endrow1 - startrow1), startcol2 + (j - 1)).Value Windows(file1).Activate If Cells(endrow1, startcol1 + (j - 1) * 2) = checksumdata Then Else MsgBox "日期:" & Cells(4, startcol1 + (j - 1) * 2).Value & "报表总数与计划报表的总数不一致!请查明原因!" End If Next j Application.DisplayAlerts = True Application.ScreenUpdating = True Windows(file2).Activate ActiveWorkbook.Save ActiveWorkbook.Close Windows(file1).Activate ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="hdd" Sheets(file1sheet2).Select ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="hdd" Sheets(file1sheet1).Select ActiveWorkbook.Save End Sub
[此贴子已经被兰色幻想于2006-6-11 12:12:19编辑过] |