突出字excel

我正在编写一个VBA程序,它将允许我通过一组Excel数据挖掘相关信息,然后将其复制到另一个表单中。

我一直试图使它正在search的单词以黄色突出显示,但是我的程序不断抛出“编译错误 – 在Ubound上的预期数组”。

Option Compare Text Public Sub Textchecker() ' ' Textchecker ' ' Keyboard Shortcut: Ctrl+h ' Dim Continue As Long Dim findWhat As String Dim LastLine As Long Dim toCopy As Boolean Dim cell As Range Dim item As Long Dim j As Long Dim sheetIndex As Long Dim inclusion As String sheetIndex = 2 Continue = vbYes Do While Continue = vbYes findWhat = CStr(InputBox("What word would you like to search for today?")) inclusion = CStr(InputBox("Do you have any inclusions? Separate words with commas")) LastLine = ActiveSheet.UsedRange.Rows.Count If findWhat = "" Then Exit Sub j = 1 For item = 1 To LastLine If UBound(inclusion) >= 0 Then For Each cell In Range("BY1").Offset(item - 1, 0) Then For Each item In inclusion If InStr(cell.Text, findWhat) <> 0 And InStr(cell.Text, inclusion) <> 0 Then findWhat.Interior.Color = 6 toCopy = True Else For Each cell In Range("BY1").Offset(item - 1, 0) Then If InStr(cell.Text, findWhat) <> 0 Then findWhat.Interior.Color = 6 toCopy = True End If Next item End If Next If toCopy = True Then Sheets(sheetIndex).Name = UCase(findWhat) + "+" + LCase(inclusion) Rows(item).Copy Destination:=Sheets(sheetIndex).Rows(j) j = j + 1 End If toCopy = False Next item sheetIndex = sheetIndex + 1 Continue = MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) Loop End Sub 

我在这里做错了什么?

在你的代码中, inclusion被声明为一个Stringvariables,并且包含一个String ,尽pipe是一个由逗号分隔的StringUbound函数在数组上工作。

解决方法:使用Split函数将string转换为数组。 请参阅下面的示例以获得一些快速帮助,如果您需要更多详细信息,请告知我们。

 Sub Tests() Dim inclusion() As String inclusion = Split("One, Two, Three", ",") MsgBox (UBound(inclusion)) End Sub 

回答你最后的评论。

For Each中的variables必须是Object或Varianttypes的variables。

要在一个Variant中改变你的'item',把'Dim item As Long'replace为'Dim item As Variant',或者甚至用'Dim item'作为一个variables来声明。