Excel宏循环表选择问题

时间:2017-06-27 18:53:09

标签: excel vba excel-vba

我是编码的新手,并且一直试图让这个循环工作一段时间而没有太大的成功。我需要这个宏来循环遍历每个工作表,不包括前两个工作表(主和计算),并将数据移动到指定的主表或计算表。感谢您提前给予的任何帮助。

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

1 个答案:

答案 0 :(得分: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倍,并使自己和论坛读者更容易帮助你。

您无需选择工作表并进行复制和粘贴,您可以直接告诉工作表xyz其范围A1值是另一个工作表范围A1值。

编辑:您也可以告诉范围其公式与其他公式相同,如果这是您需要的。例如Worksheets("Sheet1").Range("B1").Formula = Worksheets("Sheet2").Range("B1").Formula

无论哪种方式,如果你想要摆脱Select,你的代码可以简化很多,这会让每个人的工作变得更轻松!