关于 excel:运行时错误 13 如果在另一个工作簿中找到值,则键入 Mismatch VBA 以突出显示行

Runtime error 13 Type Mismatch VBA to highlight row if value is found in another workbook

我正在学习 Excel 2013 中的 VBA,上周末我发布了一个问题,但没有收到回复。我一直在研究代码并将错误缩小到一个。如果在另一个打开的工作簿 A 列中找到 A 列中的值,我正在尝试突出显示工作簿中的一行。

我收到运行时错误 13:类型不匹配错误。这就是它所说的所有内容,并且适用于这行代码:

1
If cell.Value = valuetofind Then

我查看了许多有关此错误的网站,但没有发现任何与我的情况相符的网站。我认为它的 b/c 'valuetofind' 是一个范围,它试图设置一个等于一个值的范围,见 'cell.value'。我认为我的所有变量都已正确声明。

我尝试将其更改为以下,以便它们都是范围,但会产生相同的错误:

1
If cell = valuetofind Then...

谁能帮忙解决这个错误?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Sub HighlightRow()
'http://www.vbaexpress.com/forum/showthread.php?26162-Solved-Highlight-ROW-based-on-cell-value
'http://www.mrexcel.com/forum/excel-questions/827262-visual-basic-applications-vlookup-between-2-workbooks.html
     'test column just picks any column, I think, to test how far down the rows go to, I think you could choose any column
    Const TEST_COLUMN As String ="D" '<=== change to suit
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim LastRow As Long
    Dim cell As Range
    Dim valuetofind As Range

    Set ws1 = ThisWorkbook.Sheets(1) 'name will change each day
    Set ws2 = ActiveWorkbook.Sheets(1) 'name will change each day

    With ws1
        LastRow = Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    'LastRow is testing/finding out last row using TEST_COLUMN first before performs rest of macro
    End With

    Set valuetofind = ws2.Range("A2:A" & LastRow)
         'Range("A2:A" & LastRow) is the criteria row where it is looking for Break Down and PM/SM Call below
         'Resize(,7) will highlight the row however many columns you tell it to, in this case 7
         'cell.Offset(, -6) I think tells to go back 6 columns to column A and start the highlighting there
    With ws1
        For Each cell In Range("A2:A" & LastRow)
            If cell.Value = valuetofind Then
            'old, do not use: wb2.Worksheets(wb2SheetName).Range("A2:A" & LastRow)
                cell.Offset(, -6).Resize(, 7).Interior.ColorIndex = 39
            Else
                cell.EntireRow.Interior.ColorIndex = xlNone
            End If
        Next
    End With

End Sub


代码已更改,适用于任何需要帮助的人。

这是根据 Dinesh Takyar 的关于在工作表之间复制数据的视频 (https://www.youtube.com/watch?v=AzhQ5KiNybk_) 修改的,尽管下面的代码是为了突出显示工作簿之间的行。工作簿、目标工作簿和源工作簿都需要打开。

我相信原始的 Run Time 13 Error 是 b/c 标准,称为 \\'valuetofind\\' 的原始变量是 Dim as Range,当它是一个字符串时。下面代码中的变量现在称为\\'myname\\',并且是Dim as String。但我不相信上面的代码无论如何都会起作用,因为我需要 For/Next 来遍历我的标准列中的每个单元格。

感谢 Dinesh 和论坛上的人们。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Sub HighlightRowBtwWorkbook()


Dim wkbkDest As Workbook
Dim i As Long
Dim lastrowDest As Long
Dim lastcolDest As Long
Dim wkbkSource As Workbook
Dim j As Long
Dim lastrowSource As Long
Dim myname As String
Dim lastcolSource As Long

'Destination
Set wkbkDest = ThisWorkbook 'was Workbooks("Destination_VBAHighlight.xlsm") 'was ActiveWorkbook
lastrowDest = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
lastcolDest = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

For i = 2 To lastrowDest
myname = wkbkDest.ActiveSheet.Cells(i,"A").Value

'Source
Set wkbkSource = Workbooks("TESTVBA.xlsm")
wkbkSource.Activate
lastrowSource = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
lastcolSource = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

For j = 2 To lastrowSource
    If ActiveSheet.Cells(j,"A").Value = myname Then
        'Activate Destination
        wkbkDest.Sheets(1).Activate
        ActiveSheet.Range(Cells(i,"B"), Cells(i, lastcolDest)).Interior.Color = RGB(252, 228, 214)
    End If
Next j

Next i

'select cell A1 in Destination wkbk to end there
wkbkDest.Sheets(1).Activate
wkbkDest.ActiveSheet.Range("A1").Select


End Sub