【问题标题】:Applying if statement to range of cells using VBA使用 VBA 将 if 语句应用于单元格范围
【发布时间】:2016-05-10 14:58:28
【问题描述】:

我有一小部分单元格,C6:C10。我正在尝试使用 VBA 代码将 if 语句应用于此单元格范围。目前,我的代码获取第一个单元格 (C6) 的 if 语句的输出,并将该值复制到单元格 C7:C10。 if 语句是正确的,我只是不确定如何将其应用于列中的一系列单元格。

Sub Cleanup()
Dim Segment As String
Dim i As Integer
Segment = ActiveCell(6, 3).Value
For i = 6 To 10
    If Right(Left(Segment, 6), 1) = "/" Then
        ActiveCell(i, 3).Value = Left(Segment, 5)
    Else
        ActiveCell(i, 3).Value = Left(Segment, 6)
    End If
Next i
End Sub

【问题讨论】:

  • ActiveCell 更改为Cells

标签: excel vba if-statement range


【解决方案1】:

如果您使用 Cells 而不是 ActiveCell 应该没问题,但您必须将循环从 7 更改为 10,否则它将覆盖原始单元格以及 C7:C10。

Sub Cleanup()
Dim Segment As String
Dim i As Integer
Segment = Cells(6, 3).Value
For i = 7 To 10
    If Right(Left(Segment, 6), 1) = "/" Then
        Cells(i, 3).Value = Left(Segment, 5)
    Else
        Cells(i, 3).Value = Left(Segment, 6)
    End If
Next i
End Sub

【讨论】:

    【解决方案2】:
    Sub Cleanup()
        Dim Segment As String
        Dim i As Integer
        Segment = Cells(i, 3).Value
        For i = 7 To 10
            If Right(Left(Segment, 6), 1) = "/" Then
                cells(i, 3).Value = Left(Segment, 5)
            Else
                Cells(i, 3).Value = Left(Segment, 6)
            End If
        Next i
    End Sub
    

    【讨论】:

    • 所以在将我的代码更改为这个之后,它现在给了我一个错误代码 400。我想我的代码指的是不存在或不再存在的东西,我不确定为什么将第 4 行中的 6 更改为“i”会导致这种情况。澄清一下,我不想向下复制单元格 C6 中的内容,我只想将 if 语句应用于 C6 之后的单元格
    【解决方案3】:

    这里有三个(在许多其他中)可能的代码,按简单顺序排列(最后一个比第一个更简单):

    Option Explicit
    
    Sub Cleanup()
        Dim Segment As String
        Dim i As Integer
    
        For i = 6 To 10
            Segment = Cells(i, 3).Value '<== Cells(i, 3) is the current cell as per the current row (i)
            If Mid(Segment, 6, 1) = "/" Then
                Cells(i, 3).Value = Left(Segment, 5)
            Else
                Cells(i, 3).Value = Left(Segment, 6)
            End If
        Next i
    End Sub
    
    
    Sub Cleanup2()
        Dim i As Integer
    
        For i = 6 To 10
            With Cells(i, 3) 'avoid repetitions (and a variable, too) by using 'With' keyword and implying 'Cells(i, 3)' preceeds every dot (".") till next 'End With" statement
                If Right(Left(.Value, 6), 1) = "/" Then
                    .Value = Left(.Value, 5)
                Else
                    .Value = Left(.Value, 6)
                End If
            End With
        Next i
    End Sub
    
    
    Sub Cleanup3()
        Dim i As Integer
    
        For i = 6 To 10
            With Cells(i, 3)
                .Value = Left(.Value, IIf(Mid(.Value, 6, 1) = "/", 5, 6)) ' use Iif function to avoid multiple lines. Also use 'Mid' function in lieu of 'Right(Left(..))'
            End With
        Next i
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-25
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      相关资源
      最近更新 更多