Being new to VBA, I'm trying to add a customized scrollbar to my sheet. By customized I mean I can decide the min value, max value and smallchange of the scrollbar using a Userform where I asked the wanted values. So far I've stored the values in public variables following : screen of the Userform
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
and I just reassigned the values for ".Min", ".Max" and ".SmallChange." with A,B and C in the defaut scrollbar code given by Excel :
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
So I have 3 textboxes and one Commandbutton ("Valider"). Basically my idea is to fulfill those 3 boxes with the forementionned values (min, max,...) and store them in public variables. For the moment I'm just running my code from the developper tab using F5.
I'm first running the userform. Once the textboxes are fulfilled and the CommandButton pressed, the MessageBox returns the values contained in the variables A,B and C. Then I want to use those to "define" my scrollbar. When I press F5, The scrollbar is showing itself (see screenshot) but if I go to the properties all values are set to zero. Seems I'm not calling the variables A,B,C the right way : scrollbar properties
Thanks in advance for your help.
The problem is that your second sub (curseur) doesn't know what values you assigned from your form. Variables are 'destroyed' after last bit of your code ends. So variables A, B, C in curseur() have no value.
If you add the call of the procedure to the end of your valider_click sub this should fix your problem. You should exit your sub before if your values aren't numeric:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.