运行时错误 13 如果在另一个工作簿中找到值,则类型不匹配 VBA 以突出显示行
Runtime error 13 Type Mismatch VBA to highlight row if value is found in another workbook
我在 Excel 2013 年学习 VBA,上周末我发布了一个问题但没有收到回复。我一直在研究代码并将错误缩小到一个。如果在另一个打开的工作簿的 A 列中找到 A 列中的值,我正在尝试突出显示工作簿中的一行。
我收到运行时错误 13:类型不匹配错误。这就是它所说的全部,它是针对这行代码的:
If cell.Value = valuetofind Then
我在很多网站上都看过这个错误,但没有找到符合我的情况的网站。我认为它是 b/c 'valuetofind' 是一个范围,它试图将范围设置为等于一个值,如 '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
代码已更改,适用于任何需要帮助的人。
这是根据 Dinesh Takyar 关于在工作表之间复制数据的视频 (https://www.youtube.com/watch?v=AzhQ5KiNybk_) 修改的,尽管下面的代码是为了突出显示工作簿之间的行。工作簿、目标工作簿和源工作簿都需要打开。
我相信最初的 运行 Time 13 Error 是 b/c 标准,当它是一个 String 时,名为 'valuetofind' 的原始变量是 Dim as Range。下面代码中的变量现在称为 'myname' 并且是 Dim as 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
我在 Excel 2013 年学习 VBA,上周末我发布了一个问题但没有收到回复。我一直在研究代码并将错误缩小到一个。如果在另一个打开的工作簿的 A 列中找到 A 列中的值,我正在尝试突出显示工作簿中的一行。
我收到运行时错误 13:类型不匹配错误。这就是它所说的全部,它是针对这行代码的:
If cell.Value = valuetofind Then
我在很多网站上都看过这个错误,但没有找到符合我的情况的网站。我认为它是 b/c 'valuetofind' 是一个范围,它试图将范围设置为等于一个值,如 '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
代码已更改,适用于任何需要帮助的人。
这是根据 Dinesh Takyar 关于在工作表之间复制数据的视频 (https://www.youtube.com/watch?v=AzhQ5KiNybk_) 修改的,尽管下面的代码是为了突出显示工作簿之间的行。工作簿、目标工作簿和源工作簿都需要打开。
我相信最初的 运行 Time 13 Error 是 b/c 标准,当它是一个 String 时,名为 'valuetofind' 的原始变量是 Dim as Range。下面代码中的变量现在称为 'myname' 并且是 Dim as 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