Home » excel » excel – Error passing a value between 2 subroutine

excel – Error passing a value between 2 subroutine

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to pass a String between 2 sub in VBA, but everytime I got a compilation Error

This is my code

Option Explicit

Private Sub ComboBox1_DropButtonClick()
    Dim value As String
    value = ComboBox1.value
    ComboBox1_Change value
End Sub

Private Sub ComboBox1_Change(ByVal value As String)
    Dim value2 As String
    value2 = value
End Sub

I am getting the below error

The routine declaration does not match the description of the event or routine of the same name

I’ve tried to remove ByVal but still nothing… Some tips?

How to&Answers:

The routine declaration does not match the description of the event or routine of the same name

You are getting the error because ComboBox1_Change doesn’t support passing of arguments. ComboBox1_Change(ByVal value As String) should be ComboBox1_Change()

@SiddharthRout Because I would like to get the value of the dropButton before it get changed. So when User Click on it, I get the value of the dropButton then when user change it I get the new value – ChangeWorld 10 mins ago

You do not need to add a Global variable. Declare a variable with a scope for within userform. Something like this

Option Explicit

Dim oldValue As String
Dim newValue As String

Private Sub ComboBox1_DropButtonClick()
    oldValue = ComboBox1.value
End Sub

Private Sub ComboBox1_Change()
    newValue = ComboBox1.value

    MsgBox oldValue
    MsgBox ComboBox1.value
End Sub