一旦范围内的所有单元格都具有值,则触发macros

我想触发一个macros,只有在给定范围内的所有单元格都有一个值后才能运行。 我想让我的input范围在工作簿closures时没有input值的情况下打开。 目前下面的代码在打开工作簿时运行。

Sub Page_Setup() Sheet1.Unprotect Application.EnableEvents = False Application.ScreenUpdating = False Sheet1.Range("Inputs").Style = "40% - Accent2" Sheet1.Range("Calcs").Style = "40% - Accent3" Sheet1.Range("C8:C12").Value = "-" Sheet1.Range("Calcs").Value = "-" Sheet1.Range("C13").Value = 64 Sheet1.Range("C14:C16").Value = 0 Application.EnableEvents = True Application.ScreenUpdating = True End Sub 

一旦用户在C8到C12的每个单元格中input了一个值,我想要运行这个macros。 在此先感谢您的帮助。

编辑:

我想只有当范围“C8:C12”有一个数字值才能运行macros。 当工作簿最初打开时,我倾向于清除该范围,以避免显示工作簿closures时input的值(并由macros计算)。 下面的代码在工作簿打开时运行。

 Private Sub Workbook_Open() Application.ScreenUpdating = False Sheet1.Range("User").Value = Environ("UserName") Sheet1.Range("Run_Date").Value = Format(Date, "mm/dd/yy") Application.Run ("Page_Setup") Application.ScreenUpdating = True End Sub 

然后我想触发一个单独的macros(Case1)运行一次C8:C12都有数字值。 我有下面的代码

 Private Sub Worksheet_Change(ByVal Target As Range) Sheet1.Unprotect Sheet2.Unprotect Application.ScreenUpdating = False Application.EnableEvents = False Dim KeyCells As Range Set KeyCells = Worksheets("Sheet1").Range("Inputs") If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then Application.Run ("Case1") End If Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

我的问题是Case1将返回错误,如果单元格没有数字值。 对不起,我希望这个澄清我的问题。

我的理解是你必须

  • ThisWorkbook代码窗格中放置以下代码:

     Private Sub Workbook_Open() Application.ScreenUpdating = False Application.EnableEvents = False '<--| this to avoid subsequent '.Range("Inputs").ClearContents' trigger 'Worksheet_Change()' With Sheet1 .Range("User").Value = Environ("UserName") .Range("Run_Date").Value = Format(Date, "mm/dd/yy") .Range("Inputs").ClearContents End With Application.Run "Page_Setup" Application.EnableEvents = True Application.ScreenUpdating = True End Sub 
  • 将下面的代码放在Sheet1代码窗格中:

     Private Sub Worksheet_Change(ByVal Target As Range) With Range("Inputs") '<--| reference your "inputs" range If Not Intersect(.Cells, Target) Is Nothing Then '<--| if change affects referenced range If WorksheetFunction.Count(.Cells) = .Cells.Count Then '<--| if referenced range is "full" with numbers Application.ScreenUpdating = False Application.EnableEvents = False Sheet1.Unprotect Sheet2.Unprotect On Error GoTo ErrHandler '<--| make sure you always exit this event handler properly Application.Run "Case1" ErrHandler: '<--| restore settings Sheet1.Protect '<-- add password if needed Sheet2.Protect '<-- add password if needed Application.ScreenUpdating = True Application.EnableEvents = True End If End If End With End Sub 
 Public b_is_run As Boolean Private Sub Worksheet_Change(ByVal Target As Range) If Not b_is_run And (WorksheetFunction.CountA(Range("c8:C12")) = 5) Then b_is_run = True Debug.Print "run here" End If End Sub Public Sub restart() b_is_run = False End Sub 

你有一个公共的布尔b_is_run ,它检查代码是否运行一次,如果是,它不会再运行它。 如果要重新启动 – closures工作表或restart()