2016-05-23 34 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