首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于每个循环,都不按预期操作。

对于每个循环,都不按预期操作。
EN

Stack Overflow用户
提问于 2019-05-27 09:23:27
回答 1查看 70关注 0票数 0

我在excel中有两张单张,一张是带有数个单元格的板,另一张是引用(在前面的板中有数字),我需要在单元格所在的引用中写同一行。

第一块板的图片,参考资料在哪里?

excel工作表的图像,我必须写出每个引用的位置。

我的vba代码

示例:

and 8 and是板,local.png是我编写单元格去定位的地方。

代码语言:javascript
复制
Option Explicit

Sub ciclo()

    Dim FindString As String
    Dim Rng As Range
    Dim matrixVal As Range

    Set matrixVal = Sheets("Localizações").Range("B1")
    FindString = matrixVal

    For Each Rng In matrixVal

        If Trim(FindString) <> "" Then

            With Sheets("Arm8").Range("A1:J10")

                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)

                If Not Rng Is Nothing Then
                    'Application.Goto Rng, False
                    'MsgBox Rng.Column & " - " & Rng.Row
                Else
                    MsgBox "Nothing found"
                End If

            End With

            With Sheets("Localizações")
                .Range("C1:C9").Value = Rng.Column
                .Range("D1:D9").Value = Rng.Row
            End With

        End If

    Next Rng

End Sub

我期望local.png中的输出是列C和D

2-9

2-7

2-8

2-4

5-4

7-4

5-9

9-7

9-0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-27 10:57:35

第一,正如我在评论中所说:

代码语言:javascript
复制
Set matrixVal = Sheets("Localizações").Range("B1")

matrixVal设置为一个单元格(准确地说,是B1),因此您的For-Each循环除了这个单元格之外没有任何单元格要循环,所以它只能运行一次。

其次,需要在循环中更新FindString,否则您将一次又一次地搜索相同的值。

最后,您不应该更新循环中的Rng变量,因为您已经在使用它来遍历范围。您需要Range类型的第二个变量。

您的代码应该如下所示:

代码语言:javascript
复制
 Sub ciclo()

    Dim FindString As String
    Dim Rng As Range
    Dim cell As Range
    Dim matrixVal As Range

    Set matrixVal = ThisWorkbook.Worksheets("Localizacoes").Range("B1:B9")

    For Each cell In matrixVal
        FindString = cell.Value

        If Trim(FindString) <> "" Then

            With ThisWorkbook.Worksheets("Arm8").Range("A1:J10")

                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)

                If Not Rng Is Nothing Then
                    With ThisWorkbook.Worksheets("Localizacoes")
                        .Cells(cell.Row, "C").Value = Rng.Column
                        .Cells(cell.Row, "D").Value = Rng.Row
                    End With
                Else
                    MsgBox "Nothing found"
                End If

            End With

        End If

    Next cell

End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56323174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档