types与WorksheetFunction嵌套循环中不匹配

Sub checkMe() Dim lastRow As Long Dim cell As Range lastRow = Range("A" & Rows.Count).End(xlUp).Row For Each cell In Range("B1:B" & lastRow) 'dynamically go through each row in the range For a_counter = 2001 To 2015 ' for each row, cycle through each year until 2015 j = a_counter b = cell.offset(0, 6).Value 'value for vlookup a = Application.VLookup(b, Sheet2.Range("A2:N42"), (j - 2000 + 2), False) ' look up each value in offset(0,6) in another sheet If cell.Value = "DUB" And cell.offset(0, 1).Value <= "2001" And cell.offset(0, 2).Value = "OSLR" Then cell.offset(0, j + 12 - 2000).Value = cell.offset(0, 7).Value * a 'only apply when the conditions are met Next a_counter Next End Sub 

顺序应该改变了一点,所以条件是在内部循环之前,以帮助速度,否则我认为这应该工作。 cell.offset(0,6)的值是我需要查找每行的查找的值。 这个代码工作,如果我把一个值'的地方是,但是我需要的价值来查找每行更改。 目前它只适用于第一行并停止。 现在我得到一个不匹配的错误。

我们来做一些math。

 For a_counter = 2001 To 2015 

a_counter将按顺序排列15个值; 例如2001►2015(含)。

 Sheet2.Range("A2:N42") 

共有14栏; 例如列A►列N(含)。

 j = a_counter ..., (j - 2000 + 2), ... 

您正在要求从3►17的15个不同栏目中获得回报。

然而,A:N有14列,所以当a_counter = 2013时会出错(types不匹配),因为没有 15列。