I appear to have lost contact and so I have repeated my question in the hope I can get assistance on the what appears to be the last part of my problem. The code provided worked on the Meals-Cost worksheet but when I modified (essentially copied with a change of the worksheet name) it cause an error at the section that I showed in the jpg image above. I am hoping that a minor change to the code with solve the problem.
I have also attached the modified spreadsheet.
Private Sub AddNameButton_Click()
Dim ws As Worksheet
Dim Irow, Nrow As Long
Set ws = Worksheets("Name")
' Enter a new name on form
If Me.Forename.Text = "" Or Me.Forename.Text = " " Then
MsgBox "You did not enter a Forename", , "Forename Missing"
Exit Sub
End If
If Me.SURNAME.Text = "" Or Me.SURNAME.Text = " " Then
MsgBox "You did not enter a Surname", , "Surname Missing"
Exit Sub
End If
'Turn off screen updating during next process
Application.ScreenUpdating = False
'If correct forename & surname write it back to the Name sheet for later selection if required
With ws
Irow = .Range("A" & Rows.Count).End(xlUp).Row + 1 'Remenber header row then get the last row used
.Cells(Irow, 1).Value = Me.Forename.Text
.Cells(Irow, 2).Value = Me.SURNAME.Text
End With
' =============================================================================== Start of new code
Set ws = Worksheets("Meals-Taken")
ActiveSheet.Unprotect
'Select top name cell in Meals-Costs sheet and create blank row below with same formatting
With ws
Nrow = .Range("B" & Rows.Count).End(xlUp).Row - 2 'Remenber Lastrow = Totals - blankrow = actual row of last name
.Rows(Nrow + 1).Select
Selection.Insert Shift:=xlDown
.Range("B" & Nrow, "BE" & Nrow).Copy Destination:=.Range("B" & Nrow + 1, "BE" & Nrow + 1)
'Insert full name into sheet can be amended if req
.Range("B" & Nrow + 1).Value = Me.Forename.Text & " " & Me.SURNAME.Text
Application.CutCopyMode = False
End With
'clear the sort and redo
With ws
.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=Range( _
"B10"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
End With
'Only sorts column B if all fields are sorted then it will copy formula's from that
'line for example created on line 39 on full sort will have calc for row 39 !!!!!!!!!!
'You also need to match this sheet with other meals sheet good luck !
With ActiveWorkbook.Worksheets("Meals-Costs").Sort
.SetRange Range("B10", "B" & Nrow + 2)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' ================================================================================ End of new code
Set ws = Worksheets("Meals-Costs")
ActiveSheet.Unprotect
'Select top name cell in Meals-Costs sheet and create blank row below with same formatting
With ws
Nrow = .Range("B" & Rows.Count).End(xlUp).Row - 2 'Remenber Lastrow = Totals - blankrow = actual row of last name
.Rows(Nrow + 1).Select
Selection.Insert Shift:=xlDown
.Range("B" & Nrow, "BE" & Nrow).Copy Destination:=.Range("B" & Nrow + 1, "BE" & Nrow + 1)
'Insert full name into sheet can be amended if req
.Range("B" & Nrow + 1).Value = Me.Forename.Text & " " & Me.SURNAME.Text
Application.CutCopyMode = False
End With
'clear the sort and redo
With ws
.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=Range( _
"B10"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
End With
'Only sorts column B if all fields are sorted then it will copy formula's from that
'line for example created on line 39 on full sort will have calc for row 39 !!!!!!!!!!
'You also need to match this sheet with other meals sheet good luck !
With ActiveWorkbook.Worksheets("Meals-Costs").Sort
.SetRange Range("B10", "B" & Nrow + 2)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Application.ScreenUpdating = True
'Unload the Userform
Unload Me
End Sub