Excel VBA转换一系列单元格的值

时间:2015-02-06 11:07:31

标签: excel vba

我在Sheet1中有两列从A1到B10。这些列中的每个单元格都有字符串" a"或" b"或" c"

     A    B
1    a    a
2    a    b
3    b    c
4    b    b
5    a    a
6    c    b
7    a    a
8    a    c
9    b    a
10   a    b

我想转换每个具有值的单元格#34; a"到整数" 12"," b"到整数" 14"和" c"到整数" 16"使用VBA。并在相应的单元格中显示Sheet2中的结果。

     A   B
1   12   12
2   12   14  
3   14   16
4   14   14
5   12   12
6   16   14
7   12   12
8   12   16
9   14   12
10  12   14

我有这个非常不完整的代码:

Sub getConvert()

For Each original In Range("A1 , B10")
 Select Case original
   Case "a"
    CorrectedText = Replace(original, "a", "12")
   Case "b"
    CorrectedText = Replace(original, "b", "14")
   Case Else
    CorrectedText = Replace(original, "c", "16")
 End Select
Next
End Sub

1 个答案:

答案 0 :(得分:0)

您需要访问每个单元格,读取其值然后在循环中覆盖:

Dim original As Range, destination As Worksheet

Set destination = Sheets("Sheet2")

For Each original In Range("A1:B10")
    Select Case original.Value
        Case "a": destination.Range(original.Address).Value = "12"
        Case "b": destination.Range(original.Address).Value = "14"
        Case "c": destination.Range(original.Address).Value = "16"
    End Select
Next