繁体   English   中英

如果单元格包含值,则VBA添加列

[英]VBA add columns if cell contains value

我正在处理以下内容,但是在循环期间无法选择实际的单元格。 它正在拉出一系列单元格,检查它们的值是否> 0,如果是,则在其右边添加3列。 这是我到目前为止所拥有的,不确定其效率如何:

    Dim varray As Variant
    Dim x As Long

    varray = Range("E13:AK13").Value

    For x = UBound(varray, 1) To LBound(varray, 1) Step -1
        If varray(x, 1) > 0 Then

            varray(x, 1).Activate 'ISSUE ON THIS LINE
            ActiveCell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
            ActiveCell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
            ActiveCell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
            'substract 2
        End If
    Next

像这样尝试。

Dim x As Long, rng As Range

Set rng = Range("E13:AK13")

For Each cell In rng
    If cell > 0 Then
        cell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
        cell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
        cell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
    End If
Next cell

当您使用数组时,很难选择所需的单元格。 我对代码做了一些修改,请检查它是否对您有用。

Dim varray As Range
Dim x As Long

Set varray = Range("E13:AK13") 

''For x = UBound(varray, 1) To LBound(varray, 1) Step -1
For Each c In varray
    If c.Value > 0 Then

        c.Select 'ISSUE ON THIS LINE
        ActiveCell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
        ActiveCell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
        ActiveCell.EntireColumn.Offset(0, 1).Insert (xlShiftToRight)
        'substract 2
    End If
Next c

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM