Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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/8/visual-studio-code/3.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
使用VBA函数创建自动填充宏时出现问题_Vba_Excel - Fatal编程技术网

使用VBA函数创建自动填充宏时出现问题

使用VBA函数创建自动填充宏时出现问题,vba,excel,Vba,Excel,我在创建将自动填充名为FindMyOrderNumber的VBA函数的宏时遇到问题。每次运行宏自动填充FindMyOrderNumber时,只填充列中的第一个单元格 此函数将在列A A1中查找订单号,并返回可在B B1中找到的工作表的名称 Option Explicit Function FindMyOrderNumber(strOrder As String) As String Dim ws As Worksheet Dim rng As Range For Ea

我在创建将自动填充名为FindMyOrderNumber的VBA函数的宏时遇到问题。每次运行宏自动填充FindMyOrderNumber时,只填充列中的第一个单元格

此函数将在列A A1中查找订单号,并返回可在B B1中找到的工作表的名称

Option Explicit
Function FindMyOrderNumber(strOrder As String) As String

    Dim ws As Worksheet
    Dim rng As Range

    For Each ws In Worksheets
        If ws.CodeName <> "Sheet3" Then
            Set rng = Nothing
            On Error Resume Next
                Set rng = ws.Cells.Find(What:=strOrder, LookAt:=xlWhole)
            On Error GoTo 0
            If Not rng Is Nothing Then
                FindMyOrderNumber = ws.Name
                Exit For
            End If
        End If
    Next

    Set rng = Nothing
    Set ws = Nothing

End Function
运行此宏后,仅填充B1

抱歉,如果已经讨论过这个问题,我是新的,我尝试过其他问题,但我无法将其应用于我的问题


请帮助

将application.volatile添加到函数中,这样它将随着工作表的更改而计算

Function FindMyOrderNumber(strOrder As String) As String

    Dim ws As Worksheet
    Dim rng As Range
    Application.Volatile
    For Each ws In Worksheets
        If ws.CodeName <> "Sheet3" Then
            Set rng = Nothing
            On Error Resume Next
            Set rng = ws.Cells.Find(What:=strOrder, LookAt:=xlWhole)
            On Error GoTo 0
            If Not rng Is Nothing Then
                FindMyOrderNumber = ws.Name
                Exit For
            End If
        End If
    Next

    Set rng = Nothing
    Set ws = Nothing

End Function

RangeB1:B68==FindYorderNumberRC[-1]试试看。@DaveXcel听起来像是我的答案,如果你能解释一下为什么他们当前的代码不起作用,这将是一个很好的答案。我现在遇到的问题是,A列会随着我使用RangeB:B==FindYorderNumberRC[-1]的时间的推移而变大-覆盖我的基础,但我的Excel会崩溃。你太棒了!非常感谢你。
Function FindMyOrderNumber(strOrder As String) As String

    Dim ws As Worksheet
    Dim rng As Range
    Application.Volatile
    For Each ws In Worksheets
        If ws.CodeName <> "Sheet3" Then
            Set rng = Nothing
            On Error Resume Next
            Set rng = ws.Cells.Find(What:=strOrder, LookAt:=xlWhole)
            On Error GoTo 0
            If Not rng Is Nothing Then
                FindMyOrderNumber = ws.Name
                Exit For
            End If
        End If
    Next

    Set rng = Nothing
    Set ws = Nothing

End Function
Sub Button1_Click()
    Dim Rws As Long, Rng As Range
    Rws = Cells(Rows.Count, "A").End(xlUp).Row
    Set Rng = Range(Cells(1, 2), Cells(Rws, 2))
    Rng = "=FindMyOrderNumber(RC[-1])"
End Sub