简体   繁体   中英

Code on Workbook_Open effecting initial view of images on worksheets

I have set Worksheet.EnableCalculation property to manual for a number of sheets on opening the workbook. In the process I also change the size of 2 shapes on each of these sheet (Make one image big and one small). These shapes also have macros assigned to them. To add more confusion, one sheet can be fine on opening the workbook and the rest not. The sheet that is OK is not always the same sheet and I can not work out how that sheets differs at that time from the others. It is not that it is active at time of saving.

This works fine but when viewing the sheets on opening the workbook these shapes as well as all other shapes on the sheet (which are also used as macro buttons) appear to have moved position on the sheet (up and left). However, by just zooming in/out the buttons appear back in their correct position.

I have added code to set the zoom during the Workbook_Open event to set the zoom but that does not stop this anomaly appearing. Putting the zoom into Workbook_SheetActivate also does not effect the issue

It does not have any obvious effect on the workbook and is not an issue once the zoom has been applied manually to each effected sheet but it does not look good when it opens.

Private Sub Workbook_Open()

' ***** STOPS automatic formular updating
' x - Defined Cell Names  Lock_LABEL
' x - Image               Lock_ON    Lock_OFF

Application.ScreenUpdating = False  ' do not see screen updating

Sheets("5_Angebot").Select

    ' Turn automatic forular updating OFF
    ActiveSheet.EnableCalculation = False

    ' Make ON lock Small
    ActiveSheet.Shapes.Range(Array("Lock_ONN")).Select  ' x
    Selection.ShapeRange.Height = 28.3464566929

    ' Make OFF lock Big
    ActiveSheet.Shapes.Range(Array("Lock_OFF")).Select  ' x
    Selection.ShapeRange.Height = 46.7716535433

    ' Label
    Range("ANLock_LABEL").Select                        ' x
    ActiveCell.FormulaR1C1 = "Auto Update is OFF"
    Selection.HorizontalAlignment = xlLeft
    With ActiveCell.Characters(Start:=15, Length:=4).Font
        .FontStyle = "Fett"
        .Size = 10
        .Color = -16776961
    End With
    Range("B1").Select

    Sheets("5_Auftragsb").Select

    ' Turn automatic forular updating OFF
    ActiveSheet.EnableCalculation = False

    ' Make ON lock Small
    ActiveSheet.Shapes.Range(Array("Lock_ONN")).Select  ' x
    Selection.ShapeRange.Height = 28.3464566929

    ' Make OFF lock Big
    ActiveSheet.Shapes.Range(Array("Lock_OFF")).Select  ' x
    Selection.ShapeRange.Height = 46.7716535433

    ' Label
    Range("AULock_LABEL").Select                        ' x
    ActiveCell.FormulaR1C1 = "Auto Update is OFF"
    Selection.HorizontalAlignment = xlLeft
    With ActiveCell.Characters(Start:=15, Length:=4).Font
        .FontStyle = "Fett"
        .Size = 10
        .Color = -16776961
    End With
    Range("B1").Select

    Sheets("5_Abschluss").Select

    ' Turn automatic forular updating OFF
    ActiveSheet.EnableCalculation = False

    ' Make ON lock Small
    ActiveSheet.Shapes.Range(Array("Lock_ONN")).Select  ' x
    Selection.ShapeRange.Height = 28.3464566929

    ' Make OFF lock Big
    ActiveSheet.Shapes.Range(Array("Lock_OFF")).Select  ' x
    Selection.ShapeRange.Height = 46.7716535433

    ' Label
    Range("ABLock_LABEL").Select                        ' x
    ActiveCell.FormulaR1C1 = "Auto Update is OFF"
    Selection.HorizontalAlignment = xlLeft
    With ActiveCell.Characters(Start:=15, Length:=4).Font
        .FontStyle = "Fett"
        .Size = 10
        .Color = -16776961
    End With
    Range("B1").Select

    Sheets("7_FAX_KWagen").Select

    ' Turn automatic forular updating OFF
    ActiveSheet.EnableCalculation = False

    ' Make ON lock Small
    ActiveSheet.Shapes.Range(Array("Lock_ONN")).Select  ' x
    Selection.ShapeRange.Height = 28.3464566929

    ' Make OFF lock Big
    ActiveSheet.Shapes.Range(Array("Lock_OFF")).Select  ' x
    Selection.ShapeRange.Height = 46.7716535433

    ' Label
    Range("KWLock_LABEL").Select                        ' x
    ActiveCell.FormulaR1C1 = "Auto Update is OFF"
    Selection.HorizontalAlignment = xlLeft
    With ActiveCell.Characters(Start:=15, Length:=4).Font
        .FontStyle = "Fett"
        .Size = 10
        .Color = -16776961
    End With
    Range("B1").Select

    ' Turn automatic forular updating OFF
    ActiveSheet.EnableCalculation = False
    Range("B1").Select

    MsgBox "                     Hallo " & vbNewLine & vbNewLine & _
           "   Automatic updating is currently" & vbNewLine & _
           "                   turned off"

     ' Set Zoom on all sheets
    Dim ws As Worksheet

    For Each ws In Worksheets
        ws.Select
        ActiveWindow.Zoom = 120
    Next ws

    Sheets("3_Data Form").Select

    Application.ScreenUpdating = True  ' see screen updating

