Vba 如何循环浏览所有图纸的选定部分?

Vba 如何循环浏览所有图纸的选定部分?,vba,excel,loops,Vba,Excel,Loops,我有一些Excel表格,其中包含以下数据: n=number of worksheets for each worksheet (2 to n) count=0 for each row i in column 2 for each row j in sheet1.column2 while(j not empty) if(i == j)

我有一些Excel表格,其中包含以下数据:

    n=number of worksheets
    for each worksheet (2 to n) 
    count=0
      for each row i in column 2
            for each row j in sheet1.column2
                while(j not empty)
                  if(i == j)
                  count++
                endwhile
                output.sheet1.column4(count)
            endfor
      endfor
    endfor 

所有工作表的格式相同一张工作表可以有多个。我想搜索每张工作表中的每组数据。我在第一张工作表的第3列有一个数据列表和一个命令按钮

我想要实现的是,当我单击命令按钮时,它会在每个工作表的每组中循环,并找到找到多少个匹配项。 然后在第一张纸上给我看结果

我想根据第一页中的数据列表搜索每个组

我想知道[C:3,C:5]列中有多少东西(大小是可变的)存在于每张纸的每个包中

那么,如何在一个包结束和下一个包开始的地方断开

当我到达工作表的末尾时,如何停止

我是VBA的新手,因此我知道我需要做如下工作:

    n=number of worksheets
    for each worksheet (2 to n) 
    count=0
      for each row i in column 2
            for each row j in sheet1.column2
                while(j not empty)
                  if(i == j)
                  count++
                endwhile
                output.sheet1.column4(count)
            endfor
      endfor
    endfor 
如何在VBA中执行此操作

    Private Sub CommandButton1_Click()
    Dim ws As Worksheet, myCounter
    Dim erow, myCounter As Long

    myCounter = 1
    For Each ws In Sheets
        For i = 1 To Rows.Count
            if cells(i,3) = ___                
    Next ws

    End Sub
(1) 那么,如何在一个包结束和下一个包开始的地方断开呢?
(2) 当我到达工作表的末尾时,如何停止

如何在VBA中执行此操作?
像这样:

Private子命令按钮1\u单击()
我长,j长,最后一排长
“使用除第一张之外的每张纸
对于i=2到1张。计数
'获取最后一行/最后一个条目
lastrow=工作表(i).单元格(Rows.Count,1).结束(xlUp).行'(2)
'从第二行循环到最后一行
对于j=2到最后一行
'这是您的代码,如表(i)。单元格(j,x)(x是列)
'如果len(表(i).单元格(j,1))和len(表(i).单元格(j,1))=0,则
以上是查找Excel工作表中最后一行的最可靠方法。使用此选项后,任务的一半完成

要检查新组的开始,您可以检查当前行是否包含a列中的“Bundle/group”。检查下面的示例代码

Function test()
    Dim sh As Worksheet
    Dim lastRow As Long
    lastRow = 1
    Dim i As Long
    Dim groupSearchText As String
    groupSearchText = "Bundle/Group"

    For Each sh In Worksheets
        If sh.Index <> 1 Then
            lastRow = sh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            i = 1
            For i = 1 To lastRow
                Dim aValue As String
                Dim cValue As String
                aValue = sh.Cells(i, 1)
                cValue = sh.Cells(i, 3)

                If Left(Trim(aValue), Len(groupSearchText)) = groupSearhText Then
                    ''''group code here, do something with cValue
                End If
            Next i
        End If
    Next sh

End Function
功能测试()
将sh设置为工作表
最后一排一样长
lastRow=1
我想我会坚持多久
Dim groupSearchText作为字符串
groupSearchText=“捆绑/组”
对于工作表中的每个sh
如果是索引1,则
lastRow=sh.Cells.Find(“*”,SearchOrder:=xlByRows,SearchDirection:=xlPrevious)。行
i=1
对于i=1到最后一行
作为字符串的模糊aValue
将C值设置为字符串
aValue=sh.Cells(i,1)
cValue=sh.Cells(i,3)
如果左(Trim(aValue),Len(groupSearchText))=groupSearchText,则
''这里是组码,用cValue做点什么
如果结束
接下来我
如果结束
下一个sh
端函数

< A7】为什么A7在空白处?我猜[A8:A10]应该是在[A7:A9]比赛的什么?您的图片未显示所需的结果。请立即添加详细信息!我想知道[C:3,C:5]列中有多少东西(大小是可变的)存在于每张图纸的每个捆绑包中OP会如何处理这些代码?为什么
i=1
?为什么
lastRow=1
?最后一行应该是
长的
,在第33000行中输入一些内容,看看运行代码时会发生什么。没错,它们应该是长的,编辑我的答案。我只是有一个初始化变量的习惯,从C时代开始。在这里,它被证明是正确的,因为初始值是0,在修改10秒之后,我最终可能会错过它,并将其作为0传递给单元格或行。
Function test()
    Dim sh As Worksheet
    Dim lastRow As Long
    lastRow = 1
    Dim i As Long
    Dim groupSearchText As String
    groupSearchText = "Bundle/Group"

    For Each sh In Worksheets
        If sh.Index <> 1 Then
            lastRow = sh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            i = 1
            For i = 1 To lastRow
                Dim aValue As String
                Dim cValue As String
                aValue = sh.Cells(i, 1)
                cValue = sh.Cells(i, 3)

                If Left(Trim(aValue), Len(groupSearchText)) = groupSearhText Then
                    ''''group code here, do something with cValue
                End If
            Next i
        End If
    Next sh

End Function