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
Vba 关于For循环的问题_Vba_Excel_For Loop_Offset - Fatal编程技术网

Vba 关于For循环的问题

Vba 关于For循环的问题,vba,excel,for-loop,offset,Vba,Excel,For Loop,Offset,为什么这不起作用?基本上,我在一个单元格中有一个列表,我想通过将字符串复制到不同的列中来拆分以“sec”结尾的字符串和不以“sec”结尾的字符串 Sub test_if() For i = 1 To 300 Cells(i, 2).Select If Right(Cells(i, 2), 3) = "SEC" Then ActiveCell.Select Selection.Copy Cells(i, 3).Se

为什么这不起作用?基本上,我在一个单元格中有一个列表,我想通过将字符串复制到不同的列中来拆分以“sec”结尾的字符串和不以“sec”结尾的字符串

Sub test_if()
   For i = 1 To 300
      Cells(i, 2).Select
      If Right(Cells(i, 2), 3) = "SEC" Then
         ActiveCell.Select
         Selection.Copy
         Cells(i, 3).Select
         ActiveSheet.Paste
      End If

      If Right(Cells(i, 2), 3) <> "SEC" Then
         ActiveCell.Select
         Selection.Copy
         'Cells(i, 4).Select
         ActiveCell.Offset(i - 1, 2).Select
         ActiveSheet.Paste
      End If
   Next i    
   Cells(1, 1).Select
End Sub
子测试_if()
对于i=1到300
单元格(i,2)。选择
如果正确(单元格(i,2),3)=“秒”,则
ActiveCell。选择
选择,复制
单元格(i,3)。选择
活动表。粘贴
如果结束
如果正确(单元格(i,2),3)“秒”,则
ActiveCell。选择
选择,复制
'单元格(i,4)。选择
偏移量(i-1,2)。选择
活动表。粘贴
如果结束
接下来我
单元格(1,1)。选择
端接头
试试这个:

Sub test_if()
    Dim i As Integer

    For i = 1 To 300
        With Cells(i, 2)
            If UCase(Right(.Value, 3)) = "SEC" Then
               .Offset(, 1).Value = .Value
            Else
               .Offset(i - 1, 2).Value = .Value
            End If
        End With
   Next i
End Sub
请读一读