Sub Macro()
Dim T As Variant, ReT As Variant
Dim i As Integer
T = Array("1", "2", "3", "4", "5", "6", "7")
ReT = Array("one", "two", "three", "Four", "five", "six", "seven")
For i = 0 To UBound(T)
Columns("K:O").ReplaceT (i), ReT(i)
Next i
End Sub
excel list
1fbb(1)
2cbc(1)
5ddf(3)
5asd(6)
.
.
.
我想得到:
onefbb(1),twocbc(1),fiveddf(3),fiveasd(6) ...
但结果是......
onefdd(one),twocbc(one),fiveddf(three),fiveasd(six) ...
这对我来说都是希腊人的帮助......
答案 0 :(得分:1)
这将做你想要的,我假设如下:
一开始只有一个值高达7(好吧它最多可以达到9,真正的任何一位数字)
如果您已删除代码中的-1
,则尚未将选项库明确设置为1:
Sub ChangeFirstChar()
Dim cell As Range, ret As Variant
ret = Array("one", "two", "three", "Four", "five", "six", "seven")
For Each cell In Range("K1:O" & Range("K" & Rows.Count).End(xlUp).Row)
cell.Formula = ret(CLng(Left(cell.text, 1)) - 1) & Right(cell.text, Len(cell.text) - 1)
Next
End Sub
它的工作原理是因为您的数组地址可以是值开头的数字的直接引用(-1)。
答案 1 :(得分:0)
在这里,我找到了一个给你:
Public Sub replaceNumber()
Dim cell As Range
For Each cell In Sheets("sheetname").Range("K1:O10")
cell.Value = getWords(Left(cell.Value, 1)) & Mid(cell.Value, 2)
Next cell
End Sub
Private Function getWords(number As String)
Dim numbers, words As Variant
Dim index As Long
numbers = Array("1", "2", "3", "4", "5", "6", "7")
words = Array("One", "Two", "Three", "Four", "Five", "Six", "Seven")
For index = 0 To UBound(numbers)
If number = numbers(index) Then
getWords = words(index)
Exit For
End If
Next index
End Function
在我的代码中,我将一个函数分开来获取数字。
我用 Sheets("sheetname").Range("K1:O10")
,因此,您应该将sheetname
更改为您的工作表名称。我使用固定范围 K1:O10
,因为我们在循环细胞时无法使用Columns("K:O")
。它可能会损害应用程序。
因此,请根据您的输入数据更改该范围。我已经测试了代码,它对我很有用。如果有任何问题,请告诉我。