在索引单元格属性中引用Excel VBA中的string值

VBA新手这里有一个基本的问题,我一直在努力寻找答案:

我试图在VBA中查找一个string(或单词)的循环,如果find这个单词,一个指定的字母必须出现在它旁边列的单元格中。 当我使用一个特定的单元格引用(例如Cells(72,10))时,这个工作正常,但是当我尝试使用一个索引(例如Cells(i,10))来循环它时,它给出了错误13-types的不匹配。

我正在查找的“string”是从Excel中作为基本计算函数写入的另一个macros的输出(“峰”或“谷”)。 错误是由于尝试引用不同于“单元格”function可识别的数据types的结果吗? 还是有更好的方式来运行我的循环? 以下是macros和我的循环。

Function FTrough(tmin2, tmin1, t, tplus1, tplus2) If t < tmin2 And t < tmin1 And t < tplus1 And t < tplus2 Then FTrough = "Trough" Else FTrough = "" End If End Function 

和循环“:

 Sub Lookup() Dim i As Integer Dim j As Integer Dim c As Integer c = ActiveWorkbook.Worksheets.Count For i = 1 To c For j = 2 To 141 If Worksheets(i).Cells(j, 10) = "Trough" Then Worksheets(i).Cells(j, 12) = "T" End If If Worksheets("Austria").Cells(j, 11) = "Peak" Then Worksheets("Austria").Cells(j, 12) = "P" End If Next j Next i End Sub 

任何帮助赞赏

我不能评论,因为我的知名度低于50,所以这是我的答案。

基本上你的代码完美地工作。 我用Excel 2013 / VB6testing它如下:

 Sub Lookup() Dim i As Integer Dim j As Integer Dim c As Integer c = ActiveWorkbook.Worksheets.Count For i = 1 To c For j = 2 To 141 If Worksheets(i).Cells(j, 1) = "Trough" Then Worksheets(i).Cells(j, 2) = "T" End If If Worksheets("Austria").Cells(j, 1) = "Peak" Then Worksheets("Austria").Cells(j, 2) = "P" End If Next j Next i End Sub 

在Excel中写入值的函数:

 Public Function X(tmin2, tmin1, t, tplus1, tplus2) If t < tmin2 And t < tmin1 And t < tplus1 And t < tplus2 Then X = "Trough" Else X = "" End If End Function 

在Excel中的值是由自定义以及例如=X(100;100;1;100;100)