Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在userform文本框中循环一个值_Excel_Vba - Fatal编程技术网

Excel 在userform文本框中循环一个值

Excel 在userform文本框中循环一个值,excel,vba,Excel,Vba,我试图编写一个基本循环,在工作表的特定列中查找唯一的值。我相信我已经正确地声明了我的变量。但是,当我试图运行代码时,它会给我一个溢出错误。我只希望宏能够在数据集中循环,直到找到特定的ID号 下面是我方便查看的代码: Sub Macro1() Dim FirstRow As Range Dim LastRow As Range Dim R As Long FirstRow = Worksheets("Petrobras").Range("V2") La

我试图编写一个基本循环,在工作表的特定列中查找唯一的值。我相信我已经正确地声明了我的变量。但是,当我试图运行代码时,它会给我一个溢出错误。我只希望宏能够在数据集中循环,直到找到特定的ID号

下面是我方便查看的代码:

Sub Macro1()  
    Dim FirstRow As Range
    Dim LastRow As Range
    Dim R As Long

    FirstRow = Worksheets("Petrobras").Range("V2")
    LastRow = Worksheets("Petrobras").Cells(Rows.Count, 22).End(xlUp).Select
    R = TXTOPPNUM_Insert.Value

    For R = FirstRow To LastRow
        Worksheets("Petrobras").Cells(Rows.Count, 22).Find(R, , , Lookat:=xlWhole).Select
    Next R
End Sub
以下是可能有帮助的:

Sub Macro1()
Dim FirstRow As Long
Dim LastRow As Long
Dim R As Long
Dim tempStore As New collection ' collection is initialized with .Count = 0
Dim uniqueValue As Variant

' is that needed?
'R = TXTOPPNUM_Insert.Value

With Worksheets("Petrobras")

    FirstRow = .Cells(2, 22).row ' set first row
    LastRow = .Cells(Rows.Count, 22).End(xlUp).row ' set last row


    For R = FirstRow To LastRow ' there's an actual loop
        If Application.WorksheetFunction.CountIf(range(.Cells(FirstRow, 22), .Cells(LastRow, 22)), .Cells(R, 22).Value) = 1 Then ' don't think that I have to explain how does the CountIf works
            tempStore.Add .Cells(R, 22).Value ' if the CountIf of value = 1 then adding this value to a collection
        End If
    Next

    If tempStore.Count > 1 Then ' if collection contains more that 1 value - means that there is not one unique value
        MsgBox "There is more then 1 unique value"
    Else
        uniqueValue = tempStore(1) ' if collection contains 1 value - assign the unique value variable
    End If

End With


End Sub

此外,请仔细查看和。

为了保持代码的简单,您可以使用一行程序,只需单击用户表单上的按钮即可激活该程序;只需将这行代码添加到按钮单击宏。在文本框中键入要查找的值,然后单击按钮

ThisWorkbook.Sheets("Petrobras").Range("V2", Range("V" & Rows.Count).End(xlUp)).Find(What:=Me.TXTOPPNUM_Insert.Value).Select

不知道你为什么在这里循环。听起来你只需要
查找
。确实有些错误。您正在将
R
循环为
Long
数据类型,但您已将
FirstRow
设置为
范围
变量,设置不正确。这同样适用于
LastRow
。那么我想你最终会遇到更多的问题。我认为您首先不应该出现
溢出
错误,您的代码不会超过
FirstRow=..