Hi could someone advise me how to get a workbook to run a macro before it closes please.
I have a workbook called 'StaffDetails' it contains a list of staff names and staff grades. In this work book I have a module with the following code
And on ThisWorkbook I have the this code
In another workbook book I have a userform with the following code behind the Commandbutton click event
Everything works fine apart from the columns in the first workbook StaffDetails don't sort themselves when the workbook closes, although if I close it manually then the SortCol macro works fine.
I'm thinking it has something to do with the commandbutton click event but to be honest I'm not that sure and have tried loads of different methods to get the workbook to close and sort the columns.
Please could someone let me know where I'm going wrong
Many thanks
I have a workbook called 'StaffDetails' it contains a list of staff names and staff grades. In this work book I have a module with the following code
Code:
Sub SortCol()
Dim rng As Range, r As Range
Set rng = Range("A2:B1000")
Set r = Range("A2")
Application.ScreenUpdating = False
rng.Sort key1:=r, order1:=xlAscending, Header:=xlNo, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Application.ScreenUpdating = True
End Sub
And on ThisWorkbook I have the this code
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call SortCol
ActiveWorkbook.Save
End Sub
In another workbook book I have a userform with the following code behind the Commandbutton click event
Code:
Private Sub CommandButton1_Click()
Dim StaffName As String
Dim StaffGrade As String
Dim StaffDetails As Workbook
If Textbox1.Text = "" Or ComboBox1.Text = "" Then
MsgBox "Missing entry.", vbExclamation, "Entry Canceled"
ElseIf IsNumeric(Application.Match(Textbox1.Text, Sheets("StaffDetailsList").Range("A:A"), 0)) Then
MsgBox "This name is already in the list, please click cancel and look again.", vbExclamation, "Duplicate Entry"
Else
Worksheets("StaffDetailsList").Select
StaffName = Textbox1.Value
Worksheets("StaffDetailsList").Select
StaffGrade = ComboBox1.Value
Workbooks.Open ("C:\Users\Paul\Desktop\2017 Diaries\StaffDetails.xlsm")
Worksheets("StaffList").Select
Worksheets("StaffList").Range("A1").Select
RowCount = Worksheets("StaffList").Range("A1").CurrentRegion.Rows.Count
With Worksheets("StaffList").Range("A1")
.Offset(RowCount, 0) = StaffName
.Offset(RowCount, 1) = StaffGrade
End With
Workbooks("StaffDetails").Close SaveChanges:=True
End If
Unload Me
MsgBox "Your entry has been accepted. Please return to the date(s) required to enter the name on the list", vbInformation, "Entry Accepted"
End Sub
Everything works fine apart from the columns in the first workbook StaffDetails don't sort themselves when the workbook closes, although if I close it manually then the SortCol macro works fine.
I'm thinking it has something to do with the commandbutton click event but to be honest I'm not that sure and have tried loads of different methods to get the workbook to close and sort the columns.
Please could someone let me know where I'm going wrong
Many thanks