如何根据条件在相邻单元格区域中插入值

时间:2019-05-11 20:40:28

标签: excel vba if-statement insert

我希望我的代码在单元格B1 if A1 is equal to "1"中插入“ 190”,并在光标闪烁之后立即插入,以便使B1的值与其他数字互补。

我已经完成了此过程。但是,它仅适用于一个单元格,而不适用于整个范围()

Sub BULK()
 If range("A1").Value = 1 Then range("B1").Value = C190
End Sub

我希望输出为“ C190”,并且光标闪烁以键入其他数字。

1 个答案:

答案 0 :(得分:0)

我不太确定我是否理解正确,但我会尝试一下。假设您有一个要遍历的范围,并将C190插入列B

Option Explicit
Sub FillValues()
Dim WS As Worksheet
Dim LRow As Long, i As Long
Dim ExtraDigits as Long

Set WS = ActiveSheet

    With WS
          LRow = .Range("A" & .Rows.Count).End(xlUp).Row

          For i = 1 To LRow
              If Len(Trim(.Range("A" & i).value)) = 1 Then
                 ExtraDigits = Application.InputBox("Add value:", Type:=1) 'If needed to enter different values modify from 1 to 2
                   .Range("B" & i).value = "C190" & ExtraDigits
               End If
          Next i
    End With
End Sub