Vba 数字数组中的字符串出现错误
Vba 数字数组中的字符串出现错误,vba,excel,Vba,Excel,数组(“98117849”)是一个带有一个字符串的数组:“98117849” 而Array(98117849)是一个包含两个元素的数组:9811和7849 您应该看看Split()函数 sub highlight(phm as variant) Dim w As Workbook Dim sh As Worksheet Dim x As Integer Dim rn As Range Dim k As Long Dim number As Variant number = Array(phm
数组(“98117849”)
是一个带有一个字符串的数组:“98117849”
而Array(98117849)
是一个包含两个元素的数组:9811
和7849
您应该看看Split()
函数
sub highlight(phm as variant)
Dim w As Workbook
Dim sh As Worksheet
Dim x As Integer
Dim rn As Range
Dim k As Long
Dim number As Variant
number = Array(phm)
如果需要将number
作为整数,请也应用CInt()
:
编辑
您在注释中请求将此代码段添加到子例程中:
Dim number() As Integer
phm=Split("9811,7849",",")
ReDim number(LBound(phm) To UBound(phm)) As Integer
For i=LBound(phm) To UBound(phm)
number(i)=CInt(phm(i))
Next i
子高亮显示(ByVal phm作为字符串)
将w作为工作簿
将sh设置为工作表
作为整数的Dim x
低雷诺数范围
暗k一样长
Dim number()作为字符串
编号=拆分(phm,“,”)
Set w=此工作簿
设置sh=w工作表(“表1”)
sh.选择
单元格。查找(“hello”)。选择
ActiveCell.Offset(1,0)。选择
设置rn=sh.UsedRange
k=rn.Rows.Count+rn.Row-1
对于x=1到k
对于j=0到UBound(数字)
如果ActiveCell.Value编号(j),则
Selection.Interior.Color=vbYellow
其他的
Selection.Interior.ColorIndex=xlNone
退出
如果结束
下一个j
偏移量(1,0)。选择“将ActiveCell下移一行”。
下一个x
端接头
那是不可读的。修正你的缩进,解释你的代码,解释什么结果是不正确的。缩进是正确的。我已经发布了两个代码。第一个是我尝试过的,第二个是我想要的。更正意味着所有单元格都高亮显示,而不是不匹配的单元格。我知道错误的原因是:number=Array(“98117849”)。Im无法修复itIf ActiveCell.Value=number(j)或ActiveCell.Value=vbNullString然后选择Selection.Interior.ColorIndex=xlNone-Else选择。Interior.Color=vbRed,。。。。。。除了空白单元以外的所有单元都被高亮显示为红色。
number=Split(phm,",")
Dim number() As Integer
phm=Split("9811,7849",",")
ReDim number(LBound(phm) To UBound(phm)) As Integer
For i=LBound(phm) To UBound(phm)
number(i)=CInt(phm(i))
Next i
Sub highlight(ByVal phm As String)
Dim w As Workbook
Dim sh As Worksheet
Dim x As Integer
Dim rn As Range
Dim k As Long
Dim number() As String
number = Split(phm, ",")
Set w = ThisWorkbook
Set sh = w.Worksheets("Sheet1")
sh.Select
Cells.Find("hello").Select
ActiveCell.Offset(1, 0).Select
Set rn = sh.UsedRange
k = rn.Rows.Count + rn.Row - 1
For x = 1 To k
For j = 0 To UBound(number)
If ActiveCell.Value <> number(j) Then
Selection.Interior.Color = vbYellow
Else
Selection.Interior.ColorIndex = xlNone
Exit For
End If
Next j
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next x
End Sub