如何解决存储“公共”数组值
我在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 举报,一经查实,本站将立刻删除。