如果特定单元格等于string,则计数表

我试图计算每个单元格为哪个单元格A1 =“下一个”,然后将该数字作为variables存储。 然后设置当前表单元格A2等于该variables。 这是我有什么:

Dim ws As Worksheet i = 0 For Each ws In ActiveWorkbook.Worksheets If range("A1").value = "next" i = i + 1 End If Activesheet.Range("a2").Value = i Next ws 

我在这里错过了什么?

你需要告诉vba Range(“A1”)属于哪个表:

 If ws.range("A1").value = "next" Then 

所以:

 Dim ws As Worksheet Dim i As Long i = 0 For Each ws In ActiveWorkbook.Worksheets If ws.range("A1").value = "next" i = i + 1 End If Activesheet.Range("A2").Value = i Next ws