In an attempt to automate my work, I am trying to generate personalised marksheets with name of student and marker in there as well as a save the workbook as"Student_Marker_Course" (for each student and marker), I have just picked up VBA last week and last time I tried to code something, it was in Java 10 years ago.
The code that I used below works, however, I don't think it is optimised as it does take a little while to generate the 100+ marksheets, I was just wondering if I did it well and where I could try to optimise it, Thanks!
Sub Marksheet()
Dim x As Integer
Dim Wbk1 As Workbook, Wbk2 As Workbook
Dim Filename As String, Course As String
Set Wbk1 = ThisWorkbook
LRsource = Wbk1.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row ' find the final row
For x = 2 To LRsource
Filename = Cells(x, "T")
Course = Cells(x, "G")
'Below will find out which course the student is on and which marksheet to select
If Course = "Course1" Then
Set Wbk2 = Workbooks.Open("C:\Users\XXX\Desktop\Test\Course1.xlsx") 'Select Marksheet
'Below will copy and paste the student name
Wbk1.Sheets(1).Activate
Cells(x, "E").Copy
Wbk2.Activate
Sheets(1).Cells(5, "C").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'Below will copy and paste the Markers name
Wbk1.Sheets(1).Activate
Cells(x, "Q").Copy
Wbk2.Activate
Sheets(1).Cells(7, "C").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Wbk2.SaveAs "Course1_Location\" & Filename & " .xlsx" 'Select destination
Wbk2.Close
Application.CutCopyMode = False
ElseIf Course = "Course2" Then
Set Wbk2 = Workbooks.Open("C:\Users\XXX\Desktop\Test\Course2.xlsx") 'Select Marksheet
'Below will copy and paste the student name
Wbk1.Sheets(1).Activate
Cells(x, "E").Copy
Wbk2.Activate
Sheets(1).Cells(5, "C").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'Below will copy and paste the Markers name
Wbk1.Sheets(1).Activate
Cells(x, "Q").Copy
Wbk2.Activate
Sheets(1).Cells(7, "C").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'Finding where to save it
Wbk2.SaveAs "Course2_Location\" & Filename & " .xlsx" 'Select destination
Wbk2.Close
Application.CutCopyMode = False
Else
Set Wbk2 = Workbooks.Open("C:\Users\XXX\Desktop\Test\Course3_6.xlsx") 'Select Marksheet
'Below will copy and paste the student name
Wbk1.Sheets(1).Activate
Cells(x, "E").Copy
Wbk2.Activate
Sheets(1).Cells(5, "C").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'Below will copy and paste the Markers name
Wbk1.Sheets(1).Activate
Cells(x, "Q").Copy
Wbk2.Activate
Sheets(1).Cells(7, "C").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'Below will copy and paste the course name
Wbk1.Sheets(1).Activate
Cells(x, "G").Copy
Wbk2.Activate
Sheets(1).Cells(3, "D").Select
ActiveSheet.Paste
'Finding where to save it, I have multiple courses here, hence the if
If Course = "Course3" Then
Wbk2.SaveAs "Course3_Location\" & Filename & " .xlsx" 'Select destination
End If
'Repeat above If for each course
Wbk2.SaveAs "course3_Location" & Filename & " .xlsx" 'Select destination
Wbk2.Close
Application.CutCopyMode = False
End If
Next x
End Sub