【问题标题】:Code Skipping Second Cell, Not Supposed To代码跳过第二个单元格,不应该
【发布时间】:2017-08-14 14:58:58
【问题描述】:

此代码是较大代码的一部分,它从列表框中获取单词并将其放入另一个列表框中,该代码使用此代码分隔列表框中的单词并建立成能够插入到单元格中的单词,出于某种原因第二个strsplt 没有显示,其他一切都运行良好,只是这个,我需要帮助并且没有抛出任何错误。我用 F8 和断点查看了它,问题似乎出在

If ii < .ColumnCount - 1 Then
    str = str & .List(i, ii) & vbCrLf
Else
    str = str & .List(i, ii)
End If

整个代码:

With Me.selecteditems
    ThisWorkbook.Sheets(9).Range("A:B").ClearContents
    For i = 0 To .ListCount - 1
        If .Selected(i) Then
            found = True
            For ii = 0 To .ColumnCount - 1
            ReDim strsplt(0 To i)
                If str = "" Then
                    str = .List(i, ii) & vbCrLf
                Else
                    If ii < .ColumnCount - 1 Then
                        str = str & .List(i, ii) & vbCrLf
                    Else
                        str = str & .List(i, ii)
                    End If
                End If
            Next ii
            message = "How much" & vbCrLf & str & "?" & vbCrLf
            title = "Amount"
            defaultval = "1"
            quantity = InputBox(message, title, defaultval)
            strsplt = Split(str, "*")
        End If
        'On Error Resume Next
        With ThisWorkbook.Sheets(9)
            .Range("A" & (i + 1)).Value = strsplt(i)
            .Range("B" & (i + 1)).Value = quantity
        End With
        'On Error GoTo 0
    Next i
End With  

编辑:使用debug.print str的方式

  1. 项目1
  2. item2 item3 item4 ...

【问题讨论】:

  • 请注意,如果您要嵌套循环,通常使用 j 而不是 ii 作为内部 for 循环!
  • vbLF 代替 vbCRLF 会发生什么?
  • @Jeeped 我听说 vbCrLf 在 Windows 上更好
  • 工作表中的多行单元格使用换行符 (LF),而不是回车符 (CRLF)。 0x013 (CR) 字符通常会被删除,只留下 0x010 (LF) 字符。
  • 我需要它,简而言之,转到下一行,想想,我应该这样做vbNewline

标签: vba excel listbox


【解决方案1】:

尝试一下这样的暴力破解:

If ii < .ColumnCount - 1 Then
    str = str & .List(i+1, ii) & vbCrLf
Else
    str = str & .List(i+1, ii)
End If

我已在您的代码中将 i 更改为 i+1。 然后再次调试。如果不起作用,请尝试i-1ii+1ii-1。其中之一将起作用,它可能会给出超出范围的错误。然后固定数组长度,玩得开心。

【讨论】:

  • 它只是给了我一个超出范围的订阅
  • @MaxAttack102 - 但在此之前? 2. 线存在吗?你用F8调试吗?您尝试了所有 4 种可能性吗?
  • 是的,其中 2 个给了我我们的范围,一个给了我一些关于 list 属性的信息。没有人说溢出,我可以再试一次
  • 只需重试所有 4 个并在 end if 之后添加一个 debug.print str。超出范围是正确的错误,而不是溢出。 :)
  • @MaxAttack102 - 恭喜! :) 一般来说,考虑使用vbNullString 代替"" - stackoverflow.com/questions/32435320/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
相关资源
最近更新 更多