เขียน VBA ใน 2003(ปกติรันได้) แต่ไปใช้ใน 2007-2010 กลับติด D
Posted: Fri Aug 09, 2013 10:45 am
รบกวนผู้รู้หน่อยครับ พอดีว่าผมเขียน Macro มาให้ทำงานก้อปตัวเลขไปมาแต่ทำงานเป็นลูป ตอนแรกเขียนใน 2003 ทำงานได้ปกติ แต่พอนำไฟล์ไปเิปิดใน 2007 หรือ 2010 แ้ล้วการวนลูปมันจะขึ้น debug ตลอด ถ้าให้ทำงานต้องกดปุ่ม continue ทุกครั้ง
หมายเหตุ ผมปรับ Trust center๖2007/2010) -> Macro Setting -> Enable แล้ว
ตัวอย่างโค้ดดังนี้ครับ
Sub optimize()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets(3).Range("B2:M65536").ClearContents
Sheets(4).Range("B2:M65536").ClearContents
Sheets(5).Range("B2:M65536").ClearContents
Sheets(3).Select
Dim frI, toI, frJ, toJ, frK, toK As Integer
frI = Range("R6").Value
toI = Range("R7").Value
frJ = Range("S6").Value
toJ = Range("S7").Value
frK = Range("T6").Value
toK = Range("T7").Value
Sheets(2).Select
Z = 2
For i = frI To toI
For j = frJ To toJ
For k = frK To toK
Sheets(2).Select
Range("e3").Value = i
Range("f3").Value = j
Range("g3").Value = k
Calculate
Sheets(3).Select
Cells(Z, 2).Value = i
Cells(Z, 3).Value = j
Cells(Z, 4).Value = k
Z = Z + 1
Debug.Print "OPT1:" & i & " OPT2: " & j & " OPT3: " & k
Next k
Next j
Next i
MsgBox "Done", vbInformation
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
พอเวลารันมันจะมี กล่อง ให้เลือก Continue / End / Debug / Help ถ้าจะให้รันต่อต้องกด Continue แล้วถ้ากด Debug จะขึ้นแถบสีเหลืองที่โค้ด
Calculate
Sheets(3).Select -> แถบเหลืองจะมาขึ้นที่บรรทัดนี้ตลอดครับ
หมายเหตุ ผมปรับ Trust center๖2007/2010) -> Macro Setting -> Enable แล้ว
ตัวอย่างโค้ดดังนี้ครับ
Sub optimize()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets(3).Range("B2:M65536").ClearContents
Sheets(4).Range("B2:M65536").ClearContents
Sheets(5).Range("B2:M65536").ClearContents
Sheets(3).Select
Dim frI, toI, frJ, toJ, frK, toK As Integer
frI = Range("R6").Value
toI = Range("R7").Value
frJ = Range("S6").Value
toJ = Range("S7").Value
frK = Range("T6").Value
toK = Range("T7").Value
Sheets(2).Select
Z = 2
For i = frI To toI
For j = frJ To toJ
For k = frK To toK
Sheets(2).Select
Range("e3").Value = i
Range("f3").Value = j
Range("g3").Value = k
Calculate
Sheets(3).Select
Cells(Z, 2).Value = i
Cells(Z, 3).Value = j
Cells(Z, 4).Value = k
Z = Z + 1
Debug.Print "OPT1:" & i & " OPT2: " & j & " OPT3: " & k
Next k
Next j
Next i
MsgBox "Done", vbInformation
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
พอเวลารันมันจะมี กล่อง ให้เลือก Continue / End / Debug / Help ถ้าจะให้รันต่อต้องกด Continue แล้วถ้ากด Debug จะขึ้นแถบสีเหลืองที่โค้ด
Calculate
Sheets(3).Select -> แถบเหลืองจะมาขึ้นที่บรรทัดนี้ตลอดครับ