2017-08-14 66 views
0

此代碼是較大代碼的一部分,它將一個列表框中的單詞從另一個列表框中放入另一個列表框中,該列表框使用此代碼將列表框中的單詞分隔開,並將其建立爲能夠成爲插入到一個單元格中,由於某種原因,第二個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. 方式item1
  2. item2 item3 item4 ...
+1

如果你想要嵌套你的循環,只需要把''''替代爲'ii'作爲循環的內部循環! – Jsleshem

+0

'vbLF'與'vbCRLF'有什麼不同? – Jeeped

+0

@Jeeped我聽說vbCrLf在Windows上更好 – MaxAttack102

回答

0

嘗試有點暴力破解這樣的:

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

我已經在你的代碼改變ii+1。 然後再次調試。如果不起作用,請嘗試使用i-1,ii+1,ii-1。其中一個會起作用,它可能會導致超出範圍的錯誤。然後修復數組長度並獲得樂趣。

+0

它只是讓我訂閱超出範圍 – MaxAttack102

+0

@ MaxAttack102 - 但在此之前呢? 2.線是否存在?你用F8進行調試嗎?你有沒有嘗試所有4種可能性? – Vityata

+0

是的,他們中的2人給了我們我們的範圍和一個給了我一些關於列表屬性。沒有任何關於溢出的說法,我可以再試一次 – MaxAttack102

相關問題