简体   繁体   English

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

[英]VBA Form Completion across multiple worksheets

I have a standard template I've designed in WKBK 1 that I am using to grade teachers.我有一个在WKBK 1中设计的标准模板,用于给教师评分。 WKBK 1 has 15 different worksheets, or 1 per teacher. WKBK 1有 15 个不同的工作表,或每位教师 1 个。 In WKBK 2 , I have a table I am using to keep scores on their different functions.WKBK 2 ,我有一个表格用于记录不同功能的分数。 I'm using a macro (or attempting to) to extract data in WKBK 2 and dump it in WKBK 1 .我正在使用宏(或尝试)在WKBK 2提取数据并将其转储到WKBK 1 When I attempt to loop the macro throughout the worksheets, the macro is pulling from teacher 1 and assigning their scores to teachers 2-15.当我尝试在整个工作表中循环宏时,宏从老师 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

I am currently using 'Define' to point one cell to another across workbooks.我目前正在使用“定义”跨工作簿将一个单元格指向另一个单元格。 I am not sure if that is the preferred way, but it was most direct for this beginner VBA programmer.我不确定这是否是首选方式,但对于这位初学者 VBA 程序员来说,这是最直接的方式。

Can someone help me figure out how to "step" to the next row to gather the proper teacher's scores?有人可以帮我弄清楚如何“跨步”到下一行来收集合适的老师的分数吗?

Try something like the below.尝试类似下面的内容。 It's rough and untested , but hopefully you'll find it helpful.它很粗糙且未经测试,但希望您会发现它有帮助。

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

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM