[英]how do i change only one word in excel-vba array
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 excel列表
1fbb(1)
2cbc(1)
5ddf(3)
5asd(6)
.
.
.
I want to get : 我想得到:
onefbb(1),twocbc(1),fiveddf(3),fiveasd(6) ...
but it result is... 但结果是...
onefdd(one),twocbc(one),fiveddf(three),fiveasd(six) ...
it`s all greek to me plz help... 这对我来说都是希腊文,请帮助...
This will do what you want, I am assuming the following: 这将满足您的要求,我假设以下情况:
There is only a value up to 7 at the start (well it would work up to 9, any single digit number really) 开始时最多只能有一个7值(那么它最多可以有9个值,实际上是任何一位数字)
You have not explicitly set option base to 1, if you have then remove the -1
in the code: 如果尚未在代码中删除
-1
,则尚未将option base显式设置为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
It works because your array addresses can be a direct reference (-1) from the number at the start of the value. 之所以起作用,是因为您的数组地址可以是值开头的数字的直接引用(-1)。
Here, I got one for you: 在这里,我为您准备了一个:
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
In my code, I separate a function to get the word for number. 在我的代码中,我分离了一个函数以获取数字单词。
I use as Sheets("sheetname").Range("K1:O10")
, so, you should change sheetname
to your sheet name. 我是用
Sheets("sheetname").Range("K1:O10")
那么,你应该改变sheetname
到您的工作表名称。 And I used fixed range K1:O10
because we can't use Columns("K:O")
while looping cell. 我使用了固定范围
K1:O10
因为在循环单元格时我们无法使用Columns("K:O")
。 It can harm the application. 它可能会损害应用程序。
So, please change that Range according to your input data. 因此,请根据您的输入数据更改范围。 I already tested about the code and it work well for me.
我已经对代码进行了测试,它对我来说很好用。 If it has any problem, let me know.
如果有任何问题,请通知我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.