一旦范围内的所有单元格都有值,就触发宏

时间:2016-11-29 20:11:31

标签: excel vba excel-vba

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

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将返回错误。对不起,我希望这能澄清我的问题。

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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
    
相关问题