拆分功能vba

时间:2015-05-11 18:57:51

标签: vba

我目前正在尝试接受用户输入并使用逗号分割输入的每一位。目前我正在尝试使用 -

   includer = CStr(InputBox("Do you have any inclusions? Separate words with commas"))
        inclusion = Split(includer, ",", , vbTextCompare)

没有运气。

它不断抛出“类型不匹配”错误。

我试图使用的完整代码,对任何感兴趣的人来说都是 -

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 x 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?"))
        includer = CStr(InputBox("Do you have any inclusions? Separate words with commas"))
        inclusion = Split(includer, ",", , vbTextCompare)

        LastLine = ActiveSheet.UsedRange.Rows.Count
        If findWhat = "" Then Exit Sub
        j = 1
    For item = 1 To LastLine
        For Each cell In Range("BY1").Offset(item - 1, 0)
            If InStr(cell.Text, findWhat) <> 0 And InStr(cell.Text, inclusion(x)) <> 0 Then
                toCopy = True

            End If
        Next
        If toCopy = True Then
            Sheets(sheetIndex).Name = UCase(findWhat) + "+" + LCase(inclusion(x))
            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

1 个答案:

答案 0 :(得分:1)

更改

Dim inclusion As String

Dim inclusion() As String 'recommended in my opinion

Dim inclusion As variant

修改

我稍后会在代码中看到您对包含的使用。看起来您需要迭代新阵列。

dim x as long 'put this at the top, rename as you'd like.
for x = lbound(inclusion) to ubound(inclusion)
   'do stuff with inclusion(x), i other code here before but I'm not sure that it was right anymore.
next 

编辑2

        For item = 1 To LastLine
            If UBound(insulation) >= 0 Then
                'write procedure for when inclusion are present
            Else
                'write 2nd procedure for when inclusions are not present
            End If
        Next item