String Vba如何比较2列
我在做一个启发式分析,我有一个附带的问题:我想在D列中找到与J列匹配的数字,并用“0”替换它们。你可以在这张图片上看到我想做什么: 问题:D列每个单元格有多个值,J列每个单元格有一个值。 代码的某些部分:String Vba如何比较2列,string,excel,replace,comparison,vba,String,Excel,Replace,Comparison,Vba,我在做一个启发式分析,我有一个附带的问题:我想在D列中找到与J列匹配的数字,并用“0”替换它们。你可以在这张图片上看到我想做什么: 问题:D列每个单元格有多个值,J列每个单元格有一个值。 代码的某些部分: Dim i,j As Integer Dim temp As String Dim x As Integer Dim d As String i = Application.CountA(Range("E:E")) + 10 'number of cell with values
Dim i,j As Integer
Dim temp As String
Dim x As Integer
Dim d As String
i = Application.CountA(Range("E:E")) + 10
'number of cell with values
j = Application.CountA(Range("J:J")) + 10
For j = 11 To j
temp = Range("J" & j).Value
For i = 11 To i
d = Range("D" & i).Value
*For x = LBound(vec) To UBound(vec)
If vec(x) = temp Then
vec(x) = 0
Range("D" & i).Value = vec(x)
End If
Next
Next
Next
*->这就是问题所在,我不知道如何越过D列中的“,”并存储数据。我想将temp与“d”上的值进行比较,但“d”可以在同一单元格上有多个数字,如“3,2,1”,如果有匹配项,如temp=3,则d=“0,2,1”
英语不是我的母语,所以我希望你能理解我的意思
谢谢 您可以使用下面的公式执行此操作-无需VBA
=IF(NOT(ISERROR(FIND(J:J,D:D))),SUBSTITUTE(D:D,J:J,"0"),D:D)
- 如果需要,可以隐藏D列以避免混淆
Dim i,j As Integer
Dim temp As String
Dim x As Integer
Dim d As String
i = Application.CountA(Range("E:E")) + 10
'number of cell with values
j = Application.CountA(Range("J:J")) + 10
For j = 11 To j
temp = Range("J" & j).Value
For i = 11 To i
d = Range("D" & i).Value
Vec = split(d, ",") 'split the cell
d = "" 'clear the string
For x = LBound(vec) To UBound(vec)
If vec(x) = temp Then
vec(x) = 0
End If
d = d & vec(x) & "," 'recreate the string
Next
Range("D" & i).Value = left(d, len(d) - 1) 'save the string without the last ,
Next
Next
嘿,谢谢你的回答!我已经尝试过split函数,但使用该函数的问题是如何用“,”再次将值传递给D列?因为当您将数据(数组)传递给列时,将像这样分割数字“023”,我想要“0,2,3”。因此,您可以在数组中循环,用逗号连接每个数字,构建你的字符串。或者,你可以使用公式我不明白为什么。我得测试一下,然后再给你回复。