如何在执行所有宏后保持代码处于活动状态

时间:2016-04-29 12:59:03

标签: excel vba excel-vba

我已经编写了多个将使用命令按钮执行的宏但是我希望我的最后一个宏在宏执行之前保持活动状态。我希望Macro15在之后保持活跃状态​​。对于Macro15,我想如果任何细胞更改我想用colorindex 3

突出显示该细胞
Sub RunallMacros()
macro1
macro2
macro3
macro5
Macro12
Macro13
Macro14
Macro15
End Sub




Sub macro1()
ThisWorkbook.Sheets("Main").Activate

End Sub

Sub macro2()
Dim myvalue As Variant
myvalue = InputBox("Enter Safety Stock Days")
Range("R5").value = myvalue

End Sub

Sub macro5()

Dim answer As Integer

answer = MsgBox("Are There Any ICF Forms?", vbYesNo + vbQuestion, "Other Sales")
If answer = vbYes Then ICFUserForm.Show



End Sub

Sub macro3()
Dim MyAnswer1 As Variant
Dim MyAnswer2 As Variant
Dim MyAnswer3 As Variant
Dim MyAnswer4 As Variant
Dim MyAnswer5 As Variant

MyAnswer1 = InputBox("Enter Growth Current Month")
Range("m3").value = MyAnswer1


MyAnswer2 = InputBox("Enter Growth Current Month+1")
Range("n3").value = MyAnswer2


MyAnswer3 = InputBox("Enter Growth Current Month+2")
Range("o3").value = MyAnswer3


MyAnswer4 = InputBox("Enter Growth Current Month+3")
Range("p3").value = MyAnswer4


MyAnswer5 = InputBox("Enter Growth Current Month+4")
Range("q3").value = MyAnswer5

End Sub

Sub Macro12()
    ActiveCell.FormulaR1C1 = "='raw data'!R[-5]C"
    Range("A7").Select
    Selection.AutoFill Destination:=Range("A7:A500"), Type:=xlFillDefault
End Sub

Sub Macro13()
      Range("C7").Select
    Selection.ClearContents
End Sub

Sub Macro14()
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Raw" And ws.Name <> "Main" And ws.Name <> "Calendar" Then
        For Each c In ws.Range("A50:A300")
            ws.Rows(c.Row).Hidden = c.value = 0
        Next
    End If
Next
End Sub

Sub Macro15()
If Not Intersect(Target, Range("A7:AH500")) Is Nothing Or _
  Not Intersect(Target, Range("A7:AH500")) Is Nothing Then
    Target.Interior.ColorIndex = 3
End If

End Sub

2 个答案:

答案 0 :(得分:2)

“活动”的宏正在做某事,即它正在执行代码。在执行代码时,用户无法执行任何操作。因此,要么宏处于活动状态,要么用户处于活动状态。

你想要的是回应一个事件,在这种情况下是Worksheet.Change事件:

Private Sub Worksheet_Change(ByVal Target as Range) 
    Target.Interior.ColorIndex = 3 
End Sub

请参阅https://msdn.microsoft.com/en-us/library/office/ff839775.aspx

答案 1 :(得分:0)

保罗给出了很好的答案 - 试着让它运作起来;

Private Sub Worksheet_Change(ByVal Sh As Object, ByVal Target as Range) 

      Target.Interior.ColorIndex = 3 


End Sub