Board logo

標題: 可以幫我寫迴圈並修正錯誤嗎 [打印本頁]

作者: opman    時間: 2016-1-18 12:22     標題: 可以幫我寫迴圈並修正錯誤嗎

本帖最後由 GBKEE 於 2016-1-19 10:17 編輯

Range("K2") 1-140筆資料(可以浮動嗎)
Range("K1") 1--7筆資料(可以浮動嗎)
Sub 完成跑社()
'
' 完成跑社 巨集
''
  Range("K2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "1"
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "7"
        執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "6"
        執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "5"
        執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "4"
執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "3"
執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "2"
執行SUB 志願確定
Range("K1").Select
      ActiveCell.FormulaR1C1 = "1"
      執行SUB 志願確定
Range("K2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "2"
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "7"
        執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "6"
        執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "5"
        執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "4"
執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "3"
執行SUB 志願確定
      Range("K1").Select
      ActiveCell.FormulaR1C1 = "2"
執行SUB 志願確定
Range("K1").Select
      ActiveCell.FormulaR1C1 = "1"
      執行SUB 志願確定

End Sub
作者: opman    時間: 2016-1-18 13:16

回復 1# opman

我自己摸索寫成這樣但持行好久,有辦法縮短時間嗎
    Sub 完成跑社()
'
' 完成跑社 巨集
'
Dim s As Integer
For s = 1 To 128
    Range("K2") = s
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "7"
        Columns("F:F").Select
    Selection.Copy
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="N", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.CutCopyMode = False
    Selection.Copy
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "6"
        Columns("F:F").Select
    Selection.Copy
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="N", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.CutCopyMode = False
    Selection.Copy
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "5"
        Columns("F:F").Select
    Selection.Copy
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="N", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.CutCopyMode = False
    Selection.Copy
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "4"
        Columns("F:F").Select
    Selection.Copy
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="N", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.CutCopyMode = False
    Selection.Copy
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "3"
        Columns("F:F").Select
    Selection.Copy
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="N", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.CutCopyMode = False
    Selection.Copy
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "2"
        Columns("F:F").Select
    Selection.Copy
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="N", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.CutCopyMode = False
    Selection.Copy
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "1"
            Columns("F:F").Select
    Selection.Copy
    Range("H1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="N", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.CutCopyMode = False
    Selection.Copy
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=True, Transpose:=False
  Next s
End Sub
作者: ML089    時間: 2016-1-18 16:12

回復 1# opman

照原PO文是可以縮減如下

Sub EX()
    For Each xK2 In Array("1", "2")
        Range("K2") = xK2
        For Each xK1 In Array("7", "6", "5", "4", "3", "2", "1")
            Range("K1") = xK1
            執行SUB 志願確定
        Next
    Next
End Sub
作者: GBKEE    時間: 2016-1-19 10:20

回復 2# opman


試試看
  1. Option Explicit
  2. Sub 完成跑社()
  3. '
  4. ' 完成跑社 巨集
  5. '
  6. Dim s As Integer, i As Integer
  7. Application.ScreenUpdating = False
  8. For s = 1 To 128
  9.     Range("K2") = s
  10.     For i = 7 To 1 Step -1
  11.         Range("K1") = i
  12.         Columns("F:F").Copy
  13.         With Range("H1").EntireColumn
  14.             .Cells(1).PasteSpecial Paste:=xlPasteValues
  15.             .Replace What:="N", Replacement:="", LookAt:=xlPart
  16.             .Copy
  17.         End With
  18.         Range("C1").PasteSpecial Paste:=xlPasteValues
  19.     Next
  20. Next
  21. Application.CutCopyMode = False
  22. Application.ScreenUpdating = True
  23. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)