Variable value lost

bconforto

New Member
Joined
Nov 17, 2016
Messages
30
Hello guys,
i have below code in Sheet2 for a ComboBox.

Code:
Public Combovalue As String
Private Sub ComboBox21_Change()
'Comboindex = Sheet2.ComboBox21.ListIndex + 1
Combovalue = Sheet2.ComboBox21.Value
    Select Case Combovalue
        Case "Italy"
            Sheets("Sheet1").Cells(2, 1).Value = "S060"
        Case "Germany"
            Sheets("Sheet1").Cells(2, 1).Value = "S057"
        Case "UK"
            Sheets("Sheet1").Cells(2, 1).Value = "S064"
    End Select
End Sub

I want to use Cambovalue in other macro in Module1, but this variable is empty there.
I tried setting it as Public in Sheet2 and also tried in Module1, but it is always empty inside the macro.

Any help would be much appreciatted.

Regards,
Brian
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You can do this

Code:
Public x As Integer


Sub Macro1()
    x = 1
End Sub


Sub Macro2()
    MsgBox x
End Sub

But most likely this is what you should be doing

Code:
Sub Macro1()
    Dim x As Integer
    x = 1
    Macro2 (x)
End Sub


Sub Macro2(x As Integer)
    MsgBox x
End Sub
 
Upvote 0
That's not really the issue here. It's tripping up at what and what is not in scope between sheet and standard modules.

Decent summary here edit:Removed Link

Edit: Sorry changed my mind, that wasn't a good summary. I thought it explicitly talked about public variables in sheet modules and other class modules. This is a better alternative Understanding Scope
 
Last edited:
Upvote 0
Thank you guys.
Based on the link you posted i changed this.
In Module1 instead of using variable Combovalue y used Sheet2.Combovalue and it worked.

Regards,
Brian
 
Upvote 0
Hello guys, sorry but it is not over yet.
I have 3 Macros in Module 1. The only one using Combovalue is Macro2.
If after making the selection in Combobox I execute Macro2, variable "Sheet2.Combovalue" has the correct value.
But if after making the selection in Combobox i execute Macro1 first, when I execute Macro2 later variable "Sheet2.Combovalue" is empty.
Any ideas why or how to solve it?

As a workaround, as i am populating a cell based on Combobox selection, I could read this cell again in Macro2 to determine the value, but i would like to know above question.

Regards,
Brian
 
Last edited:
Upvote 0
What does macro1's code look like?
 
Upvote 0
Macro1 is creating one document in SAP, but Combovalue is not used here.

I implemented the workaround i suggested before, but anyway i would like to know if this is possible somehow.
 
Upvote 0
We need to see the code to figure out why it's clearing the variable.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top