Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.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/0/vba/16.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
Macos Excel VBA更新:查找数据、循环浏览多个工作表、复制范围_Macos_Vba_Excel - Fatal编程技术网

Macos Excel VBA更新:查找数据、循环浏览多个工作表、复制范围

Macos Excel VBA更新:查找数据、循环浏览多个工作表、复制范围,macos,vba,excel,Macos,Vba,Excel,从昨天开始更新此线程: (特别感谢findwindow让我走了这么远!) 我一直在某个部分上遇到运行时91错误,并最终放入一个If/Then语句以跳到下一页……但现在我在它正下方的行上遇到了一个错误1004(见下文): 昨天,错误91发生在: fRow=rng.行 今天,在我将If/Then部分放入该区域后,我在以下方面得到了错误1004(对象的方法“Range”\u工作表“failed”): 设置copyRng=ws.Range(单元格(fRow,1),单元格(fRow,18)) 语法正在运行

从昨天开始更新此线程:

(特别感谢findwindow让我走了这么远!)

我一直在某个部分上遇到运行时91错误,并最终放入一个If/Then语句以跳到下一页……但现在我在它正下方的行上遇到了一个错误1004(见下文):

昨天,错误91发生在:

fRow=rng.行

今天,在我将If/Then部分放入该区域后,我在以下方面得到了错误1004(对象的方法“Range”\u工作表“failed”):

设置copyRng=ws.Range(单元格(fRow,1),单元格(fRow,18))


语法正在运行,它似乎在正确的工作簿中查找,但我不确定它是否卡住了,因为我正在搜索的变量(变量A)没有出现在第一页上。有什么想法吗?

一个简短的说明-可能还有解决方案:

我看到您正在处理多个工作表-这很好,只需记住在设置范围时保持高度警惕

对于
设置copyRng
,您可以正确指定
ws.Range
,但对于
单元格()
,您也需要这样做。有两种修复方法,请使用此方法:
Set copyRng=ws.Range(ws.Cells(fRow,1),ws.Cells(fRow,18))

或者,将
一起使用(我个人的偏好):

With
的情况下,你会注意到你可以用一个小数点作为你的
With _u
的占位符。(我喜欢
With
,因为如果你的工作表变量很长,或者你只是使用了实际的名称,你必须在
thismyworksheet.Range(thismyworksheet.Cells(1,1)中重复这一点),这是工作表。单元格(…
可能会很长)

如果这不起作用,请告诉我。在给出
范围后,我忘记显式给出
单元格()
工作表,导致电子表格挂起

编辑:根据您的评论, 首先,如果你的
戒指不算什么
-如果rng不算什么,那么应该是
,我不喜欢“如果(真的)那么[隐式什么都不做]”

对于工作表循环,请尝试以下操作:

For Each ws In X.Worksheets
    With ws.Range("A:A")
        Set rng = .Find(What:=A, After:=ActiveCell, LookIn:=xlValues, _
                        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not rng Is Nothing Then
            fRow = rng.Row
            Set copyRng = ws.Range(ws.Cells(fRow, 1), ws.Cells(fRow, 18))
            Destination.Value = copyRng.Value
        End With
    Next ws

    Application.ScreenUpdating = True
End Sub

不确定这是不是你要找的? 如果缺少结尾,则有结尾?您可以在一行中复制。请参见下面

For Each ws In X.Worksheets
    With ws.Range("A:A")
        Set rng = .Find(What:=A, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        If rng Is Nothing Then 'do nothing
          Else
          fRow = rng.Row
           ws.Range("A" + CStr(fRow) + ":" + "R" + CStr(fRow)).Copy Destination:=Destination
        End If
    End With
Next ws

是的,Bruce知道了。很好的错误检查。我想我们越来越接近了。这确实解决了运行时错误。谢谢!但是,宏一直运行,没有将任何数据复制到目标区域,我无法判断它是否真的在变量X工作簿中的每个工作表中循环。变量X工作簿有宏,所以每当它打开了,我必须单击“启用宏”框,这会将其带到默认工作表…这有关系吗?它有效!我必须修复范围,因为它不是我所需要的100%,但我确切地知道如何做到这一点。谢谢你,以及其他线程中的BruceWayne和findwindow!很好!尽管,正如我在回答中提到的,我会如果rng为Nothing,则将
翻转为[implicit DO Nothing,END If]
如果rng不是Nothing,则翻转为fRow=…
。当然,除非有原因-我个人不喜欢
If
语句“挂起”“如果可以避免的话。这是一个公平的观点。我会在调整代码时牢记这一点。仅供参考,这对我来说是一个重大突破,因为它为我几个月来一直在研究的东西提供了概念证明。你们都是最好的!
For Each ws In X.Worksheets
    With ws.Range("A:A")
        Set rng = .Find(What:=A, After:=ActiveCell, LookIn:=xlValues, _
                        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not rng Is Nothing Then
            fRow = rng.Row
            Set copyRng = ws.Range(ws.Cells(fRow, 1), ws.Cells(fRow, 18))
            Destination.Value = copyRng.Value
        End With
    Next ws

    Application.ScreenUpdating = True
End Sub
For Each ws In X.Worksheets
    With ws.Range("A:A")
        Set rng = .Find(What:=A, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        If rng Is Nothing Then 'do nothing
          Else
          fRow = rng.Row
           ws.Range("A" + CStr(fRow) + ":" + "R" + CStr(fRow)).Copy Destination:=Destination
        End If
    End With
Next ws