引用未明确定义的单元格中的数据

时间:2016-07-13 13:12:08

标签: vba excel-vba excel

我意识到这个问题的措辞可能有点奇怪,但我对编码比较陌生,所以我不完全确定如何将我的问题写成文字。我想在一张纸上迭代一列数据,并使用该列中的每个单元格作为我在不同工作表的给定行中搜索的搜索词。

Sub Test1()
n = 0
' n is the counter for the number of times that the search term will appear

Dim sn As String
For Each a In Worksheets("Sheet1").range("A5:A34").Value
f = a.Cells
sn = range(a).Value
' each sn is equal to the contents of the cell "a", which will be the serial number of the tooling


For Each c In Worksheets("RunsheetTest").range("A3:R3")
' Defines c as a range with cells A3 to R3?


If InStr(c.Cells, sn) > 0 Then
n = n + 1
Else
n = n
End If
' If the serial number is found in one of the cells, add 1 to n and move on to the next.

Next c

MsgBox "Searching for " & sn
range(a).Value = n
Next a
' I'm also getting an error here, saying "for loop not initialized", however I thought I initialized it above.

End Sub

我最大的问题是我是否在顶部使用了“f = a.Cells”的正确语法,因为我想引用循环当前所在的单元而不是特定单元格,以及为什么编译器是给我一个错误的循环。我不能在VBA中运行嵌套循环吗?另外,如果我的任何评论并不完全准确,请随时告诉我。

提前致谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

Sub Test1()
    Dim n As Integer
    Dim a As Range
    Dim c As Range
    Dim sn As String

    n = 0
    ' n is the counter for the number of times that the search term will appear

    For Each a In Worksheets("Sheet1").Range("A5:A34")
        'f = a.Cells
        sn = a.Value
        ' each sn is equal to the contents of the cell "a", which will be the serial number of the tooling

        For Each c In Worksheets("RunsheetTest").Range("A3:R3")
            ' Defines c as a range with cells A3 to R3?
            If InStr(1, c.Value, sn, vbTextCompare) > 0 Then
                n = n + 1
            Else
                n = n
            End If
            ' If the serial number is found in one of the cells, add 1 to n and move on to the next.
        Next

        MsgBox "Searching for " & sn
        a.Value = n
    Next
    ' I'm also getting an error here, saying "for loop not initialized", however I thought I initialized it above.

End Sub

ac是范围,因此您不能通过range(a)range(c)引用它们。另外,只需使用Next,而不是Next a

答案 1 :(得分:0)

另一个问题 - VBA确实支持嵌套循环,但最好使用内置的Range.Find()方法。请参阅下面的示例和我的评论:

Sub Test1()

    Dim n As Long
    n = 0 ' n is the counter for the number of times that the search term will appear

    Dim sn As String
    Dim c As Range
    Dim a As Range

    '' removing ".Value" at the end -- you need to loop through a range
    For Each a In Worksheets("Sheet1").Range("A5:A34")
        '' here "a" is already a range object that contains one cell so all you need is:
        n = 0 '' reset the counter for new serial number
        sn = a

        '' to search for a value it is better to use special .Find function on a range, not a loop
        With Worksheets("RunsheetTest").Range("A3:R3")

            Set c = .Find(sn, LookIn:=xlValues)
            If Not c Is Nothing Then
                '' you get here only if something is found, otherwise counter will be 0 -- i.e. nothing found
                firstAddress = c.Address
                Do
                    n = n + 1
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> firstAddress
            End If

        End With

        MsgBox "Searching for " & sn & " and found it " & n & " times."

    Next a

End Sub