简体   繁体   English

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

[英]Excel VBA convert values of a range of cells

I have two columns from A1 to B10 in Sheet1. 我在Sheet1中有两列从A1到B10。 each cells in these columns have the strings "a" or "b" or "c" 这些列中的每个单元格都有字符串“ 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

I want to convert every cell that has a value "a" to the integer "12", "b" to the integer "14" and "c" to the integer "16" using VBA. 我想使用VBA将具有值“ a”的每个单元格转换为整数“ 12”,将“ b”转换为整数“ 14”,将“ c”转换为整数“ 16”。 and show the results in Sheet2 in corresponding cells. 并在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

I have this very incomplete codes: 我的代码很不完整:

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

You need to access each cell, read its value then overwrite in your loop: 您需要访问每个单元格,读取其值,然后在循环中覆盖:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM