存储“公共”数组值

问题描述

我在Sub中声明一维数组的值。 当我再次运行相同的Sub时,我需要读取上一次运行的数组值。

在特定示例中,我以黄色高亮显示用户选择的范围。 当用户“单击”时,我想恢复单元格上一次运行时得到的原始颜色(作为数组值),但是无法存储。

我知道我可以使用变通办法,但是我想用Public array或类似的方法解决它。

请在下面找到代码

Option Explicit
Public PreColor
Public UseIndex As Boolean
Public TargetArrayColors As Integer

Private Sub Workbook_open()
    
Dim xLastRng As Range

    PreColor = 16777215
    Set xLastRng = Selection
    UseIndex = True

End Sub


Sub Workbook_SheetSelectionChange(ByVal Sh As Object,ByVal Target As Excel.Range)

Dim Counter As Integer
Dim i As Integer
Dim Cell
Dim TargetArrayColors()

    Static xLastRng As Range
    On Error Resume Next
    Counter = -1
    i = -1
    TargetArrayColors = Array()
    
'   Rimetti il colore vecchio facendo attenzione che se era senza colore bisogna usare color.index
'   Se il vecchio target era una cella sola
    If xLastRng.Cells.Count = 1 Then
        If UseIndex = False Then
        xLastRng.Interior.Color = PreColor
        Else
        xLastRng.Interior.ColorIndex = xlNone
        UseIndex = False
        End If
'   Se invece era già un insieme di celle,bisogna rimettere i valori di colore precedentemente immagazzinati
    Else
        For Each Cell In xLastRng
'        Cell.Select
        i = i + 1
        xLastRng.Interior.Color = TargetArrayColors(i)
        Next
    End If
    
    Erase TargetArrayColors

'   Prendi il colore che andrà poi rimesso quando deselezionerai il range e quindi si toglierà il giallo.
'   Se si tratta di una cella sola è molto semplice,altrimenti compila l'Array con il colore di ciascuna cella del target
    If Target.Cells.Count > 1 Then 'InStr(Target.Address,":") > 0 Or InStr(Target.Address,",") > 0 Or InStr(Target.Address,";") > 0 Then
        For Each Cell In Target
        Counter = Counter + 1
        ReDim Preserve TargetArrayColors(Counter)
        TargetArrayColors(Counter) = Cell.Interior.Color
        Next
    Else
    PreColor = Target.Interior.Color
    End If

'   Se il fondo è trasparente accendi la variabile che occorre per usare il Color.Index
    If PreColor = 16777215 Then UseIndex = True

'   Metti il giallo al range che hai sottolineato
    Target.Interior.ColorIndex = 6
'   Definisci quello che sarà l'ultimo range come l'attuale target
    Set xLastRng = Target



End Sub

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...