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
Excel 带IF语句的VBA Double FOR循环_Excel_Vba_For Loop - Fatal编程技术网

Excel 带IF语句的VBA Double FOR循环

Excel 带IF语句的VBA Double FOR循环,excel,vba,for-loop,Excel,Vba,For Loop,我需要一个VBA脚本,将执行双FOR循环。第一个FOR循环是需要在多个工作表上执行一些命令的循环(第一个工作表是主工作表,需要跳过!!) 第二个for循环需要比较多行上的值。到目前为止,我已经粘贴了我的代码 Public Sub LoopOverSheets() device = Cells(6, 1) 'This value is whatever the user chooses from a drop-down menu Dim mySheet As Worksheet 'Creatin

我需要一个VBA脚本,将执行双FOR循环。第一个FOR循环是需要在多个工作表上执行一些命令的循环(第一个工作表是主工作表,需要跳过!!)

第二个for循环需要比较多行上的值。到目前为止,我已经粘贴了我的代码


Public Sub LoopOverSheets()
device = Cells(6, 1) 'This value is whatever the user chooses from a drop-down menu
Dim mySheet As Worksheet 'Creating variable for worksheet
orow = 8 'setting the starting output row

For Each mySheet In ThisWorkbook.Sheets 'this is the first FOR loop, to loop through ALL the worksheets
tabName = ActiveSheet.Name    'this is a variable that holds the name of the active sheet

    For irow = 2 To 10 'This line of code starts the SECOND FOR loop.
        If (Range("a" & irow)) = device Then 'This line of code compares values
            orow = orow + 1
            Range("'SUMMARY'!a" & orow) = device 'This line of code pastes the value of device variable
            Range("'SUMMARY'!b" & orow) = tabName 'This line of code needs to paste the name of the current active sheet
            'Range("'SUMMARY'!c" & orow) = Range("'tabName'!b" & irow) 'this line of code needs to paste whatever value is in the other sheet's cell
            'Range("'SUMMARY'!d" & orow) = Range("'tabName'!c" & irow) 'same objective as the last line of code, different rows and columns
        End If
    Next irow 'This line of code will iterate to the next orow. This is where I get an error (Compile Error : Next Without For)*******
Next mySheet 'This line of code will iterate to the next sheet

End Sub

当前代码运行,但它只输出第一个(主工作表)的结果。它需要跳过第一张工作表,并遍历其余的工作表。

Dim irow尽可能长
Dim irow As Long
Dim ws  As Worksheet
Dim mySheet As String
mySheet = "mainSheet" 'Insert in the mySheet variable the name of your main sheet
device = Cells(6, 1)
orow = 8
For Each ws In Application.ActiveWorkbook.Worksheets
    If ws.Name <> mySheet Then
        For irow = 2 To 10
            If ws.Range("A" & irow) = device Then
                orow = orow + 1
                Range("'SUMMARY'!a" & orow) = device
                Range("'SUMMARY'!b" & orow) = ws.Name
                'Range("'SUMMARY'!c" & orow) = Range("'tabName'!b" & irow) 'this line of code needs to paste whatever value is in the other sheet's cell
                'Range("'SUMMARY'!d" & orow) = Range("'tabName'!c" & irow) 'same objective as the last line of code, different rows and columns
            End If
        Next irow
    End If
Next ws
将ws设置为工作表 将我的纸张变暗为字符串 mySheet=“mainSheet”'在mySheet变量中插入主工作表的名称 设备=单元(6,1) orow=8 对于Application.ActiveWorkbook.Worksheets中的每个ws 如果是ws.Name mySheet,那么 对于irow=2到10 如果ws.Range(“A”&irow)=设备,则 orow=orow+1 范围(“'SUMMARY'!a”&orow)=设备 范围(“'SUMMARY'!b”&orow)=ws.Name '范围('SUMMARY'!c“&orow)=范围('tabName'!b”&irow)'这行代码需要粘贴其他工作表单元格中的任何值 '范围('SUMMARY'!d“&orow)=范围('tabName'!c”&irow)'与最后一行代码的目标相同,不同的行和列 如果结束 下一步 如果结束 下一个ws
更新摘要工作表 链接:

  • 以下内容未经测试
代码

Option Explicit

Sub loopOverSheets()
    
    Const tName As String = "Summary"
    Const tFirstRow  As Long = 8
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    
    Dim tgt As Worksheet
    Set tgt = wb.Worksheets(tName)
    Dim device As String
    device = tgt.Cells(6, 1).Value
    Dim tRow As Long
    tRow = tFirstRow
    
    Dim src As Worksheet
    Dim sName As String
    Dim i As Long
    For Each src In wb.Worksheets
        sName = src.Name
        If Not StrComp(sName, tName, vbTextCompare) = 0 Then
            For i = 2 To 10
                If StrComp(src.Range("A" & i), device, vbTextCompare) = 0 Then
                    tRow = tRow + 1
                    tgt.Range("A" & tRow).Value = device
                    tgt.Range("B" & tRow).Value = sName
                    tgt.Range("C" & tRow).Value = src.Range("B" & i).Value
                    tgt.Range("D" & tRow).Value = src.Range("C" & i).Value
                End If
            Next i
        End If
    Next src

    MsgBox "Data transferred.", vbInformation, "Success"
    
End Sub

正如您对上一个问题的评论,通常的做法不是在
范围内使用工作表名称,而是限定
工作表
工作表(“摘要”).Range(“A”&orow)
。或者按索引循环(
,对于i=2到此工作簿.Sheets.Count
),或者使用
If
跳过第一张工作表。代码中不清楚,但显然您希望
Range(“a”&irow)
来自
mySheet
,另一张
Range
来自名为Summary的工作表。由于从不使用显式工作表引用,因此不会发生这种情况。也不要在循环中使用工作表名称(
tabName
)。使用
mySheet
mySheet.Range(“B”和irow)
Range(“A”和irow)
需要使用所讨论的
工作表进行鉴定。非常正确!我相信它可以