【问题标题】:Split function vba拆分函数vba
【发布时间】:2015-05-11 18:57:51
【问题描述】:

我目前正在尝试获取用户输入并使用逗号分隔输入的每一位。目前我正在尝试使用 -

   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

【问题讨论】:

  • 当您的用户 ESC 退出您的 InputBox 时也会爆炸
  • 我没有任何问题,请问您为什么这么说?
  • 嗯,VB 必须对空字符串指针进行字符串合并。

标签: vba


【解决方案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

【讨论】:

  • 我会继续试一试,请问您为什么推荐包含()而不是将其作为变体?此外,当我将其更改为数组时,它会在我的其余代码中调用错误。
  • 我尽可能避免变体。错误来自使用没有索引的数组。查看我的编辑。
  • 好的,我继续尝试,将更改放入原始帖子。我几乎可以完美地工作。我唯一剩下的就是处理没有输入包含语句的实例。我试图让它在没有输入任何内容时自动放置一个逗号。我试过 - 如果包含(x)=“”那么包含(x)=“,”但没有运气
  • 我有点迷失在你的逻辑中,看起来你并不是每次都迭代包含的项目。那有必要吗?在编写代码时,它只会检查包含的第一项,因为 x 将默认为 0。
  • 抱歉,这是我第一次在 VBA 中编写如此密集的内容。我试图让它检查所有具有初始关键字的交互,然后在这些结果中,找到包含包含语句的任何交互,获取这些结果并将它们复制到另一个工作表,根据搜索的单词命名该工作表。
猜你喜欢
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 2016-02-26
相关资源
最近更新 更多