Skip to main content
executable snippets only work with JavaScript...
Source Link
Mathieu Guindon
  • 71.5k
  • 8
  • 113
  • 250

Public Abc As String

Public Abc As String 

Private Sub Workbook_Open() Abc = "C5" End Sub

Private Sub Workbook_Open() Abc = "C5" End Sub 

Public Abc As String

Private Sub Workbook_Open() Abc = "C5" End Sub

Public Abc As String 
Private Sub Workbook_Open() Abc = "C5" End Sub 
Source Link

VBA Excel - Mantaining Public Variables With Button Press

I'm trying to Create a document that has a bunch o constant strings.

I've declared then Public in a Module like this:

Public Abc As String

In "ThisWorkbook" I run the following code to initialize de variable

Private Sub Workbook_Open() Abc = "C5" End Sub

I have Buttons coded to change some values like:

If Range(Abc) = "" Then Range(Abc) = 1 Else Range(Abc) = Range(Abc) + 1 End If 

When I run a button with this code:

Sub BotaoNovoDia() i = 3 While i <= 33 If Cells(i, 11) = "" Then Cells(i, 11) = Range(Apresentacao) Cells(i, 12) = Range(Aceitacao) Cells(i, 13) = Range(Aceitou) Cells(i, 31) = Range("D41") Cells(i, 11).Interior.Color = Range(Apresentacao).Interior.Color Cells(i, 12).Interior.Color = Range(Aceitacao).Interior.Color If Range("K34") < 0.65 Then Range("K34").Interior.Color = vbRed Else Range("K34").Interior.Color = vbGreen End If If Range("L34") < 0.45 Then Range("L34").Interior.Color = vbRed Else Range("L34").Interior.Color = vbGreen End If Range(Aceitou) = 0 Range(Rejeitou) = 0 Range(NaoApres) = 0 End Else i = i + 1 End If Wend End Sub 

And i try to run the first button again I get and error saying: "Run-Time error '1004': Method 'Range' of object '_Global' failed" the debug button takes me to the fisrt line that tries to access to the public variables value. What can i do to mantain the values in the Public variables?