Can you please tell me what this event is doing Private Sub Worksheet_Activate() Dim…
- User
Can you please tell me what this event is doing
Private Sub Worksheet_Activate()
Dim response As Integer
If ThisWorkbook.Worksheets("Observe").Range("E1").Value = "SUMMARY PRINTED" Then
response = MsgBox("Would you like to clear data from the worksheets?", vbYesNo + vbQuestion, "Clear Data")
If response = vbYes Then
Dim ObserveSheet As Worksheet Set ObserveSheet = ThisWorkbook.Worksheets("Observe")
Dim monthSheet As Worksheet Dim monthName As String monthName = Format(ObserveSheet.Range("A1").Value, "mmmyy")
'''''On Error Resume Next Set monthSheet = ThisWorkbook.Worksheets(monthName) '''''''On Error GoTo 0
''''''If monthSheet Is Nothing Then ''''''Set monthSheet = ThisWorkbook.Worksheets.Add monthSheet.Name = monthName ''''''End If
Dim LastRow As Long LastRow = monthSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
ObserveSheet.Range("A1:C30").Copy 'monthSheet.Range("A" & lastRow).Resize(, 3).PasteSpecial xlPasteValues monthSheet.Range("A" & LastRow).PasteSpecial xlPasteValues Application.CutCopyMode = False
ClearData Sheets("Prem MG") ClearData Sheets("Prem SU") ClearData Sheets("Prem Cl") ClearData Sheets("Prem AA") ClearData Sheets("Prem PM") ClearData Sheets("Observe")
End If
End Sub Sub ClearData(sheet As Worksheet)
sheet.Range("A2:B21").Value = ""
If sheet.Name = "Observe" Then sheet.Range("A2:E30").Value = "" End If
End Sub
- Assistant
This event is triggered when the worksheet is activated. It checks if cell E1 in the "Observe" sheet has the value "SUMMARY PRINTED". If it does, a message box prompts the user whether they want to clear data from other worksheets. If the user clicks "Yes", data from several worksheets including "Observe" is cleared. Then a new worksheet is created with the name represented by the month and year in cell A1 of the "Observe" sheet. Data from cells A1 to C30 of "Observe" sheet is copied to the new worksheet, starting from the last row. Finally, the ClearData subroutine is called to clear data from specific cells in some of the worksheets.
- User
What I actually want it to do is this: The event should triggered when the worksheet is activated. It should check if cell E1 in the “Observe” sheet has the value “SUMMARY PRINTED”. If it does, a message box should prompt the user whether they want to clear data from other worksheets. If the user clicks “Yes”, it should check if there is a worksheet with the name represented by the month and year in cell A1 of the “Observe” sheet. If there is such a worksheet, the Data from cells A1 to C30 of “Observe” sheet should then be copied to the worksheet with the name represented by the month and year in cell A1 of the “Observe” sheet, starting from the last row. Finally, the ClearData subroutine is called to clear data from specific cells in some of the worksheets. Can the code be modified to do this.
- Assistant
Yes, the code can be modified to achieve this. Please see the modified code below:
Private Sub Worksheet_Activate()
Dim response As Integer
If ThisWorkbook.Worksheets("Observe").Range("E1").Value = "SUMMARY PRINTED" Then
response = MsgBox("Would you like to clear data from the worksheets?", vbYesNo + vbQuestion, "Clear Data")
If response = vbYes Then
Dim ObserveSheet As Worksheet Set ObserveSheet = ThisWorkbook.Worksheets("Observe")
Dim monthSheet As Worksheet Dim monthName As String monthName = Format(ObserveSheet.Range("A1").Value, "mmmyy")
On Error Resume Next Set monthSheet = ThisWorkbook.Worksheets(monthName) On Error GoTo 0