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

时间:2016-10-29 23:43:39

标签: excel-vba runtime-error excel-2013 vba excel

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

我收到运行时错误13:输入不匹配错误。这就是它所说的全部代码:

If cell.Value = valuetofind Then

我已经在很多网站上查看了这个错误,但我看不出任何与我的情况相符的内容。我认为这是&b; c'重要的因素。是一个范围,它试图设置一个等于一个值的范围,在' cell.value'中可以看到。我认为我的所有变量都是正确的。

我已尝试将其更改为下方,以便它们都是范围,但会出现同样的错误:

If cell = valuetofind Then...

任何人都可以帮助解决此错误吗?

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

1 个答案:

答案 0 :(得分:1)

代码已被更改,并且正在为需要帮助的任何人工作。

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

我认为最初的运行时间13错误是b / c标准,当它是一个字符串时,名为'valuetofind'的原始变量是Dim as Range。下面代码中的变量现在称为“myname”,并且Dim为String。但我不相信上面的代码无论如何都不会有用b / c我需要For / Next来遍历我的标准列中的每个单元格。

感谢Dinesh和本论坛上的人们。

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