2014-02-11 28 views
0

基本上,我試圖模仿使用代碼拆分和回收用於我的目的代碼的連接結果。但是,當腳本嘗試處理「Next T」idk時,我遇到了問題,但我已經表明它是一個Dim - Integer,而且似乎還沒有成功。VBA:無效的下一步控制變量參考

的代碼原始來源: Concatenate multiple ranges using vba

我已經有很多的問題,這一塊,因爲它似乎是我實際上已經想在我的腳本中的唯一的事情現在已經很久了。有編譯錯誤關閉If,調整Then,甚至退出循環。

我認爲下一個應該是我最後的擔心。

順便說一句,rnumbers應該保留一個值/整數的位置,但我不完全確定這是否正確完成。

rnumbers = Rows(ActiveCell.Range("A3").End(xlDown)) + 3 
    'or CellCount = ActiveCell.Range("A" & Rows.Count).End(xldown).Row 

    Do While Rows(ActiveCell.Range("A3").End(xlDown)) > 3 

     'For Q = 1 To 10 'This provides a column reference to concatenate - Outer For statement 
     For T = 3 To rnumbers 'This provides a rows reference to concatenate - Inner for statement 

      For Each Cell In Cells("A" & T) 'provides rows and column reference 
       If Cell.Value = "" Then 
        GoTo Line1 'this tells the macro to continue until a blank cell is reached 
        Exit For 
       End If 
       x = x & Cell.Value & Chr(10) 'This provides the concatenated cell value and comma separator 
      'Next ' this loops the range 

     Next T 'This is the inner loop which dynamically changes the number of rows to loop until a blank cell is reached 

     Line1: 
     On Error GoTo Terminate 'Terminates if there are less columns (max 10) to concatenate 

     ActiveCell.Value = Mid(x, 1, Len(x) - 1) 'This basically removes the last comma from the last concatenated cell e.g. you might get for a range 2,3,4, << this formula removes the last comma to 
    'give 2,3,4 

     ActiveCell.Offset(1, 0).Select 'Once the concatenated result is pasted into the cell this moves down to the next cell, e.g. from F1 to F2 

     x = "" 'The all important, clears x value after finishing concatenation for a range before moving on to another column and range 


     'Next Q 'After one range is done the second column loop kicks in to tell the macro to move to the next column and begin concatenation range again 

    'rnumbers = 0 
    'Next 
    Exit Do 
    'Resume 
    Terminate:'error handler 

回答

0

再次嘗試......當我把你的代碼仔細看看我實際使用一個壞詞。

你一直在錯誤的人羣中,並且正在挑選一些非常糟糕的代碼結構想法。 A GoTo後跟Exit For?後者的聲明永遠無法達成!跳出For循環是一件危險的事情(如果沒有錯的話)。是的,你仍然需要一個NextFor Each聲明(與一個匹配的控制參數 - Next T屬於一個不同的For循環,而不是最內層的一個)。

無論如何 - 我感覺像the Cat In The Hat:「這個爛攤子太大了,太深了,太高了 - 我們無法拿起它,沒有辦法!」。所以我決定替你建一座新房子。

我認爲以下是你想要做的,而且相當優雅。看看它是否有意義,如果你能適應你的目的。我需要去睡覺,但早上會看看你是否從這裏算出來。

Sub concAll() 
Dim allRows As Range, target as range 
Dim oneRow 
Dim nc as Integer 

Set allRows = Range("A3", "J10") ' pick the real range here - dynamically, probably 
nc = allRows.Columns.Count  ' need this number later to know where to put result 

For Each oneRow In allRows.Rows ' loop over one row of the range at a time 
    Dim s As String 
    s = ""       ' start with empty string 
    For Each c In oneRow.Cells  ' loop over all the cells in the row 

    If Not IsEmpty(c) Then 
     s = s & "," & c.Text 
    Else 
     Exit For      ' done with this row: found empty cell 
    End If 

    Next c       ' keep looping over the cells... 

    Set target = oneRow.Cells(1).Offset(0, oneRow.Cells.Count) ' cell where we put result 
    target.Value = Mid(s, 2) ' put the concatenated value to the right of everything; 
          ' skipping first comma (which came before first text) 
Next oneRow     ' repeat for all rows in source range 


End Sub 
+0

感謝您抽出寶貴時間來評論和協助我的麻煩。您提出的第一條評論,關於下一條評論和活動。原來,這是一個額外的下一個,我評論說,爲了幫助腳本在沒有任何編譯錯誤的情況下流動,我留下了那條評論,讓我想起了所做的更改和調整。特別是當我回頭看時,有助於我的進步和矯正技能的提高。我也將變量從T更改爲'rcell',這也引發了相同的錯誤。我發佈了其餘的腳本。任何其他想法? – user2988769

0

對不起,我應該解釋我試圖製作的東西,而不是要求修理我想做的事。我在vba的經歷是自學成才的,對於尋求幫助我有點新。

弗洛里斯製作的劇本似乎有功能,但並不像預期的那樣。原來我寫的東西有點過時了,需要擦拭和重新啓動。這實際上是我幾個月前開始使用的一箇舊的腳本,它從Web查詢中解脫出來。但該網站通過一些變化,現在劇本是在各地。

我遇到的主要問題是一個編譯錯誤「無效的下一個控制變量引用」,結果是由一個開放的'Do while'循環引起的,似乎沒有太多的研究出口點我擡頭。原本應該使用另一個'If'命令。同時,當試圖解決'Do While'時,我添加了一個'Next'(因爲我認爲它們是兼容的),並且它與腳本一起搞砸了。

很難解釋..但是,「別當」我使用,我想它只有當值的數量是更大

rnumbers = Rows(ActiveCell.Range("A3").End(xlDown)) + 3 
'or CellCount = ActiveCell.Range("A" & Rows.Count).End(xldown).Row 

Do While Rows(ActiveCell.Range("A3").End(xlDown)) > 3 

值相結合,但相反,它應該是

Dim CellCount As Range 

CellCount = ActiveCell.Range("A" & Rows.Count).End(xlDown).Row + 2 
'cause its the active cell + two additional cells 

If CellCount > 3 

然後通到弗洛里斯提交的劇本。 (但由於上述內容,這也失敗了)。

再次感謝,希望它解釋了一切...對不起,如果我浪費你的時間與弗洛里斯,真的很欣賞的援助。只是希望我早些時候要求幫助,會爲我節省很多我現在正在處理的挫折。 > _>