2016-05-23 37 views
1

作爲VBA新手,我正在嘗試向我的工作表添加自定義滾動條。通過自定義,我的意思是我可以使用Userform來確定滾動條的最小值,最大值和小變化,我在其中詢問想要的值。到目前爲止,我已經存儲在以下公共變量的值: screen of the UserformVBA自定義用戶窗體變量的滾動條

Option Explicit 

Public A As Integer 
Public B As Integer 
Public C As Integer 


Private Sub Valider_Click() 

If IsNumeric(TextBox1.Value) Then 
    A = TextBox1.Value 
    Unload Me 
Else 
    MsgBox "Valeur mimimale incorrecte" 
End If 

If IsNumeric(TextBox2.Value) Then 
    B = TextBox2.Value 
    Unload Me 
Else 
    MsgBox "Valeur maximale incorrecte" 
End If 

If IsNumeric(TextBox3.Value) Then 
    C = TextBox3.Value 
    Unload Me 
Else 
    MsgBox "Pas incorrect" 
End If 

MsgBox A & " " & B & " " & C 

End Sub 

,我只是重新分配「.Min」,「最大」和值「.SmallChange。」用A,B和C在用Excel給出的defaut滾動代碼:

Sub curseur() 

ActiveSheet.ScrollBars.Add(180, 45.75, 119.25, 13.5).Select 
With Selection 
    .Value = 0 
    .Min = A 
    .Max = B 
    .SmallChange = C 
    .LargeChange = 10 
    .LinkedCell = "$G$4" 
    .Display3DShading = True 
End With 
Range("F4").Select 
ActiveCell.FormulaR1C1 = "=RC[1]/100" 
Range("G4").Select 
With Selection.Interior 
    .Pattern = xlSolid 
    .PatternColorIndex = xlAutomatic 
    .ThemeColor = xlThemeColorDark1 
    .TintAndShade = -0.149998474074526 
    .PatternTintAndShade = 0 
End With 
With Selection.Font 
    .ThemeColor = xlThemeColorDark1 
    .TintAndShade = -0.149998474074526 
End With 
End Sub 

所以我有3個文本框和一個命令按鈕( 「驗證者」)。基本上我的想法是用預先設定的值(min,max,...)完成這3個盒子,並將它們存儲在公共變量中。目前,我只是使用F5從developper選項卡運行我的代碼。

我第一次運行userform。一旦文本框完成並按下CommandButton,MessageBox就會返回變量A,B和C中包含的值。然後我想用這些來「定義」我的滾動條。當我按F5時,滾動條顯示自己(見截圖),但如果我去屬性所有值設置爲零。似乎我沒有調用變量A,B,C正確的方式:scrollbar properties

在此先感謝您的幫助。

+0

究竟發生了什麼以及缺少什麼?你有一個包含多個文本框的表單嗎?因爲你在第一次檢查後卸貨。你的msgbox說什麼?你如何以及何時調用你的代碼?請添加一些細節以幫助我們提供幫助。我不知道你是如何轉發你的滾動條的參數... – Jochen

+0

我編輯我的帖子,添加2個截圖和更詳細的解釋。它有幫助嗎? (我很抱歉,因爲我是新手,我不確定你需要哪些信息,但這幾乎都是我的代碼)。感謝您的幫助btw! – beckq

回答

0

問題是,你的第二個子(curseur)不知道你從表單分配了什麼值。變量在代碼的最後一位結束後被「銷燬」。因此,curseur()中的變量A,B,C沒有任何價值。

如果您將過程的調用添加到valider_click子版的末尾,應該可以解決您的問題。如果您的值不是數值,您應該先退出分支:

Private Sub Valider_Click() 

    If IsNumeric(TextBox1.Value) Then 
     A = TextBox1.Value 
    Else 
     MsgBox "Valeur mimimale incorrecte" 
     Exit Sub 
    End If 

    If IsNumeric(TextBox2.Value) Then 
     B = TextBox2.Value 
    Else 
     MsgBox "Valeur maximale incorrecte" 
     Exit Sub 
    End If 

    If IsNumeric(TextBox3.Value) Then 
     C = TextBox3.Value 
    Else 
     MsgBox "Pas incorrect" 
     Exit Sub 
    End If 

    MsgBox A & " " & B & " " & C 
    Call curseur 
    Unload Me 

End Sub 
+0

再次感謝。但仍然有一個地方...我的子「curseur」的呼叫工作正常(我只需要寫「curseur.curseur」,因爲我的Sub在一個模塊調用「curseur」...我進步得到熟悉層次結構)。我仍然在上面,但如果你有一個想法,在此先感謝 – beckq

+0

我曾經將變量A,B和C作爲變量從用戶窗體調用。 Id est:「.Min = userform1.A」。它是否做出敏感? – beckq

+0

可能最好的方法是將你的sub聲明爲Sub Curseur(A作爲INteger,B作爲Integer,C作爲Integer)',並將呼叫改爲'Call curseur(A,B,C)'。在幾乎所有情況下,最好用參數調用而不是聲明和使用全局變量。 – Jochen