简体   繁体   中英

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 has 15 different worksheets, or 1 per teacher. In WKBK 2 , I have a table I am using to keep scores on their different functions. I'm using a macro (or attempting to) to extract data in WKBK 2 and dump it in 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.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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