Excel中怎么通过VBA代码实现,当A列中的值为1时,B列自动补充值为S,当A列中的值为2时,B列自动补充值为M
展开全部
以下代码能满足要求:
Sub 自动填充()
Dim Ar, I&, S, M
S = 10: M = 20 'S,M先得赋值
Ar = Range("A1", Cells(Rows.Count, 1).End(3).Offset(, 2))
For I = 2 To UBound(Ar)
If Ar(I, 1) = 1 Then
Ar(I, 2) = S
ElseIf Ar(I, 1) = 2 Then
Ar(I, 2) = M
End If
Next
Range("A1").Resize(UBound(Ar), 2) = Ar
End Sub
展开全部
Sub aa()
Dim i As Integer, arr1(), arr2()
arr1 = Range([a2], Cells(Rows.Count, 1).End(xlUp))
ReDim arr2(1 To UBound(arr1), 1 To 1)
For i = 1 To UBound(arr1)
Select Case arr1(i, 1)
Case 1
arr2(i, 1) = "s"
Case 2
arr2(i, 1) = "m"
End Select
Next i
Range([b2], Cells(Rows.Count, 1).End(xlUp).Offset(0, 1)) = arr2
End Sub
Dim i As Integer, arr1(), arr2()
arr1 = Range([a2], Cells(Rows.Count, 1).End(xlUp))
ReDim arr2(1 To UBound(arr1), 1 To 1)
For i = 1 To UBound(arr1)
Select Case arr1(i, 1)
Case 1
arr2(i, 1) = "s"
Case 2
arr2(i, 1) = "m"
End Select
Next i
Range([b2], Cells(Rows.Count, 1).End(xlUp).Offset(0, 1)) = arr2
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用if条件嵌套也可以实现
B1公式: =if(a1=1,b1=s,if(a1=2,b1=m,if(a1=3,b1=........)))
然后下拉即可!
B1公式: =if(a1=1,b1=s,if(a1=2,b1=m,if(a1=3,b1=........)))
然后下拉即可!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询