Setting selection to Nothing when programming Excel

后端 未结 17 1855
误落风尘
误落风尘 2021-02-02 09:35

When I create a graph after using range.copy and range.paste it leaves the paste range selected, and then when I create a graph a few lines later, it uses the selection as the f

17条回答
  •  梦谈多话
    2021-02-02 09:57

    I do not think that this can be done. Here is some code copied with no modifications from Chip Pearson's site: http://www.cpearson.com/excel/UnSelect.aspx.

    UnSelectActiveCell

    This procedure will remove the Active Cell from the Selection.

    Sub UnSelectActiveCell()
        Dim R As Range
        Dim RR As Range
        For Each R In Selection.Cells
            If StrComp(R.Address, ActiveCell.Address, vbBinaryCompare) <> 0 Then
                If RR Is Nothing Then
                    Set RR = R
                Else
                    Set RR = Application.Union(RR, R)
                End If
            End If
        Next R
        If Not RR Is Nothing Then
            RR.Select
        End If
    End Sub
    

    UnSelectCurrentArea

    This procedure will remove the Area containing the Active Cell from the Selection.

    Sub UnSelectCurrentArea()
        Dim Area As Range
        Dim RR As Range
    
        For Each Area In Selection.Areas
            If Application.Intersect(Area, ActiveCell) Is Nothing Then
                If RR Is Nothing Then
                    Set RR = Area
                Else
                    Set RR = Application.Union(RR, Area)
                End If
            End If
        Next Area
        If Not RR Is Nothing Then
            RR.Select
        End If
    End Sub
    

提交回复
热议问题