我想找到一種方法來搜索單元格可能已被輸入的數字。 所以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