2017-06-27 74 views
0

我是新來的編碼,並試圖讓這個循環工作一段時間沒有太多的成功。我需要這個宏循環遍歷除前兩張紙(主紙和計算紙)之外的每張紙,並將數據移動到指定的主紙張或計算紙張。感謝您提前給予的任何幫助。Excel宏循環表選擇問題

Dim i As Integer 

Dim WS_Count As Integer 

While i > 2 
    Sheets("Master").Select 
    Range("A2:R2").Select 
    Selection.Insert shift:=xlDown, copyOrigin:=xlFormatFromLeftOrAbove 
    Sheets("Calculations").Select 
    Range("A:G").Select 
    Selection.Insert shift:=xlRight, copyOrigin:=xlFormatFromLeftOrAbove 
'Calculational sheet formating 
    sheet (i).Select 
    Range("B7").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Calculations").Select 
    Range("A1").Select 
    ActiveSheet.Paste 
    Sheets(i).Select 
    Range("C1").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Calculations").Select 
    Range("B1").Select 
    ActiveSheet.Paste 
' Subject & Number: Data Input to Master Sheet 
' Locations: Data File: B7 Master: A2 
    Sheets(i).Select 
    Range("B7").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Master").Select 
    Range("A2").Select 
    ActiveSheet.Paste 
' Date: Data Input to Master Sheet 
' Locations: Data File: C5 Master: B2 
    Sheets(i).Select 
    Range("C5").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Master").Select 
    Range("B2").Select 
    ActiveSheet.Paste 
' Experiment Name 
' Locations: Data File: B8 Master: C2 
    Sheets(i).Select 
    Range("B8").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Master").Select 
    Range("C2").Select 
    ActiveSheet.Paste 
' Group Number 
' Locations: Data File: B9 Master: D2 
    Sheets(i).Select 
    Range("B9").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Master").Select 
    Range("D2").Select 
    ActiveSheet.Paste 
' Box Number: 2 or 4 
' Locations: Data File: B10 Master: E2 
    Sheets(i).Select 
    Range("B10").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Master").Select 
    Range("E2").Select 
    ActiveSheet.Paste 
' Total Time in box (S): Over all experiment time 
' Locations: Data File: B27 Master: F2 
    Sheets(i).Select 
    Range("B27").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Master").Select 
    Range("F2").Select 
    ActiveSheet.Paste 
' Time In Box (E): Sum(all E)- Values are summed in the calculations sheet 
(A1:A20,F1:F20) 
' Locations: Data File: C32:G32, Calculations: A2:G2 Master: G2 
    Sheets(i).Select 
    Range("C32:G32", Range("C32:G32").End(xlDown)).Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Calculations").Select 
    Range(A2).Select 
    ActiveSheet.Paste 
    Range("F10") = WorksheetFunction.Sum(Range("A2:E2", 
    Range("A2:E2").End(xlDown))) 
    Range("F10").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Calculations").Select 
    Range("F11").Select 
    ActiveSheet.Paste 
    Range("F11").Select 
    Application.CutCopyMode = False 
    Selection.Cut 
    Sheets("Master").Select 
    Range("G2").Select 
    ActiveSheet.Paste 
' Entries (J): count (# tunnel time stamps) 
' locations: Data File: B21 Master: H2 
    Sheets(i).Select 
    Range("B21").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Master").Select 
    Range("H2").Select 
    ActiveSheet.Paste 
' Average Time In Box: Average (all E) 
' Locations: Data File: C32:G32, Calculations: A22 Master: I2 
    Sheets(i).Select 
    Range("C32:G32", Range("C32:G32").End(xlDown)).Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Calculations").Select 
    Range("A22").Select 
    ActiveSheet.Paste 
    Range("F30") = WorksheetFunction.Average(Range("A2:E2", 
    Range("A2:E2").End(xlDown))) 
    Range("F30").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Calculations").Select 
    Range("F31").Select 
    ActiveSheet.Paste 
    Range("F31").Select 
    Application.CutCopyMode = False 
    Selection.Cut 
    Sheets("Master").Select 
    Range("I2").Select 
    ActiveSheet.Paste 
