【发布时间】:2015-07-29 16:16:55
【问题描述】:
我创建了类似于 vlookup 但具有拆分值的宏。我想从第二张拆分值(用分号分隔)中查找值,然后将描述复制并粘贴到新表中。
第一个循环遍历工作表 2 中的列表并设置变量中的值,第二个循环通过拆分值检查是否存在完全匹配并将描述复制并粘贴到第二个工作表。
但是 - 它不起作用,我不知道问题是什么。
我有通知"type mismatch"。
我尝试使用部分文本字符串进行 vlookup,但它也不起作用。
Sub Metadane()
Dim ws As Worksheet
Dim aCell As Range, rng As Range
Dim Lrow As Long, i As Long
Dim myAr
Dim ws2 As Worksheet
Dim bCell As Range, rng2 As Range
Dim variable As String
'~~> Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find the last row in Col A
Lrow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rng = .Range("A1:A" & Lrow)
Set ws2 = ThisWorkbook.Sheets("Sheet2")
With ws2
'~~> Find the last row in Col A
Lrow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Set your range
Set rng2 = .Range("A1:A" & Lrow)
'~~> Loop trhough your range
For Each bCell In rng2
If Len(Trim(bCell.Value)) <> 0 Then
variable = bCell.Value
For Each aCell In rng
'~~> Skip the row if value in cell A is blank
If Len(Trim(aCell.Value)) <> 0 Then
'~~> Check if the cell has ";"
'~~> If it has ";" then loop through values
If InStr(1, aCell.Value, ";") Then
myAr = Split(aCell.Value, ";")
For i = LBound(myAr) To UBound(myAr)
If myAr = variable Then
Worksheets("sheet2").bCell(, 2).PasteSpecial xlPasteValues
Next i
Else
Worksheets("sheet2").bCell(, 2).PasteSpecial xlPasteValues
End If
End If
Next
End If
Next
End With
End Sub
我更改了我的代码,但它仍然不能正常工作,我有一个结果:
【问题讨论】:
-
If myAr = variable Then你应该为这个声明添加End If。 -
谢谢。这很有帮助,但我仍然有问题。现在“运行时错误'13'。类型不匹配”
-
您正在将字符串与数组进行比较。
myAr = variable。我相信这是你的错误?您需要将其更改为myAr(i) = variable。