通过确定第一个数字Excel来在前面添加数字

时间:2017-04-19 17:05:07

标签: excel vba

我想在他们面前添加一个新号码,具体取决于第一个号码

  • 如果第一个数字是3,5,7,9,那么在前面添加2
  • 另外在前面添加6

使用微距按钮 enter image description here

4 个答案:

答案 0 :(得分:3)

这样的事情对你有用:

=--(IF(OR(--LEFT(A1,1)={3,5,7,9}),2,6)&A1)

答案 1 :(得分:2)

Select Case语句可以轻松进行多重比较。

dim rw as long
with worksheets("sheet1")
    for rw = 2 to .cells(.rows.count, "A").end(xlup).row
        select case int(left(.cells(rw, "A").value2, 1))
            case 3, 5, 7, 9
                .cells(rw, "A") = int(2 & .cells(rw, "A").value2)
            case else
                .cells(rw, "A") = int(6 & .cells(rw, "A").value2)
        end select
    next rw
    .range(.cells(2, "A"), .cells(.rows.count, "A").end(xlup)).numberformat = "0 000-0000"
end with

答案 2 :(得分:0)

如果使用公式在新列中重新创建列表,请使用:

=IF(OR(LEFT(A2)="3",LEFT(A2)="5",LEFT(A2)="7",LEFT(A2)="9"),2&A2,6&A2)

答案 3 :(得分:0)

如果要通过VBA执行此操作,请运行以下代码序列:

Sub AddNumber()

    Dim i As Long, j As Long
    Dim Strt As String
        i = 2 'your first row index with data
        j = 1 'the column index your list is located (e.g. column A)

    Do While Cells(i, j) <> ""
        Strt = Left(Cells(i, j), 1)
        Select Case Strt
        Case 3, 5, 7, 9
            Cells(i, j) = 2 & Cells(i, j)
        Case Else
            Cells(i, j) = 6 & Cells(i, j)
        End Select
        i = i + 1
        Loop

End Sub