I'm currently working on a workbook with "SHEET1" and "SHEET2". Now, when there's an empty cell in SHEET1 or SHEET2, it will prompt the user to add values on the mandatory cells.
Example:
CELL B2 = NULL in SHEET1
- It will prompt the user that there's a missing value on that cell and redirect to that cell but once the you clicked the message box, it will redirect to the SHEET2.
PROBLEM: If the missing cell is for SHEET1, I dont want to activate the SHEET2.
Here's my code below:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim maxrow As Long
Dim maxrow2 As Long
Dim errcnt As Long
Dim errcnt2 As Long
ThisWorkbook.Sheets("SHEET1").Select
ThisWorkbook.Sheets("SHEET1").Cells(1, 1).Select
Range("A1048576").Select
Selection.End(xlUp).Select
maxrow = Selection.Row
For I = 2 To maxrow
If ThisWorkbook.Sheets("SHEET1").Cells(I, 1).Value = "YES" Then
For j = 2 To 10
If ThisWorkbook.Sheets("SHEET1").Cells(I, j).Value = "" Then
ThisWorkbook.Sheets("SHEET1").Select
ThisWorkbook.Sheets("SHEET1").Cells(I, j).Activate
errcnt = errcnt + 1
End If
Next j
End If
If ThisWorkbook.Sheets("SHEET1").Cells(I, 1).Value = "NO" Then
For j = 2 To 10
If ThisWorkbook.Sheets("SHEET1").Cells(I, j).Value = "" Then
ThisWorkbook.Sheets("SHEET1").Select
ThisWorkbook.Sheets("SHEET1").Cells(I, j).Activate
errcnt = errcnt + 1
End If
Next j
End If
Next I
If errcnt > 0 Then
MsgBox "There are " & errcnt & " fields missing", vbExclamation, "Missing Value"
Cancel = True
Else
End If
ThisWorkbook.Sheets("SHEET2").Select
ThisWorkbook.Sheets("SHEET2").Cells(1, 1).Select
Range("A1048576").Select
Selection.End(xlUp).Select
maxrow2 = Selection.Row
For X = 2 To maxrow2
If ThisWorkbook.Sheets("SHEET2").Cells(X, 1).Value = "YES" Or ThisWorkbook.Sheets("SHEET2").Cells(X, 1).Value = "NO" Then
For j = 2 To 10
If ThisWorkbook.Sheets("SHEET2").Cells(X, j).Value = "" Then
ThisWorkbook.Sheets("SHEET2").Select
ThisWorkbook.Sheets("SHEET2").Cells(X, j).Activate
errcnt2 = errcnt2 + 1
End If
Next j
End If
Next X
If errcnt2 > 0 Then
MsgBox "There are " & errcnt & " fields missing", vbExclamation, "Missing Value"
Cancel = True
Else
End If
End Sub
I believe this is because of this code:
ThisWorkbook.Sheets("SHEET2").Select