End Sub

I didn't solve anything, just made a more concise version of your code.

Private Sub Workbook_Open()

' ***** STOPS automatic formular updating

' x - Defined Cell Names  Lock_LABEL
' x - Image               Lock_ON    Lock_OFF
Dim ShName(3, 1) As String
ShName(0, 0) = "5_Angebot"
    ShName(0, 1) = "ANLock_LABEL"
ShName(1, 0) = "5_Auftragsb"
    ShName(1, 1) = "AULock_LABEL"
ShName(2, 0) = "5_Abschluss"
    ShName(2, 1) = "ABLock_LABEL"
ShName(3, 0) = "7_FAX_KWagen"
    ShName(3, 1) = "KWLock_LABEL"
Dim ws As Worksheet
Application.ScreenUpdating = False                                        ' do not see screen updating
For Each ws In ActiveWorkbook.Worksheets
    ws.Select
    ActiveWindow.Zoom = 120                                               ' your zoom, seems unnecessary
    '.Range("B2").Select                                                  ' this one really isn't necessary
    Cells(ActiveWindow.SplitRow + 1, ActiveWindow.SplitColumn + 1).Select ' fancy way instead of B2, selects cell after frozen panels, still unnecessary
Next

For i = 0 To UBound(ShName(, 0))                                          ' not sure about this UBound, it should be the fancy way of 3
Set ws = Sheets(ShName(i, 0))
With ws
    .EnableCalculation = False                                            ' Turn automatic forular updating OFF
    .Shapes.Range(Array("Lock_ONN")).ShapeRange.Height = 28.3464566929    ' Make ON lock Small
    .Shapes.Range(Array("Lock_OFF")).ShapeRange.Height = 46.7716535433    ' Make OFF lock Big
    With .Range(ShName(i, 1))                                             ' Label     ' I'm not sure about this one, should work anyway; the dot might not be necessary
        .FormulaR1C1 = "Auto Update is OFF"
        .HorizontalAlignment = xlLeft
        With .Characters(Start:=15, Length:=4).Font
            .FontStyle = "Fett"
            .Size = 10
            .Color = -16776961
        End With
    End With
End With
Next i
MsgBox "                     Hallo " & vbNewLine & vbNewLine & _
           "   Automatic updating is currently" & vbNewLine & _
           "                   turned off"
Sheets("3_Data Form").Select
Application.ScreenUpdating = True                                         ' see screen updating
End Sub

Ok, not exactly solved the issue but have got the workaround to hide the problem.

I have added a second zoom in the code.

It looks like when actioning the zoom coding it only affected the sheets that were not already set on the zoom value which is why it appeared random to which sheets were opening with the screen looking OK. When changing it twice it effectively results in them all being 'changed' and hence resolving the screen updating glitch.

It will work for now but appreciate that it is just a bandaid.

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