I have a public array in a module in my spreadsheet. The array is an array of a class type (not integers, strings, etc). When I run the macro that uses the array for processing the first time, everything seems to work properly. However, if I run the macro a second time WITHOUT getting out of Excel and coming back in, it doesn't come back with the proper results. It seems like either the array isn't being cleared out properly between the macro runs, OR that something in the macro is overwriting memory. Is there a way to make sure the array is being cleared out between runs of the macro?
I've got two public dynamic arrays being declared in a module attached to my workbook. I then have a sub that runs off a userform in that workbook which populates and redims the arrays preserving the data. Next I activate a PowerPoint application and insert some tables into which I want to put the data in the arrays but when I refer to the arrays (admittedly in a Sub called from the first Sub) I'm getting a 'subscript out of range error'. I can print out the data before I call the second sub and it's fine. I've worked with public variables before and never had this problem. Do I have to call the public variable differently if I'm curretnly in a different application (i.e. PowerPoint)?
i'm trying to develop an array that is populated when a userform is activated. The userform has a command button that when pressed, will cross check the information filled out in the userform with the entries in the array. If there are no matches then the array is ReDim and the new information is added to the array. Once all the entries have been made. The array is then transposed to a sheet titled "Database". My problem is that vba is not allowing me declare a public array.Below is the first part of the code. Which is when the userform is activated.
Private Sub UserForm_Activate() With Sheets("Resources") cbZIP.List = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value cbBED.List = .Range("B2", .Range("B" & Rows.Count).End(xlUp)).Value cbBATH.List = .Range("C2", .Range("C" & Rows.Count).End(xlUp)).Value cbSTABRV.List = .Range("G2", .Range("G" & Rows.Count).End(xlUp)).Value End With
In my VBA project I've declared several public variables as normal (ahead of all procedures) ... Public simolo() As double etc. They are declared in normal modules, and arent declared twice. I set the values in one procedure and then execute a second procedure but, when the variable is encountered in the second procedure, it appears to be empty and I get a "Subscript out of range" error. Clearly, the public variable isn't public since no data is stored in it. What is going on?
But I need the array to be public as the array can hold one of 6 different sets of values so I will declare the array to be only used within the module as:
Dim CurOptions(30)
Later I need to fill in the values manually, they will not be copied in, they need to be provided in a list.
Here's an array I have that is filled with values that come from a worksheet. I'd like to have it so that once this array is full the code that fills it up (see below) gets skipped on subsequent runs of the subroutine. What's a good way to do this? Seems like when I Dim the Array it gets blanked.
Code: 'Fill Bulb Array with House number (j) and Bulb Wattages j = 1 Do While j
1) after I have made an filled an array with data. Dim orders(100000 To 200000) As Double I want to then sum the orders above and below a number, lets say orders>orders(150000) and orders
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
I am running into trouble with too much data accumulating on the clipboard in XL2007, so is there any way to set cutcopymode=false (or some other function) so that the clipboard is automatically cleared after every paste event throuout a macro project without having to restate cutcopymode=false after every paste?
I have a UserForm called Menu. The first line of code behind the OK button says
Menu.Hide
When I run this using F8, it behaves itself and the menu disappears after the code line Menu.Hide. When I click the OK button to run it normally the menu stays on the screen partially hidden behind the next sub menu.
I have 4 listboxs on a Userform populated by rowsource
I have code that will not allow conflicting selections of the list boxs
EG listbox1 conflicts with listbox3 listbox2 conflicts with listbox4
What I am trying to do is when the user clicks on listbox1 and listbox3
I clear the selections from 1 and 3 leaving no highlted blue in the boxs
My code runs it clears the first listbox and not the second
I have tried using boolean to clear them but it is the same thing
here is my code below
If shOptions.Cells(r, c) = "" Then MsgBox "You have selected conflicting Alterations" UserForm1.Controls(h.Name).ListIndex = -1 *this works UserForm1.Controls(lb.Name).ListIndex = -1 * this does not fire Exit Sub End If
If I change the code to UserForm1.Controls(lb.Name).ListIndex = -1 * this works UserForm1.Controls(h.Name).ListIndex = -1 *this does not
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?
I am using a set of arrays in a module. The arryas are set as public as the module that loads the data is called many times from diffrent other modules that then use the data.
How so I reset the array at the start of the module so each level is "Empty" so new data can be loaded with no data left over from the last time the array was used.
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:
I have a combo box which is on a worksheet and it's always active/visible. I need to pass on the selection data to another project. I thought I had successfully converted it to a public variable but it doesn't work.
I've indicated that the Tr variable is public and it does populate with the results of the case select but to no avail. What do I have to do?
Public Tr As String Public Sub ComboBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim a As String Select Case ComboBox1.Value Case "NYF PT"
I am trying to use a public variable but it is not holding the value when I call another sub.
Here is basically what I have:
Public AcctName as Long
Sub Statments() There is much more code in here but it is not relevent AcctName = an account name lets say "frank"
If X XX then Call Title End If
End Sub
It then goes to the title sub and executes the code however it is supposed to write out the value held in acctname but it comes up blank instead of as Frank which is the value it held when it left the statments sub. I am new to this public thing and need any help I can get. Just a note: the spelling is correct and the variable does hold the correct value before title sub is called.
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
I have a VBA application that uses data from a sheet in the excel workbook. It accesses this data with an ADO connection. On workbook open I open this connection--I declare it as public variable so I can continue to use it throughout the session. My problem is that the connection really doesn't ever dissappear. I have the problem that once I close the workbook, the project still exists in the VB Editor. How do I get rid of the connection? I am stuck because I declared it as public? I want to keep it open, rather than opening every time I want to use it, because it is faster to keep the connection open.
My code is below.
Option Explicit 'requires variable declaration 'declare global connection Public cn As ADODB.Connection
Sub OpenDBConnection() 'open db connection 'this happens on workbook open If cn Is Nothing Then Set cn = New ADODB.Connection With cn .Provider = "Microsoft.Jet.OLEDB.4.0" .ConnectionString = "Data Source=" & WCHSBook.FullName & ";" & _ "Extended Properties=""Excel 8.0;HDR=Yes;""" .Open End With End If End Sub
I've recently been experiencing some strange phenomena in relation to public variables. It seems that if I run separate routines in quick enough succession (e.g. press a button twice within a couple of seconds), the memory of the public variable "hangs over" into the second routine.
The routines, however, are completely separate events. And I'm not using userforms or anything like that in order to keep VBA running in the background. In which case it seems that Public variables don't always get set to nothing with the end of the final sub in a routine. Has anyone else experienced anything like this? I was under the impression only Static variables could do this.
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.
I'm programming with multiple workbooks. So public variables in one workbook often reference ranges in another. Anyhow, I have found that, when closing a workbook, unless I nullify all public variables that reference this workbook, it closes in excel but remains visible in the VB Editor. When I open the workbook again, another instance of its VB project appears in the VB Editor..and so on and so on, which is obviously pretty annoying..
I've worked out a way of nullifying (setting equal to nothing) all public variables in a workbook, but it's quite arduous and I haven't tried it yet.. basically, it involves running a code which runs through the procedure which sets these public variables (they are all set in a single procedure), then having the code write a new subroutine setting all variables to nothing, then running this subroutine..
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.
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.