【问题标题】:Highlight names in sheet1 that are in sheet2突出显示 sheet1 中位于 sheet2 中的名称
【发布时间】:2021-05-02 17:44:13
【问题描述】:

我想使用 findnext 和字典来比较两个工作表中的列表,并突出显示工作表 1 中的单元格(工作表名称是 shH)(A 列中的文本)如果它出现在 sheet2 中(工作表名称是 shS)(文本在 B 列)。

正在显示

编译错误:参数数量错误或属性分配无效

FINDNEXT

并显示

运行时错误“9”:下标超出范围

对于x=ar(i,1)这一行。

For i = 0 To dict.Count - 1
    x = ar(i, 1)
    Set rg = shH.range("A2", shH.range("A" & Rows.Count).End(xlUp))
    Set cell = rg.Find("*x*", MatchCase:=False)
    
    If Not cell Is Nothing Then
        cell.Interior.colourindex = 27
        first = cell.Address
        Do
            Set cell = rg.FindNext("*cell*", Matchcare:=False)
            cell.Interior.colourindex = 27
        Loop While first <> cell.adress
    End If
    
Next i

我的逻辑是使用字典记录 sheet2(shS) 中显示的名称并在 sheet1(shH) 中找到它们并突出显示它们,突出显示部分匹配的所有内容。

【问题讨论】:

  • 在模块开头使用Option Explicit。这将迫使您声明所有变量并指出您在代码中的各种拼写错误。如果仍然有问题,请edit您的帖子添加完整的代码并更准确地报告问题。

标签: excel vba


【解决方案1】:

我不确定我是否正确理解了您想要的内容。此代码会将数据集中的每个单元格与另一张表进行比较。如果它们相等,它将用颜色填充单元格。

只有当两个数据集在同一范围内时才会起作用。

当你尝试时,不要忘记更改代码配置部分中的值。

Sub highlight()

Dim this_row, db_col, last_row, start_row As Integer
Dim name_s1, name_s2 As String

'CONFIG THIS BEFORE YOU RUN THE MACRO
'-----------------------------------------
db_col = 2                'in what column number is the data?
start_row = 2             'in what column does the data start?
name_s1 = "Sheet1"        'name of first sheet
name_s2 = "Sheet2"        'name of second sheet
'------------------------------------------

'get the last row of the data set
last_row = Sheets(name_s1).Cells(Rows.Count, db_col).End(xlUp).Row

'loop through the column
For this_row = start_row To last_row

        'compare cells from sheet1 to sheet2
        If Sheets(name_s1).Cells(this_row, db_col) = _
        Sheets(name_s2).Cells(this_row, db_col) Then

        'highlight the cells that have the same value
        Application.Sheets(name_s1).Range("B" & this_row).Select
        Selection.Interior.ThemeColor = xlThemeColorAccent4
        Selection.Interior.TintAndShade = 0.799981688894314

        End If

Next this_row

End Sub

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 2013-08-07
    • 1970-01-01
    • 2016-01-21
    • 2019-02-12
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多