VBA范围和string混淆

认为我有一个范围和string之间的差异的问题。 试图让它search任何获胜者的范围内的单元格,然后吐出一个点值。

Sub it() Dim Round1 As Range Dim Round2 As Range Dim lngCnt As Long Dim Winner() As String Dim pointSum As Double ReDim Winner(1 To 4) Winner(1) = "Mohler" Winner(2) = "Scotter" Winner(3) = "DKGAY" Winner(4) = "Lassie" Set Round1 = Range("L3,L11,L22,L32").Text For Each Winner In Round1 lngCnt = lngCnt + 10 Winner(lngCnt) = pointSum.Value Next MsgBox pointSum.Value End Sub 

你将需要两个循环:

 Sub it() Dim Round1 As Range Dim Round2 As Range Dim lngCnt As Long Dim Winner() As String Dim pointSum As Double Dim rng As Range Dim i As Long ReDim Winner(1 To 4) Winner(1) = "Mohler" Winner(2) = "Scotter" Winner(3) = "DKGAY" Winner(4) = "Lassie" Set Round1 = Range("L3,L11,L22,L32") For Each rng In Round1 For i = 1 To UBound(Winner) If rng.Value = Winner(i) Then pointSum = pointSum + 10 Exit For End If Next i Next rng MsgBox pointSum.Value End Sub 

第一个循环遍历string数组的第二个范围。 当它find它相等的地方时,它增加10。

只有一个循环的解决scheme我不是通过我的电脑,所以不能testing或格式化代码标签

 Sub Main Dim Round1 As Range, rng As Range Dim Winners As Variant Dim pointSum As Double Winners =Array ("Mohler","Scotter","DKGAY", "Lassie") Set Round1 = Range("L3,L11,L22,L32") WinnersStr = "-" & Join (Winners, "-") & "-" For Each rng In Round1 If Instr(WinnersStr, "-" & rng.Value & "-") > 0 Then pointSum = pointSum + 10 Next rng End Sub