Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
使用For/Each循环但跳转活动单元格VBA_Vba_Excel_For Loop - Fatal编程技术网

使用For/Each循环但跳转活动单元格VBA

使用For/Each循环但跳转活动单元格VBA,vba,excel,for-loop,Vba,Excel,For Loop,因此,我试图为每个循环编写一个表,以查看整行。如果找到“专业”一词,将其复制到下三个单元格。 这部分做得很好,但当它循环时,下一个单元格当然有“特殊性”,它只是复制了它。我需要弄清楚如何说,如果你找到了“专业”并复制了它,跳过4个单元格,然后再次开始搜索。。。。。尝试偏移活动单元格,但不起作用。 有什么想法吗? 谢谢 以下是如何在给定当前代码的情况下向后循环: Sub CopySpecialtyOver() Dim rngRow As Range Dim Cell As Ran

因此,我试图为每个循环编写一个表,以查看整行。如果找到“专业”一词,将其复制到下三个单元格。 这部分做得很好,但当它循环时,下一个单元格当然有“特殊性”,它只是复制了它。我需要弄清楚如何说,如果你找到了“专业”并复制了它,跳过4个单元格,然后再次开始搜索。。。。。尝试偏移活动单元格,但不起作用。 有什么想法吗? 谢谢


以下是如何在给定当前代码的情况下向后循环:

Sub CopySpecialtyOver()

    Dim rngRow As Range
    Dim Cell As Range
    Dim cIndex As Long

    Set rngRow = Range("A8:BA8")

    For cIndex = rngRow.Columns.Count To rngRow.Column Step -1
        Set Cell = Cells(rngRow.Row, cIndex)
        If InStr(1, Cell.Value, "Specialty", vbTextCompare) Then
            Cell.Offset(, 1).Resize(, 3).Value = Cell.Value
        End If
    Next cIndex

End Sub

以下是如何在给定当前代码的情况下向后循环:

Sub CopySpecialtyOver()

    Dim rngRow As Range
    Dim Cell As Range
    Dim cIndex As Long

    Set rngRow = Range("A8:BA8")

    For cIndex = rngRow.Columns.Count To rngRow.Column Step -1
        Set Cell = Cells(rngRow.Row, cIndex)
        If InStr(1, Cell.Value, "Specialty", vbTextCompare) Then
            Cell.Offset(, 1).Resize(, 3).Value = Cell.Value
        End If
    Next cIndex

End Sub

可以将“For each”替换为整数iterable:

Sub CopySpecialtyOver()
    Dim i As Integer
    Dim rngRow As Range
    Dim Cell As Range

    Set rngRow = Range("A8:BA8")

    For i = 1 To rngRow.Cells.Count
        Set Cell = rngRow(1, i)
        If InStr(1, Cell.Value, "Specialty") Then
            Cell.Offset(0, 1).Value = Cell.Value
            Cell.Offset(0, 2).Value = Cell.Value
            Cell.Offset(0, 3).Value = Cell.Value
            i = i + 3
        End If
    Next i
End Sub

可以将“For each”替换为整数iterable:

Sub CopySpecialtyOver()
    Dim i As Integer
    Dim rngRow As Range
    Dim Cell As Range

    Set rngRow = Range("A8:BA8")

    For i = 1 To rngRow.Cells.Count
        Set Cell = rngRow(1, i)
        If InStr(1, Cell.Value, "Specialty") Then
            Cell.Offset(0, 1).Value = Cell.Value
            Cell.Offset(0, 2).Value = Cell.Value
            Cell.Offset(0, 3).Value = Cell.Value
            i = i + 3
        End If
    Next i
End Sub

非常感谢你!我最终是这样解决的:

Sub CopySpecialtyOver()

Dim rngRow As Range
Dim Cell As Range

Set rngRow = Range("A8:BA8")

For Each Cell In rngRow
If InStr(1, Cell.Value, "Specialty") Then
    If InStr(1, Cell.Offset(0, -1).Value, "Specialty") Then
    Else
       Cell.Offset(0, 1).Value = Cell.Value
       Cell.Offset(0, 2).Value = Cell.Value
       Cell.Offset(0, 3).Value = Cell.Value
       End If
End If
Next Cell
End Sub

非常感谢你!我最终是这样解决的:

Sub CopySpecialtyOver()

