如何使用不同的参数对多个单元格执行类似的操作

问题描述 投票:0回答:1

在提供这个答案后,有人问我如何在有多个单元格且每个单元格都有自己的参数(在本例中为占位符文本)的情况下使以下代码工作。


Option Explicit

Const TARGET_CELL_ADDRESS As String = "C2"
Const PLACEHOLDER_TEXT As String = "My placeholder text"

Const GREY_COLOR = -5855579
Const BLACK_COLOR = -16777216

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Replace(Target.AddressLocal, "$", "") = TARGET_CELL_ADDRESS Then
        'Clear text content
        With Target
            If .Value2 = PLACEHOLDER_TEXT Then
                .Value2 = ""
                .Font.Color = BLACK_COLOR
            End If
        End With
    Else
        'Restore placeholder text if needed
        Dim Rng As Range
        Set Rng = Me.Range(TARGET_CELL_ADDRESS)
        With Rng
            If .Value2 = vbNullString Then
                .Value2 = PLACEHOLDER_TEXT
                .Font.Color = GREY_COLOR
            ElseIf .Value2 = PLACEHOLDER_TEXT Then
                If .Font.Color <> GREY_COLOR Then
                    .Font.Color = GREY_COLOR
                End If
            End If
        End With
        
    End If
        
End Sub


我认为最好在一个单独的问题中回答,并尝试使问题的标题更笼统。希望它能让您更轻松地找到此类问题的答案。

excel vba dictionary
1个回答
0
投票

在这种情况下,我们需要一个数据结构,允许我们循环不同的单元格并检索每个单元格相应的文本。

脚本Dictionary是一个不错的选择,因为它允许我们将单元格的地址存储为

key
,将文本存储为
value
(又名项目)。

请注意:

要使用词典(具有早期绑定),您需要首先添加引用:

从 Visual Basic 菜单转到“工具”->“引用”。找到“微软 脚本运行时”在列表中,并选中它旁边的框。

在此处阅读有关如何使用词典的更多信息。

一旦我们有了字典,我们就能够将需要为多个单元格运行的代码包装在

For Each

 循环中。这将允许重复使用代码,每个 
key
(单元格地址)替换 
TARGET_CELL_ADDRESS
 常量,并用相应的 
item
 替换 
PLACEHOLDER_TEXT
 常量。

Option Explicit Const GREY_COLOR = -5855579 Const BLACK_COLOR = -16777216 'Let's define the dictionary at the worksheet module level, so we can store its values in between each run of Worksheet_SelectionChange Private CellsDict As Dictionary Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'Create the dictionary to store the information If CellsDict Is Nothing Then Set CellsDict = New Dictionary CellsDict.Add "C9", "Text for C9" CellsDict.Add "C21", "Text for C21" CellsDict.Add "C58", "Text for C58" CellsDict.Add "C96", "Text for C96" End If 'Now let's loop over each key inside the dict to perform the same changes/checks on each cells listed inside of it. Dim Key As Variant For Each Key In CellsDict.Keys '''''''''''''''''''''''''' 'Here goes the code that needs to run for each cell address in the dictionary '''''''''''''''''''''''''' If Replace(Target.AddressLocal, "$", "") = Key Then 'Clear text content With Target If .Value2 = CellsDict.Item(Key) Then .Value2 = "" .Font.Color = BLACK_COLOR End If End With Else 'Restore placeholder text if needed Dim Rng As Range Set Rng = Me.Range(Key) With Rng If .Value2 = vbNullString Then .Value2 = CellsDict.Item(Key) .Font.Color = GREY_COLOR ElseIf .Value2 = CellsDict.Item(Key) Then If .Font.Color <> GREY_COLOR Then .Font.Color = GREY_COLOR End If End If End With End If '''''''''''''''''''''''''' Next End Sub
注意:即使对于十几个单元格,此代码也应该可以正常工作,但如果单元格列表大几个数量级,则需要进行优化。

© www.soinside.com 2019 - 2024. All rights reserved.