Felipe
Felipe

Reputation: 59

Executing Macro without showing other sheet

I've developed the following macro to insert a new line in Sheet 2 and to Autofill this new line from another one. The macro is running properly.

Sub AutoFill()
    Worksheets("Sheet2").Unprotect
    Worksheets("Sheet2").Rows("8:8").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Set SourceRange = Worksheets("Sheet2").Rows(9)
    Set fillRange = Worksheets("Sheet2").Range("8:9")
    SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault    
    Worksheets("Sheet2").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Worksheets("Sheet2").EnableSelection = xlNoRestrictions
End Sub

I set to run the macro on Sheet1 and I don't want Sheet2 to be shown when I do it. How do I do that?

Upvotes: 1

Views: 561

Answers (1)

SierraOscar
SierraOscar

Reputation: 17637

turn screenupdating off, but ensure you switch it back on again when the code has finished:

Sub AutoFill()

Application.ScreenUpdating = False
Worksheets("Sheet2").Unprotect

Worksheets("Sheet2").Rows("8:8").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove


    Set SourceRange = Worksheets("Sheet2").Rows(9)
    Set fillRange = Worksheets("Sheet2").Range("8:9")
    SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault

Worksheets("Sheet2").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Worksheets("Sheet2").EnableSelection = xlNoRestrictions

Application.ScreenUpdating = True
End Sub

Upvotes: 1

Related Questions