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 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'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.
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.
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 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.
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.
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
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 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.
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.
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.
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'm only starting to get to grips with arrays. I have what I consider to be a lot of data that I need to 'cut' into separate workbooks. I have written some code that does this by simply looping through each line, 250k+, checking against a variable and copying the row into a separate sheet. This took longer than it would have doing it manually. It was suggested to me that I use arrays to speed up the process. I have managed to store the test data into an array but am struggling to find a way to loop through and pull out an entire 'row' from the array based on a variable. I have looked for 2 days in various places to find some way to loop through the data held in the array, but to no avail.
That code will appear here from about 8am GMT tomorrow. I know that once I've cracked this I'm on the road to some very significant time saving and comprehensive report writing.
i need to replicate what i did using array formulas with VBA macro (array variable). to make things clear and simple i created an example for illustration only. look at it & u will find what i did & what i need to do ,much of it in writing so that i accurately describe my problem. attached is my example
I am trying to assign a value to a variable based on the value of another variable. I have about 15 items each with about a dozen specifications to them. I'd like to compare the specifications of "MyItem" to those of "OtherItems". Problem is MyItem changes constantly, and I don't want to hardcode the values for all of these items for every possible value of MyItem. Plus, I would have to create new code every time there is a new item. It just seems like REALLY bad coding. Please see below:
Sub MyItemHardcoded Dim MyItem As String Dim OtherItems As String
Dim MyItemSpec1 As Long Dim MyItemSpec2 As Long Dim MyItemSpec3 As Long Dim MyItemSpec4 As Long Dim OtherItemsSpec1 As Long Dim OtherItemsSpec2 As Long Dim OtherItemsSpec3 As Long Dim OtherItemsSpec4 As Long
I was trying to use a dynamic variable name to cut through this. Sub DynamicVariable Dim MyItem As String Dim OtherItems As String
Dim ThisItemSpec1 As Long Dim ThisItemSpec2 As Long Dim ThisItemSpec3 As Long Dim ThisItemSpec4 As Long Dim OtherItemsSpec1 As Long Dim OtherItemsSpec2 As Long Dim OtherItemsSpec3 As Long Dim OtherItemsSpec4 As Long Dim ItemNames(1 To ItemCount)
I tried using the evaluate method, but it returns an error when the expression evaluates to text - even if that text is a variable name. I've tried some off-the-wall ideas as well, but nothing seems to work. There has to be an easy way to do this. I'm just totally missing it.
I am trying to count the number of items in an array, but want to protect against someone inserting a row and changing the position of the first item destined for the array. The first item is in F25.
Presently I calculate a worksheet and store the result in any array the size of which is determined at the start. I then enter all the results in a worksheet and get the max and min values and numerous other data. I am trying to find a way to get the max and min values without having to enter the data to speed the routine up.
this is the code that puts the results into a worksheet.
Code:
Sub Recalculate() 'Recalculates the WorkBook Dim Calc_ Worksheets(startSht).Select Set Output = Application.InputBox(prompt:="Please select the 1st Output Range.", Title:="SPECIFY RANGE", Type:=8) Set OutPutLabel = Application.InputBox(prompt:="Please select Label for
I have a workbook that launches a form for a user to confirm (or un-confirm) some data retrieved from an Access database. I would like to put the "confirming" aspect of the process into a checkbox array. The recordset can return anywhere from 1 to around 12 records. I would like to add the checkboxes on the fly based on how many records were returned. Anyone have some sample code to show me how to add these to the form at run time?
I am trying to sqaure every element in a dynamic array and display the result . I donot understand how can I select the value in the cell using VBA?
Dim Y as variant, d() as double, i as long, j as long, rows as double, cols as double Set Y = Application.InputBox("select the matrix: ", Type:=8) Rows = UBound(Y) Cols = UBound(Y, 2) ReDim d(1 To Rows, 1 To Cols) for i = cols d(1,i) = ______==> How do I select the value of element in that particular cell and how do I sqaure it? I know cells(rowindex, columnindex) is used to select a particular cell but If I have a large array it would be difficult to go cell by cell and sqaure it.