excel vba freeze pane without select

后端 未结 5 1344
情歌与酒
情歌与酒 2020-11-30 07:39

I have a VBA script in Excel that freezes the panes of an Excel worksheet, but I\'m curious to see if this is possible without first selecting a range. Here\'s by code now

5条回答
  •  悲&欢浪女
    2020-11-30 08:14

    I need to be able to properly refreeze panes (when creating new windows, notably) without losing the activecell or messing up the visible range. It took a lot of playing around but I think I have something solid that works:

    Sub FreezePanes(nbLignes As Integer, nbColonnes As Integer, Optional ByVal feuille As Worksheet)
        If feuille Is Nothing Then Set feuille = ActiveSheet Else feuille.Activate
        Error GoTo erreur
        With ActiveWindow
            If .View = xlNormalView Then
                If .FreezePanes Then .FreezePanes = False
                If .Split Then .Split = False
    
                .SplitColumn = nbColonnes
                .SplitRow = nbLignes
    
                If .Panes.Count = 4 Then 'rows and columns frozen
                    .Panes(1).ScrollRow = 1
                    .Panes(1).ScrollColumn = 1
                    .Panes(2).ScrollRow = 1 'top right pane
                    .Panes(3).ScrollColumn = 1 'bottom left pane
                ElseIf nbLignes > 0 Then .Panes(1).ScrollRow = 1
                ElseIf nbColonnes > 0 Then .Panes(1).ScrollColumn = 1
                Else: GoTo erreur
                End If
    
                .FreezePanes = True
            End If
        End With
        Exit Sub
    erreur:
        Debug.print "Erreur en exécutant le sub 'FreezePanes " & nbLignes & ", " & nbColonnes & ", '" & feuille.Name & "' : code #" & Err.Number & Err.Description
    End Sub
    

提交回复
热议问题