Excel 不匹配的类型错误,包括列(“U2:U”)和单元格(,)。地址
我目前正在尝试创建一个程序,查找“可靠性失败”标题,并检查此标题下的数据是否>0。如果大于0,它将向右偏移(0,3),并将使用left函数获得的值放入。由于我仍然不太熟悉VBA语法,我将面临不匹配类型错误,如下所示。有人知道怎么解决吗Excel 不匹配的类型错误,包括列(“U2:U”)和单元格(,)。地址,excel,vba,Excel,Vba,我目前正在尝试创建一个程序,查找“可靠性失败”标题,并检查此标题下的数据是否>0。如果大于0,它将向右偏移(0,3),并将使用left函数获得的值放入。由于我仍然不太熟悉VBA语法,我将面临不匹配类型错误,如下所示。有人知道怎么解决吗 Dim arr As Variant With Application Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets(1) Dim z As Long Dim lastro
Dim arr As Variant
With Application
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets(1)
Dim z As Long
Dim lastrow2 As Long
Dim result As Long
lastrow2 = ws1.Columns("U2:U").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row '--->**mistmatch error here**
arr = .Transpose(ws1.Range("T1:W1").Value)
For p = 2 To lastrow2
If .Count(.Match(Array("Reliability Fail"), arr, 0)) = 1 And ws1.Cells(p, "U") > 1 Then
result = ws1.Cells(p, "U").Address(False, False) '---->**mismatch error here**
ws1.Cells(Range("result").Row, Range("result").Column).Offset(0, 3) = Left(output, Len(output) - 1)
End If
Next
End With
更新的更改:
Dim arr As Variant
With Application
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets(1)
Dim z As Long
Dim lastrow2 As Long
Dim resultRng As Range
Set resultRng = ws1.Cells(p, "U")
lastrow2 = ws1.Columns("U2:U & ws1.Rows.Count").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
arr = .Transpose(ws1.Range("T1:W1").Value)
For p = 2 To lastrow2
If ws1.Range("U1") = "Reliability Fail"And resultRng > 1 Then
ws1.Cells(p, "U").Offset(0,3).Value = Left(output, Len(output) - 1)
End If
Next
End With
“U2:U”
不是有效的范围(或列)引用。你需要一个行号。并使用Range
而不是Columns
:ws1.Range(“U2:U”&ws1.Rows.Count)
。虽然我真的认为您只需要ws1.Columns(“U:U”)
在这里范围.地址
返回一个字符串
,例如$a$1
,而不是长
result = resultRng.Address(False, False)
resultRng.Offset(0,3) = Left(output, Len(output) - 1)
到
感谢@Scott Craner将这些更改合并到以下修订代码中:
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets(1)
If ws1.Range("U1") = "Reliability Fail" Then
Dim lastrow2 As Long
lastrow2 = ws1.Range("U2:U" & ws1.Rows.Count).Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For p = 2 To lastrow2
If ws1.Cells(p, "U").Value > 1 Then
ws1.Cells(p, "U").Offset(0, 3).Value = Left(output, Len(output) - 1)
End If
Next
End If
“U2:U”
不是有效的范围(或列)引用。你需要一个行号。并使用Range
而不是Columns
:ws1.Range(“U2:U”&ws1.Rows.Count)
。虽然我真的认为您只需要ws1.Columns(“U:U”)
在这里范围.地址
返回一个字符串
,例如$a$1
,而不是长
result = resultRng.Address(False, False)
resultRng.Offset(0,3) = Left(output, Len(output) - 1)
到
感谢@Scott Craner将这些更改合并到以下修订代码中:
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets(1)
If ws1.Range("U1") = "Reliability Fail" Then
Dim lastrow2 As Long
lastrow2 = ws1.Range("U2:U" & ws1.Rows.Count).Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For p = 2 To lastrow2
If ws1.Cells(p, "U").Value > 1 Then
ws1.Cells(p, "U").Offset(0, 3).Value = Left(output, Len(output) - 1)
End If
Next
End If
U2:U
不是有效的范围引用。@BigBen为什么会这样?我在这里的目的是选择从第二行开始的整个列U…也是范围。Address
返回一个字符串,而不是长的“U2:U”和ws1.Rows.Count
。您需要一个行号。您还使用.Count(.Match(Array(“Reliability Fail”),arr,0))=1
来测试标题是否在那里,但始终引用列U。为什么不只ws1.Range(“U1”)=“Reliability Fail”
?U2:U
不是有效的范围引用。@BigBen为什么?我在这里的目的是选择从第二行开始的整个列U…也是范围。Address
返回一个字符串,而不是长的“U2:U”和ws1.Rows.Count
。您需要一个行号。您还使用.Count(.Match(Array(“Reliability Fail”),arr,0))=1
来测试标题是否存在,但始终参考列U。为什么不直接使用ws1.Range(“U1”)=“Reliability Fail”
?@ScottCraner但我在最后一行得到了应用程序定义或对象定义的错误tho..@cena-将列更改为范围@cena您是否尝试了BigBen的更新代码,他们纠正了我的错误。@BigBen啊,我的错,我没看到。@ScottCraner很抱歉,我刚才错过了now@ScottCraner但是我仍然在最后一行得到应用程序定义或对象定义的错误tho..@cena-将列更改为范围@cena您是否尝试了BigBen更新的代码,他们纠正了我的错误。@BigBen啊,真糟糕,我没看到。@ScottCraner很抱歉我刚才错过了