代码之家  ›  专栏  ›  技术社区  ›  CPS

当变量值为空时,在另一个变量中赋值

  •  0
  • CPS  · 技术社区  · 2 年前

    例如:

    • 当AAA(2)=空时,单元格(2,1)=BBB(2)=54321
    With Activesheet
        Range(cells(1,1), cells(10,1) = AAA
        If Range(cells(1,1), cells(10,1) = "" Then
         Range(cells(1,1), cells(10,1) = BBB
        End if
    End With
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Scott Craner    2 年前

    迭代 AAA 并用中的相应值替换空白 BBB 然后应用阵列:

    Dim i As Long
    For i = LBound(AAA, 1) To UBound(AAA, 1)
        If AAA(i, 1) = "" Then AAA(i, 1) = BBB(i, 1)
    Next i
    With ActiveSheet
        .Range(.Cells(1, 1), .Cells(10, 1)) = AAA
    End With
    

    AAA级 然后创建第三个数组并将其用于输出:

    Dim CCC As Variant
    CCC = AAA
    
    Dim i As Long
    For i = LBound(AAA, 1) To UBound(AAA, 1)
        If AAA(i, 1) = "" Then CCC(i, 1) = BBB(i, 1)
    Next i
    With ActiveSheet
        .Range(.Cells(1, 1), .Cells(10, 1)) = CCC
    End With