将语句应用于使用VBA的单元格范围 [英] Applying if statement to range of cells using VBA

查看:92
本文介绍了将语句应用于使用VBA的单元格范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小范围的细胞,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(左(段,6),1)=/然后
ActiveCell(i,3).Value = Left(Segment,5)
Else
ActiveCell(i,3).Value = Left(Segment,6)
End If
Next i
End Sub


解决方案

  Sub Cleanup()
Dim Segment As String
Dim i As Integer
段=单元格(i,3).Value
对于i = 7到10
如果右(左(段,6),1)=/然后
个单元格(i,3).Value = Left(Segment,5)
Else
单元格(i,3).Value = Left(Segment,6)
End If
Next i
End Sub


I have a small range of cells, C6:C10. I'm trying to apply an if statement to this range of cells using VBA code. Currently, my code takes the output of the if statement for the first cell (C6), and replicates that value for cells C7:C10. The if statement is correct, I'm just not sure how to apply it to a range of cells in a column.

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

解决方案

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

这篇关于将语句应用于使用VBA的单元格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