Hi Jomili
I have noticed a glitch with looking for the last row then offsetting 6 this means if i insert more then 1 student rows on the student details and data tab, when the new sheet is created it takes the name of the last student added not the 1st of the new students added,
Please see attached work book.
I was wondering if there would be a way to combine all the following 3 VBA so when you click the insert mew student rows it adds the rows on the Student details and data worksheet, Exam data worksheet and then adds the new worksheet for the student aswell?
This would solve the issue and also save time by have to switch between worksheets to add the rows.
Thanks for your help.
Code that adds new worksheet
Sub AddNewSheet(): Dim ws As Worksheet, S As String, n As Long, i As Long, C As Range
Dim wdd As Worksheet: Set wdd = Sheets("Student Data & Details")
Dim LastRow As Long
'Determine our last row
LastRow = Range("B" & Rows.Count).End(xlUp).Row
'Get new sheet name - if null then quit
' n = 7 * (Worksheets.Count - 1): S = wdd.Range("B" & n).Value
S = Range("B" & LastRow).Offset(-6, 0)
MsgBox S
If S = "" Then Exit Sub
Application.EnableEvents = False 'Shut off events
'Copy last sheet to new last place
Set ws = Worksheets(Worksheets.Count): ws.Copy After:=Worksheets(Worksheets.Count)
'Set and name the new sheet
Set ws = ActiveSheet: ws.Name = S: ws.Cells.Hyperlinks.Delete
For Each C In ws.UsedRange 'look at every cell in new sheet
If C.HasFormula Then 'Cells with Formulas
S = C.Formula: i = InStr(1, S, "!") + 1
'it puts hyperlink in B22? - new feature?
If IsNumeric(Right(S, Len(S) - i)) Then n = Right(S, Len(S) - i) Else GoTo GetNext
'Going inside the formula If the ref is to Exam add 20 else add 5 to the row number
n = IIf(InStr(1, S, "Exam"), n + 20, n + 7)
S = Left(S, i) & n: C.Formula = S 'Reconstruct the Formula with the new row number
End If
GetNext: Next C 'Get the next Cell
'Turn on Events and Quit
Application.EnableEvents = True: End Sub
Option Explicit
Code that Adds new rows on the student details and data worksheet
Sub InsertRows()
Const sROWS_TO_COPY As String = "10:16"
Dim vNoOfStudents As Variant
Dim iNoOfStudents As Integer
Dim iStudentNo As Integer
Dim rLastRow As Range
Dim wks As Worksheet
vNoOfStudents = InputBox("How many new students should be addeded?", "How many")
If IsNumeric(vNoOfStudents) Then
iNoOfStudents = CInt(vNoOfStudents)
If iNoOfStudents > 0 Then
Set wks = ActiveSheet
Set rLastRow = wks.Rows.Find("*", , , , xlByRows, xlPrevious)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For iStudentNo = 1 To iNoOfStudents
Application.StatusBar = "Adding Student No " & iStudentNo & " . . ."
With wks
.Rows(sROWS_TO_COPY).Copy
rLastRow.Offset(1, 0).EntireRow.Insert
End With
Next iStudentNo
Application.StatusBar = False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.CutCopyMode = False
End If
End If
End Sub
Code that inserts rows on Exam data input worksheet
Sub InsertRows(): Dim R As Range
Set R = Rows.Find("*", , , , xlByRows, xlPrevious)
With ActiveSheet
.Rows("4:23").Copy
R.Offset(1, 0).EntireRow.Insert
End With
Application.CutCopyMode = False
l = Range("A" & Rows.Count).End(xlUp).Row - 19
Range("A" & l).Resize(20).Formula = "='Student Data & Details'!$B$" & ((l - 4) / 20) * 7 + 10
End Sub
Bookmarks