2015-10-05 120 views
0

我有一個數據庫,其中包含一組從1到100的數字。我希望VBA能夠在列A中運行此列表,並且在A爲20到40之間的區域中,在列B上鍵入McFly。讓VBA循環訪問一個數組

我給出的代碼中存在不匹配錯誤13, 「如果列表> =」

Dim list As Range 
Dim list_readthru As Range 
Set list = Range("A2", Range("A50").End(xlUp)) 
For Each list_readthru In list 
If list >= 20 And list <=40 Then Range("B:B") = "McFly" 
Next list_readthru 
End Sub 

我有問題,確定我做錯了什麼。

+2

我想你想要'list.value'但'range(「B:B」)'需要更多的工作...... – findwindow

回答

2

如果你想要一個數組,你可以有一個數組:

Sub MM() 

Dim list, i 

list = WorksheetFunction.Transpose(Range("A2", [A50].End(xlUp))) 

For i = 1 To UBound(list) 
    If list(i) >= 20 And list(i) <= 40 Then Cells(i + 1, 2).Value = "McFly" 
Next 

End Sub 

不過,我看不出這是隻是把公式中的列有什麼不同B:

With Range("B2", [A50].End(xlUp)) 
    .FormulaR1C1 = "=IF(AND(RC[-1]>=20,RC[-1]<=40),""McFly"","""""")" 
    .Value = .Value 
End With 
1

您將list作爲Range對象(不是數組)來調暗,因此請將其作爲範圍使用。例如,list.valuelist_readthru也是如此。

1

你在這裏混淆了一些東西。試試這個:

Dim list As Range 
Dim list_readthru As Range 
Set list = Range("A2", Range("A50").End(xlUp)) 
For Each list_readthru In list 
    If list_readthru >= 20 And list_readthru <=40 Then 
     Cells(list_readthru.row, 2) = "McFly" 
    End If 
Next list_readthru 
End Sub