我想找到一种方法来搜索单元格可能已被输入的数字。 所以B2的值是12.当使用Instr时,它会发现1,2和12.有没有办法让它只返回12?这里是我目前使用它的代码,如果我只使用1-9会很好,但是我需要更多选项。Instr函数,找到整个数字,所以12将不会返回为1
Sub PRTLookup()
Dim WsP As Worksheet
Dim PRTval As String
Dim MonCt As Long, DayCT As Long
Set WsP = ThisWorkbook.Sheets("PT")
For MonCt = 2 To 46 Step 4
For DayCT = 2 To 32
CelVal = Cells(MonCt, DayCT)
If IsNumeric(CelVal) Then
For x = 1 To 26
If InStr(Cells(MonCt, DayCT).Text, x) Then PRTval = PRTval + WsP.Cells(x, 1).Value
Next
Cells(MonCt, DayCT).Value = PRTval
PRTval = Empty
End If
Next
Next
End Sub
这是为我工作的最终代码:
Dim myarr() as string
Dim num as Variant
For MonCt = 2 To 46 Step 4
For DayCT = 2 To 32
Celval = Cells(MonCt, DayCT)
myarr = Split(Celval, ",")
For Each num In myarr
PRTval = PRTval & " " & WsP.Cells(num, 1).Text
Next num
Cells(MonCt, DayCT).Value = PRTval
PRTval = Empty
Next
Next
难道你的细胞拥有多个号码?如何格式化? – trincot
“CelVal”的价值在哪里? – trincot
@Trincot,单元格值可以有多个数字。数字将被输入像1,4,6,13等,这取决于他们从WsP工作表中得到什么。 CelVal没有被任何原因复制到代码中,我编辑它来添加它。 – cparsons