' Trail Number: (A) Data Input to Master sheet 
' Locations: Data File: B14 Master: J2 
Sheets(i).Select 
Range("B14").Select 
Application.CutCopyMode = False 
Selection.Copy 
Sheets("Master").Select 
Range("J2").Select 
ActiveSheet.Paste 
' Lever Presses: (B) Data Input to Master sheet 
' Locations: Data File: B15 Master: K2 
Sheets(i).Select 
Range("B15").Select 
Application.CutCopyMode = False 
Selection.Copy 
Sheets("Master").Select 
Range("K2").Select 
ActiveSheet.Paste 
' Food cup checks (K): 
' Locations: Data File: B22 Master: L2 
Sheets(i).Select 
Range("B22").Select 
Application.CutCopyMode = False 
Selection.Copy 
Sheets("Master").Select 
Range("L2").Select 
ActiveSheet.Paste 
' Pellet reward numbers's (P/O) 
' Locations: Data File: B23 Master: M2 
Sheets(i).Select 
Range("B23").Select 
Application.CutCopyMode = False 
Selection.Copy 
Sheets("Master").Select 
Range("M2").Select 
ActiveSheet.Paste 
' Food Retrieval latency: Food cup stamp - lever time stamp/total trials * 
100 
' Locations: Data File:((C35:G35)-(C83:G83))/B14*100, Calculations A42, A62, 
F41, F42, F43, F44, F45 Master: N2 
Sheets(i).Select 
Range("C35:G35", Range("C35:G35").End(xlDown)).Select 
Application.CutCopyMode = False 
Selection.Copy 
Sheets("Calculations").Select 
Range("A42").Select 
ActiveSheet.Paste 
    Sheets(i).Select 
    Range("C83:G83", Range("C83:G83").End(xlDown)).Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Calculations").Select 
    Range("A62").Select 
    ActiveSheet.Paste 
    Range("F41") = WorksheetFunction.Sum(Range("A42:G42", 
    `Range("A42:G42").End(xlDown))) 
    Range("F42") = WorksheetFunction.Sum(Range("A62:G62", 
    Range("A62:A62").End(xlDown))) 
     Range("F43").Value = Range("F41") - Range("F42") 
      Sheets(i).Select 
      Range("B14").Select 
      Application.CutCopyMode = False 
      Selection.Copy 
      Sheets("Calculations").Select 
      Range("F44").Select 
      ActiveSheet.Paste 
      Range("F45").Value = (Range("F43")/Range("F44")) * 100 
       Range("F45").Select 
       Application.CutCopyMode = False 
       Selection.Copy 
       Sheets("Calculations").Select 
       Range("F46").Select 
       ActiveSheet.Paste 
       Range("F46").Select 
       Application.CutCopyMode = False 
       Selection.Cut 
       Sheets("Master").Select 
       Range("N2").Select 
       ActiveSheet.Paste 
    ' File Name 
    ' Location: Data File:C1 Master: O2 
    Sheets(i).Select 
Range("C1").Select 
Application.CutCopyMode = False 
Selection.Copy 
Sheets("Master").Select 
Range("O2").Select 
ActiveSheet.Paste 
Wend 

'Exit Handler updates the view screen so that your on the last sheet of data 
added to the file and exits the subroutine 
ExitHandler: 
Application.ScreenUpdating = True 
Set wkbAll = Nothing 
Set wkbTemp = Nothing 
Exit Sub 

ErrHandler: 
MsgBox Err.Description 
Resume ExitHandler 

End Sub 

回答

0

這是一個龐大的代碼塊。爲了清晰起見,我在這裏回答這個問題,而不是評論,即使它不是對問題的「回答」......它可能會顯示如何壓縮代碼並減少很多錯誤潛力。

相反的:

Sheets(i).Select 
Range("B14").Select 
Application.CutCopyMode = False 
Selection.Copy 
Sheets("Calculations").Select 
Range("F44").Select 
ActiveSheet.Paste 

你可以簡單地說:

Sheets("Calculations").Range("F44").Value = Sheets(i).Range("B14").Value 

如果你這樣做你的代碼塊,它會通過3-4倍縮小尺寸,並使它成爲不少更容易讓你自己和論壇的讀者來幫助你。

您不需要選擇工作表並複製和粘貼,您可以直接告訴sheet xyz其範圍A1值是另一個表格範圍A1的值。

編輯:你也可以告訴一個範圍,它的公式與另一個範圍相同,如果這是你所需要的。例如Worksheets("Sheet1").Range("B1").Formula = Worksheets("Sheet2").Range("B1").Formula

無論哪種方式,你的代碼可以簡化很多,如果你期待擺脫Select ing,它使每個人的工作更容易!

+1

將單元格的「值」設置爲另一個單元格的「值」與將一個單元格複製到另一個單元格不同 - 默認情況下,複製/粘貼將複製公式(和格式)而不是值。我懷疑OP **是**只是試圖複製值,但你應該添加一個警告來說明這個「答案」,以防萬一他們不這樣做。 (他們的代碼仍然可以通過使用'Sheets(i).Range(「B14」),複製表格(「Calculations」)。Range(「F44」)'等)來簡化 – YowE3K

+0

這是一個很好的觀察,當然您可以按照相同的方式將'Value'分配給'Formula',具體取決於需求。我會更新我的非答案來反映。 – jamheadart