Excel 空时如何激活单元格位置

Excel 空时如何激活单元格位置,excel,vba,Excel,Vba,我目前正在编写一份包含“SHEET1”和“SHEET2”的工作簿。现在,当SHEET1或SHEET2中有空单元格时,它将提示用户在必填单元格上添加值 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 wi

我目前正在编写一份包含“SHEET1”和“SHEET2”的工作簿。现在,当SHEET1或SHEET2中有空单元格时,它将提示用户在必填单元格上添加值

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.
问题:如果缺少的单元格用于SHEET1,我不想激活SHEET2

下面是我的代码:

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
我相信这是因为这个准则:

此工作簿。工作表(“SHEET2”)。选择

,而不是此:

If errcnt > 0 Then
    MsgBox "There are " & errcnt & " fields missing", vbExclamation, "Missing Value"
    Cancel = True
Else
End If
试试这个:

If errcnt > 0 Then
    MsgBox "There are " & errcnt & " fields missing", vbExclamation, "Missing Value"
    Cancel = True
    Exit Sub
End If

请注意,我通过exit sub更改了ELSE(在那里没有用)。这将触发结束宏,以防SHEET1中缺少某些内容,并且它将无法继续选择SHEET2。

您没有说明发生了什么错误,您也没有问任何问题。@ScottCraner here:-它将提示用户该单元格上缺少值并重定向到该单元格,但一旦您单击消息框,它将重定向到SHEET2。它不能重定向到Sheet2你有很多记录器代码在那里…利用这些信息我尝试了这一个,但文件将被保存。我不想在缺少必填单元格时保存文档。@bobajob我对这个很陌生。我目前正在使用工作簿\u BeforeSave函数。但在保存之前,我想确认是否所有的madantory单元格都已填满。