2016-11-29 42 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()

相關問題