Public Variable Declaration Not Being Recognized
Jul 27, 2007
Hopefully this isn't too vague, but almost every project I've worked on so far has not recognized Public variables in all modules. I've constantly been searching for exceptions to this rule that could be contributing to this issue but haven't been successful.
I have read a million times that declaring a variable as Public makes it visible to all modules and preserves the value throughout. I've also read that if you edit code or reset your project, the values may be lost. I also know that if you try to use a variable before you give it a value (or for object variables set it = to something), it doesn't have a value yet. But all this applied, I'm still not getting Public variables to be consistently recognized or stay with the value I want them to have throughout the lifetime of my project (while the workbook is open)
My specific current issue (one of many so far) is that I have 4 command buttons within a worksheet object. Each triggers a different group of procedures, some of these are user forms. In the first button, I create two worksheets one to contain the current fiscal period "Options" the other for "Summary" of all the totals etc... (all financial data). In this first userform, triggered by the button, I allow the user to name these worksheets: like Summer2007Options or Summer2007Summary and I declare these publically as object variables OptSheet and SumSheet. These variable names and sheets are recognized for the next 2 buttons but now that I'm on the fourth, I have object variable not set errors when I try to refer to this variable.
View 9 Replies
ADVERTISEMENT
Aug 28, 2009
I'm using a public declaration for a worksheet but I seem to have to initialize it in each routine. Is there a way to set it and leave it? So in a module I have:
View 4 Replies
View Related
Jan 7, 2008
I've been asked to produce a spread sheet that will calculate split payments for a list of households built by our company. The bill amount will be given to us by the power company, and the basis for bill separation is base on the billing period, and the date of house settlement.
The codes I've produced are written below, however I couldn't get it to run properly. The software keeps on telling me that my variables aren't declared correctly.
I've attached all my codes below for your reference.
Public Start_Date As Date
Start_Date = Worksheets("Sheet_Input").Cells("D", 1).Value
Public End_Date As Date
End_Date = Worksheets("Sheet_Input").Cells("F", 1).Value
Public Total_Date As Integer
Total_Date = Worksheets("Sheet_Input").Cells("H", 1).Value
Public aAverage As Integer
aAverage = Worksheets("Sheet_Input").Cells("B", 3).Value
Public bAverage As Integer
bAverage = Worksheets("Sheet_Input").Cells("D", 3).Value
View 3 Replies
View Related
Aug 14, 2012
I have two issues in SumProduct formula.
1.Formula Def.Cells(c, d).Formula = "=SUMPRODUCT(--(Severity =" & """" & severity_3 & """" & "),
--(OFFSET(Severity,0,4)=" & """" & module & """" & "))"
Where severity_3= Shipping
There are many fields like Shipping, Shipping-UI, Shipping-BE.
I want the formula to consider all the above when severity_3= Shipping and take the count & not only consider Shipping(Shipping*) .
2.Formula test = "0 - Unclassified" Or "Alpha Testing" Or "Beta Testing" Or "Functional - Interface" Or "Functional - Report" Or "Regression Testing" Or "Requirement Review"
when i use the above formula , it is displaying the message "Run-time error'13':Type mismatch There are spaces in the criteria i have used but i am not sure how to resolve this
View 1 Replies
View Related
Dec 25, 2007
I have a smattering of experience within various programming languages, but am still coming to terms with the basics of VBA. I am trying to declare a global variable, assign it a value, then use that global variable. Within 'ThisWorkbook' I have the following...
Public myText As String
Private Sub Workbook_Open()
myText = "Hi There"
End Sub
...and in the Microsoft Worksheet Object Sheet1 (Sheet1) I have...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox myText
End Sub
Now, my understanding of this code is that upon opening the workbook, myText variable will be declared, and then assigned the value "Hi There". Then, once I have clicked anywhere on Sheet1, a message box will appear stating "Hi There". Problem is, the message box is blank. This is all fine, except I want the message box to state "Hi There". What am I doing wrong? Is the variable declared (publically) correctly? Am I assigning the public variable the value correctly?
Am I referencing the public variable correctly in the Worksheet_SelectionChange procedure correctly?
View 3 Replies
View Related
Aug 3, 2006
I defined two custom buttons in Workbook_Open() function:
Private Sub Workbook_Open()
Dim cb As CommandBar
Dim ctrlInit As CommandBarControl
Dim ctrlComp As CommandBarControl
On Error Resume Next
Application.CommandBars("Project").Delete
On Error Goto 0
Set cb = Application.CommandBars.Add( Name:="Project", temporary:=True)
With cb
.Visible = True
Set ctrlInit = .Controls.Add(Type:=msoControlButton, temporary:=True)
With ctrlInit
.FaceId = 68
.Style = msoButtonIcon
.Caption = "Retrieve Template Types"............
In Module1 I must to turn ".enable" properties to Turn, but Excel don't recognize these variables there.
View 2 Replies
View Related
Oct 16, 2006
I created in module custom toolbar with a button, see the
Option Explicit
Public ctrlGUI As CommandBarControl
Sub CreateToolbar()
Dim cb As CommandBar
On Error Resume Next
Application.CommandBars("Cust").Delete
On Error Goto 0
Set cb = Application.CommandBars.Add( Name:="Cust", temporary:=True)
With cb
.Visible = True
Set ctrlGUI = .Controls.Add(Type:=msoControlButton, temporary:=True)
With ctrlGUI
.FaceId = 3205
.Style = msoButtonIcon .............
View 5 Replies
View Related
Feb 12, 2008
Setting the Public Statement does not work. These are my codes:
Option Explicit
Public Ankis_makro As Boolean
Sub Makro2_Ankis_veckor()
On Error Resume Next
Application. ScreenUpdating = False
Sheets("Listan").Activate
Selection. AutoFilter Field:=1
Selection.AutoFilter Field:=2
Selection.AutoFilter Field:=3
Selection.AutoFilter Field:=4
Selection.AutoFilter Field:=5
Selection.AutoFilter Field:=6
Selection.AutoFilter Field:=7
Selection.AutoFilter Field:=8
Selection.AutoFilter Field:=9
Range(Range("J2"), Range("I65536").End(xlUp).Offset(0, 1)).FormulaR1C1 = "=TEXT(RC[-9],""ÅÅÅÅ"")" ...................
What ever I do I can't get the Ankis_makro set to True.
View 2 Replies
View Related
May 26, 2014
I have declared one variable outside of Sub. And in a Sub I gave it a value. Then I run a macro, which should take that value and print it to the cell. However, it is printing a blank cell, not even "0" (I run a macro and anything in that cell is removed).
[Code].....
and then my macro:
[Code]......
How to make so macro see my variable declared outside of this macro?
View 8 Replies
View Related
Jul 8, 2008
I have a number of userforms that all reference the same worksheet. I wanted to create a public variable for that worksheet so I don't have to keep referencing it in each userform/commandbuttons etc. So I inserted a module and placed this declaration.
Option Explicit
Public MySh As Worksheet
Set MySh = ActiveWorkbook.Sheets("sheetName")
And I referenced it in a UserForm commandbutton
Private Sub CommandButton2_Click()
MySh.Range("i4") = Me.ComboBox1
MySh.Range("i5") = Me.TextBox1
End Sub
View 9 Replies
View Related
Oct 28, 2009
explain the difference between declaring a variable as global or as public. Aren't they both available to the entire project including forms?
View 9 Replies
View Related
Feb 22, 2010
I've got several proccedures all inside a single module where I declare 7 variables as Public before the start of the first proccedure.
My structure is something like this:
Public v1 as Variant
Public v2 as Variant
.
.
.
Public v7 as Variant
Sub Proc1
Call Proc2 (defines v2 as name of a file opened by a user; inside Proc1)
Call Proc3 (inside Proc1)
Call Proc4 (inside Proc3)
Call Proc5 (inside Proc5)
End Sub
Sub Proc6
Here I use v2
call Proc7 (inside Proc6)
End Sub
Variable v2 is uniquely named and is only used to hold a file's name so that I can call upon that file in various procedures. However, after Proc1 ends, Proc 1 retains the variable names of some of my Public variables, but not all, including v2, which I need!
I've tried going through this all step-by-step, no joy/nothing apparently obvious. I've also turned on Tools-Options-General-Notify Before State Loss but this isn't generating anything either.
View 9 Replies
View Related
Feb 10, 2007
I have a few lines of code as follows:
Private Sub Workbook_Open()
fileName = ThisWorkbook. Name
fileLocation = ThisWorkbook.Path
Dim strFound As Boolean, pos As Long
If Not fileName = "" Then
strFound = False
pos = 1
Do While strFound = False
stringFound = Mid(fileName, pos, 1)
If stringFound = "-" Then
productName = Mid(fileName, 1, pos - 1)
MsgBox "Name of Product Is: " & productName
strFound = True
Else
pos = pos + 1
End If
Loop
End If
End Sub
It would return "Test" if the file's name is "Test-Part1.xls". I then declare the productName variable as Public (Public productName As String) in one of the modules. But the productName only holds the value for a certain amount of time. After a while, it's empty.
View 4 Replies
View Related
Feb 17, 2007
I read the Excel VBA Variables Scope and Lifetime info, but I still don't understand b/c of the statement "It's value is retained unless the Workbook closes or the End Statement is used." Which "End Statement"?
I want to fill an array in a form module, then later use those values in a different form module. If I declare the public variable in a standard module, I know any of the modules can access it, but will it retain its value after form1 is no longer running?
If not, do I need to involve a class somehow? I know nothing about classes except that their variables retain their values as long as any of the members is running.
View 9 Replies
View Related
Aug 20, 2014
I have a userform that is called within a sub in module. I declared a public string, "divisonb", in the module. When the userform's ok button is clicked, I define the public string through a "select case" method.
At the end of the private sub for the ok button click I have this:
[Code].....
a message box comes up with the correct string for divisonb. After the sub ends and it returns to the module I have the following:
[Code] ....
When this message box pops up, it is blank. Somehow, divisonb was redifined as blank within that 2 lines of code. All my other public strings are returned to the module with their correct values.
View 3 Replies
View Related
Mar 6, 2010
How can I make a dynamic array public?
I have to Dim it so it stays valid only inside the sub.
Public MyVar()
Sub test1()
Dim MyVar()
ReDim MyVar(1 To 4)
For x = 1 To 4
MyVar(x) = "ffffff"
Next x
End Sub
Sub test2()
For x = 1 To 4
Range("A" & x) = MyVar(x)
Next x
End Sub
Error I get is 'MyVar(x) = Subscript out of range'
View 9 Replies
View Related
Feb 10, 2007
I want to define a varible named MonthEnd that I will use in more than one project. In a normal example the variable would look like this:
Dim MonthEnd As String
MonthEnd = Format(Sheet1.Range("C3"), "MMYY")
The problem is that I will be using this more than one time so I figured I could define this a Public constant like
Public Const MontEnd As String = Format(Sheet1.Range("C3"), "MMYY")
View 3 Replies
View Related
Mar 14, 2008
I've got the following code, setting up and initializing a Public variable for a sheet, but I've found that it's has no value after the Auto_Open is run,
though it works fine in the procedure Auto_Open calls.
I can get around it by reSetting it in the module I need it in,
but that kind of defeats the purpose of having a Public variable:
code in Module1
Public parSheet As Worksheet
Sub Auto_Open()
Set parSheet = Worksheets("Params")
Call Initialization.BuySellInit
End Sub
View 8 Replies
View Related
Jan 29, 2009
I have an Excel 07 spreadsheet containing multiple tabs, modules, and userforms.
In Module1, I have a public variable declared as boolean - will call it X. When X is selected from a combo box in Userform 1, X is set to TRUE. However I've noticed that when the user enters Userform 2, X is somehow set to FALSE.
I can't set it back to TRUE at that point because more often than not, it SHOULD be FALSE, thereby sending the macro down a different path. Any idea how I can retain the "TRUE" value for X. I've tried changing the Public Variables to Global, but am still having the problem.
View 2 Replies
View Related
Oct 15, 2008
I am in the process of reformatting an excel workbook to act as a review tool for different factors in a process. Part of my redesign includes the use of coding that creates different cell colors based upon the cell contents. The new workbooks will be used to handle existing data for this year. I have developed a process macro to open an existing workbook and copy and transfer the original data worksheet into the newly formatted workbook. The data gets transferred to the new worksheet and the resulting workbook is renamed and saved, Heres'' the rub... the newly saved workbook is missing all of the coding for the worksheets... apparently this is a MS bug.
Has anyone figured a workaround for this. One thought I had is to open both( new and old )workbooks and rather than move/copy , i would transfer the data using cell references.
View 5 Replies
View Related
Aug 14, 2012
I am trying to bring up a form after clicking the "ok" button on another form. Both forms are defined in my public sub, so basically, the module creates both forms, calls the first form, and hides the second form. Then the first form hides itself and shows the second form. However, when second form is defined using "dim", the first form can't find it to show it, and when I make the second form "public" in my public sub, I get the error that it is an invalid attribute or function.
Here is the code from my sub that applies to this error:
Code:
Public Sub PutInEngine()
Dim InputForm As New FrmInputs
InputForm.Show
[Code].....
View 3 Replies
View Related
Jan 2, 2007
The following is not working for me with an overflow #6 error.
Sub multiplythis()
Dim highnumber ' As Long, I've tried Double, Long and now Variant
highnumber = 50 * 250 * 300 * 400 * 20
End Sub
View 9 Replies
View Related
Nov 20, 2008
I am getting "Duplicate Declaration" error while running my code. Cant post entire code as its too long but i am pasting the problem line.
View 12 Replies
View Related
Apr 23, 2012
I have some code that runs a mouse event and keeps my computer active when I'm away. It works on my 32-bit computer, but doesn't work on my 64-bit. What is the correct syntax to have it work on a 64-bit?
I get the error: "declare stmts must be updated for 64 bit and marked with PtrSafe attribute."
BTW, I already tried changing "user32" to "user64", that didn't work.
Code follows:
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Dim TimerActive As Boolean
Sub KeepWindowsActive()
[code].....
using win 7.
View 2 Replies
View Related
Jun 21, 2008
I suspect the array variables declaration in the following example is the problem.
The array Function MyRoots() incorrectly returns 0.0 results to cells I11:J13.
cell B8::3
cell B9: myTrue
cells B11:C14 numerical values
cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
Function MyRoots (a, m As Integer, polish As String)
ReDim a(m + 1, 2) As Double
ReDim roots(m, 2) As Double
Dim j As Integer, its As Integer
Dim x(2) As Double
ReDim ad(m + 1, 2) As Double
......................................my code............
For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j
......................................my code............
Call Laguer (ad, j, x, its)
......................................my code............
roots(j, 1) = x(1)
roots(j, 2) = x(2)
......................................my code............
MyRoots = roots
End Function
Sub Laguer (a, m, x, its)
Dim x1(2) As Double
......................................my code.......................................
x(1) = x1(1)
x(2) = x1(2)
......................................my code............
Exit Sub
End Sub
View 9 Replies
View Related
Aug 3, 2009
I have a formula that hides rows within a range for a sheet.
View 2 Replies
View Related
Nov 19, 2009
I am using the format of using variables for declaring file paths and document names in VBA however I want to make sure I am doing it properly...
Dim Server, VersionName As String
Server = Worksheets("Calculation Matrix").Range("CalculationMatrix_Server").Value
VersionName = Worksheets("Calculation Matrix").Range("CalculationMatrix_VersionName").Value
ActiveWorkbook.SaveAs Filename:="" & Server & "uploadcomplete" & VersionName & "", FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
Should these variables be declared as String?
View 9 Replies
View Related
Jun 3, 2007
I am trying to run a VB script that will copy data from 3 worksheets and past it into 3 New worksheets, sort that data into alpha order then delete all rows containing Zeros that may be found in Column B, Then copy that data from the 3 New worksheet back into the original 3 worksheets.
The copying and sorting of the data from one sheet to another is easy. The problem is trying to delete the zeros found in column B once its been copied over into the 3 New worksheets.
The script I am using to delete any Zeros found in Column B is below.
If I try to use this script more then once, so that I can delete all the zeros found in column B on all the New worksheets I get this error “Duplicate declaration in current script”
Is there a way for me to refer back to this script instead of me trying to past it more then once within the VB script.
Application. ScreenUpdating = False
Application.Calculation = xlCalculationManual
Const StartRow As Long = 1 'Row to Start looking at
Dim StopRow As Long
Dim Col As Long
Col = ActiveCell.Column
StopRow = Cells(Rows.Count, Col).End(xlUp).Row
Dim cnt As Long
For cnt = StopRow To StartRow Step -1
If Not IsEmpty(Cells(cnt, Col)) Then
If IsNumeric(Cells(cnt, Col)) Then
If Cells(cnt, Col) = 0 Then Rows(cnt).Delete
End If
End If
Next cnt
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Please see attached workbook. You will understand it better once you see the workbook and the VB script in it.
View 6 Replies
View Related
Jun 28, 2009
I get an error: Duplicate declaration in current scope? The formula calculates the compound annual growth rate.
View 5 Replies
View Related
Dec 12, 2011
I have a worksheet used for scheduling. When a members time is updated, it updates the counting cell for that time by subtracting 1 (thats simplified, the forumla is more complex than that).
I have five teams and five workbooks for each team to do it's own scheduling. In just ONE workbook, the Worksheet_Change() event has stopped executing. It's fine in the others. I renamed the workbook to archive it then put another workbook in it's place and now that one works just fine.
The workbook that I've archived, I hate doing that not knowing what would cause the Worksheet_Change() to stop being recognized. There is no code on the sheet or related to the sheet that would stop it or cause events to be cancelled.
I wanted to know if there is some secret keystroke combination that may have been inadvertently clicked that would cause events to firing or stop being recognized?
View 4 Replies
View Related