简体   繁体   中英

How to remain a code active after all macros have been executed

I have written multiple macros that will be executed with a command button however I want my last macro to remain active after all before macros have been executed. I want Macro15 to remain active after. And for for Macro15 I want if any cell changes I want to highlight that cell with 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

A macro that is "active" is doing something, ie it is executing code. While it is executing code, the user can't do anything. So either the macro is active or the user is active.

What you want is to respond to an event, in this case the Worksheet.Change event:

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

See https://msdn.microsoft.com/en-us/library/office/ff839775.aspx

Great answer from Paul - try this to get it working;

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

      Target.Interior.ColorIndex = 3 


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM