多个工作表中的VBA表单完成

时间:2014-01-31 17:02:39

标签: excel excel-vba excel-vba-mac vba

我在WKBK 1设计了一个标准模板,用于评分教师。WKBK 1有15种不同的工作表,或每位教师1本。在WKBK 2中,我有一张桌子用来保持他们不同功能的分数。我正在使用宏(或尝试)在WKBK 2中提取数据并将其转储到WKBK 1中。当我尝试在整个工作表中循环宏时,宏从老师1拉出并将他们的分数分配给教师2-15。

Sub Scorecard()

    Dim Current As Worksheet

    For Each Current In Worksheets

    Range("TeacherYTD") = "='[Teacher Formula.xlsx]Sheet1'!R3C2"
    Range("B7:C7").Select
    Range("TeacherCCO") = "='[TeacherFormula.xlsx]Sheet1'!R3C3"
    Range("F6").Select

    Next

End Sub

我目前正在使用“定义”将一个单元格指向工作簿中的另一个单元格。我不确定这是否是首选方式,但对于这位初学VBA程序员来说,这是最直接的。

有人可以帮我弄清楚如何“迈步”到下一行来收集合适的老师的分数吗?

1 个答案:

答案 0 :(得分:0)

尝试类似下面的内容。它粗糙且未经测试,但希望您会发现它有用。

Option Explicit

Sub Test()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim Teachers As Range
Dim Teacher As Range

'Using objects makes it much easier to maintain your code.
Set wb1 = Workbooks("WKBK 1")
Set wb2 = Workbooks("WKBK 2")
Set ws2 = wb2.Sheets("YourTableSheet") '<~~ The sheet that contains your table.
Set Teachers = ws2.Range("A2:A17") '<~~List of teachers on your table.

'Loop through your list of teachers.
For Each Teacher In Teachers
    'I'm assuming your template sheets are named the same as your teachers.
    'If they (the sheet names and names in the list) aren't exactly the same, 
    'this code won't work.
    Set ws = wb1.Sheets(Teacher)
    'This is where you'll transfer the data. See my examples. You can add as many as needed.
    'This also assumes that every template sheet is set up the same.
    ws.Range("TeacherYTD") = ws2.Range("B" & Teacher.Row).Value '<~~ Copies/pastes the table data in column B for that particular teacher into that teacher's template sheet.
    ws.Range("TeacherCCO") = ws2.Range("C" & Teacher.Row).Value '<~~ Same as above except for column C.
    'If you have additional data to transfer, model additional lines after the above two lines.
Next Teacher

End Sub

希望有所帮助!