2016-11-29 31 views
1

我想触发一个宏,只有在给定范围内的所有单元格都有一个值后才能运行宏。我希望打开输入范围时不会在工作簿关闭时输入值。目前下面的代码在打开工作簿时运行。一旦一个范围内的所有单元格都有值,触发宏

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的每个单元格中输入一个值。预先感谢您的帮助。

编辑:

我想是如果范围“C8:C12”宏只运行有一个数值。当工作簿最初打开时,我更愿意清除该范围,以避免显示工作簿关闭时输入的值(并由宏计算)。下面的代码在工作簿打开时运行。

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 

然后我想触发一个单独的宏(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将返回一个错误。对不起,我希望这可以澄清我的问题。

+3

你不能直接这样做,但你可以用'Worksheet.Change()'来模拟它在change事件中,检查用户是否刚刚在C8:C12中输入了一个值,并且如果所有这些单元格都有一个值,如果是的话,调用你想要的宏。变化事件充当守门员。 –

+0

如何检查单元格是否有数字值?我有一个Worksheet.Change()运行,当范围更改时触发宏。 – Derek

+0

将值输入到Sheet1上吗?与PageSetup代码相同?在输入所有数值后,C8:C12每次都会被清除掉? –

回答

0

我的理解是你必须

  • 发生在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 
    
+0

谢谢你的帮助!一切工作我现在想如何 – Derek

+0

不客气 – user3598756

0
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,检查代码是否被运行一次,如果是,它不会再次运行。如果你想重新启动 - 关闭工作表或restart()

相关问题