2016-11-23 36 views
2

我想創建一個循環來更新已經在Userform上創建的一些「文本框」。 的問題是,當我連接線對於功能錯誤的下一個循環

   For y = 2 * (3 + k) To 2 * (3 + k) 

隨着

For k = 0 To 7 

我得到一個不匹配錯誤。但是如果我定義y = 6,一切都很好。

你能幫我嗎?

請參閱整個代碼如下:

Sub Update_TextBox_Preco() 

    Dim k As Double 
    Dim myarray2 As Variant 
    Dim y As Double 
    Dim Textbox As String 
    Dim Textbox_1 As String 
    Dim line As Variant 

    Array with contained TextBox names 
     myarray2 = Array("TextBox_Moeda_Atual", "TextBox_Medida_Atual", "TextBox_Acond_Atual", "TextBox_Lote_Atual", _ 
        "TextBox_Incoterm_Atual", "TextBox_p_liq_atual", "TextBox_encargo_atual", "TextBox_Frete_Atual") 

    For k = 0 To 7 
    Textbox = myarray2(k) 
       For y = 2 * (3 + k) To 2 * (3 + k) 
       UserForm1.Controls(Textbox).Value = Worksheets("PANEL").Cells(y, 45).Value 
       Next y 
    Next k 

    End Sub 
+1

我剛剛試過,沒有收到錯誤,錯誤在哪裏發生?你的第二個循環只產生6,8,10,12,14,16,18和20,所以爲什麼不能有一個變量y,從6開始每次遞增2? –

+0

@Nathan_Sav給出錯誤的循環/序列是'For y = 2 *(3 + k)To 2 *(3 + k)',它在For'k = 0 To 7'上鍊接。但是,如果我設置「對於y = 6到6」,例如,它正常運行。 –

+2

你應該'Dim'' k'和'y'作爲'Long'而不是'Double'。可能會發生一些舍入錯誤? – bobajob

回答

1

剛剛擺脫了y循環。它只觸發一次,所以每次移動k循環時只設置y

Sub Update_TextBox_Preco() 

Dim k As Long 
Dim myarray2 As Variant 
Dim y As Long 
Dim TextBoxUp As String 
Dim Textbox_1 As String 
Dim line As Variant 

'Array with contained TextBox names 
    myarray2 = Array("TextBox_Moeda_Atual", "TextBox_Medida_Atual", "TextBox_Acond_Atual", "TextBox_Lote_Atual", _ 
       "TextBox_Incoterm_Atual", "TextBox_p_liq_atual", "TextBox_encargo_atual", "TextBox_Frete_Atual") 

For k = 0 To 7 
    TextBoxUp = myarray2(k) 
    y = 2 * (3 + k) 
    UserForm1.Controls(TextBoxUp).Value = Worksheets("PANEL").Cells(y, 45).Value 
Next k 

End Sub 
+1

如果這適用於您,請接受綠色複選標記。 – Chrismas007

+0

錯誤仍然存​​在,但是如果我將'y = 2 *(3 + k)'中的'k'省略爲'y = 2 * 3',那麼結果會很好。 –

+1

@Petter_Mendes我複製並粘貼了這段代碼,直到'UserForm1.Controls'行(因爲我顯然沒有構建文本框)纔得到錯誤。 Type Mismatch沒有錯誤。 – Chrismas007