Dim rngRow As Range
Dim Cell As Range

Set rngRow = Range("A8:BA8")

For Each Cell In rngRow
If InStr(1, Cell.Value, "Specialty") Then
    If InStr(1, Cell.Offset(0, -1).Value, "Specialty") Then
    Else
       Cell.Offset(0, 1).Value = Cell.Value
       Cell.Offset(0, 2).Value = Cell.Value
       Cell.Offset(0, 3).Value = Cell.Value
       End If
End If
Next Cell
End Sub

对于每个
——正如其他回复所指出的那样——可能不是最好的策略。然而,正如您所要求的,这里有一段代码,使用一些循环内控制来克服本用例中每个的
缺陷:

Sub CopySpecialtyOver()

Dim rngRow As Range
Dim Cell As Range
Dim Found As Boolean
Dim Cnt As Integer

Set rngRow = Range("A8:BA8")
Found = False
Cnt = 0

For Each Cell In rngRow.Cells

    If InStr(1, Cell.Value, "Specialty") And Not Found Then
        ' capture start of sequence - otherwise do nothing
        Found = True
        Cnt = 0
    Else

        If Found Then
            'if in Found mode increment counter
            Cnt = Cnt + 1

            ' expand using negative offset
            If Cnt <= 3 Then
                Cell = Cell.Offset(0, -Cnt).Value
            End If

            ' break after 3rd
            If Cnt = 3 Then
                Found = False
                Cnt = 0
            End If

        End If

    End If

Next Cell
End Sub
子副本特殊版本()
随着射程的增长而增长
暗淡单元格作为范围
Dim被发现为布尔值
作为整数的Dim Cnt
设置rngRow=范围(“A8:BA8”)
发现=错误
Cnt=0
对于rngRow中的每个细胞。细胞
如果未找到InStr(1,单元格值,“专业”),则
'捕获序列的开始-否则不执行任何操作
找到=真
Cnt=0
其他的
如果找到了
'如果处于查找模式,则递增计数器
Cnt=Cnt+1
'使用负偏移展开

如果Cnt
,正如其他回复所指出的那样,可能不是最好的策略。然而,正如您所要求的,这里有一段代码,使用一些循环内控制来克服本用例中每个
缺陷:

Sub CopySpecialtyOver()

Dim rngRow As Range
Dim Cell As Range
Dim Found As Boolean
Dim Cnt As Integer

Set rngRow = Range("A8:BA8")
Found = False
Cnt = 0

For Each Cell In rngRow.Cells

    If InStr(1, Cell.Value, "Specialty") And Not Found Then
        ' capture start of sequence - otherwise do nothing
        Found = True
        Cnt = 0
    Else

        If Found Then
            'if in Found mode increment counter
            Cnt = Cnt + 1

            ' expand using negative offset
            If Cnt <= 3 Then
                Cell = Cell.Offset(0, -Cnt).Value
            End If

            ' break after 3rd
            If Cnt = 3 Then
                Found = False
                Cnt = 0
            End If

        End If

    End If

Next Cell
End Sub
子副本特殊版本()
随着射程的增长而增长
暗淡单元格作为范围
Dim被发现为布尔值
作为整数的Dim Cnt
设置rngRow=范围(“A8:BA8”)
发现=错误
Cnt=0
对于rngRow中的每个细胞。细胞
如果未找到InStr(1,单元格值,“专业”),则
'捕获序列的开始-否则不执行任何操作
找到=真
Cnt=0
其他的
如果找到了
'如果处于查找模式,则递增计数器
Cnt=Cnt+1
'使用负偏移展开

如果Cnt您可以将其转换为正常的
for
循环,从右向左而不是从左向右,您可以将其转换为正常的
for
循环,从右向左而不是从左向右谢谢!我用我在下面贴的东西解决了这个问题。非常感谢!如果分支不是一个很好的样式,我最终用我在下面发布的内容解决了这个问题。。。为什么不干脆否定
If
条件,然后dum
Else
?再看一眼,我似乎不相信这个逻辑100%。。。如果“专业”后面的第四列再次包含搜索词,会发生什么情况?如果分支不是很好的样式,Emty
。。。为什么不干脆否定
If
条件,然后dum
Else
?再看一眼,我似乎不相信这个逻辑100%。。。如果“专业”后面的第四列再次包含搜索词,会发生什么情况?