Dynamic Array As Public Variable

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


ADVERTISEMENT

Set Public Variable

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

Macro Can't See Public Variable?

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

Keep Getting Error With Public Variable

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

Global Vs. Public Variable

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

Public Variable Not Being Retained

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

Scope Of Public Variable

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

Public Variable Not Work

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

How To Dim Array And Its Values As Public

Apr 3, 2014

I am trying to declare a public (or global) array and it's values so I don't have to keep dimensioning it for each function I create.

I was trying things such as:

Public whatever(10) as string

whatever(0) = "something"
whatever(1) = "somethin else"
....

And tried creating an initialize macro in "ThisWorkbook".

View 11 Replies View Related

Public Array Not Avaiable In Sub?

Jul 17, 2007

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)?

View 2 Replies View Related

Userform Not Passing Through 1 Particular Public Variable?

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

Public Variable Producing Error

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

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 View Related

Public Variable Loosing Reference

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

Public Array Not Clearing Between Runs

May 19, 2008

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?

View 9 Replies View Related

Losing Public Variable When Submitting Userform

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

Declaring Public 2 Dimensional Array In Userform

Apr 22, 2014

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

[code]....

View 1 Replies View Related

Subscript Out Of Range Using Public Array Variables

Aug 18, 2006

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?

View 6 Replies View Related

How To Populate Public Array With Static Text Values

Aug 29, 2013

I understand how to create an array within a routine:

CurOptions = Array("M1", "M3", "M3 DSR", "L1", "L2", "CSA")

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.

It should be just one line.

View 1 Replies View Related

Workaround For Public Variable Bug: Creates Different Cell Colors Based Upon The Cell Contents

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

Call Public Object From Public Sub?

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

VBA To Output From Array Based On Variable In One Element Of Array

May 2, 2013

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.

View 9 Replies View Related

Using Array Variable Instead Of Array Formula

Jun 30, 2006

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

View 4 Replies View Related

Dynamic Variable Name

Feb 16, 2007

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.

View 9 Replies View Related

Dynamic Array

Apr 2, 2007

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.

TickerNum = Sheets("Dashboard").Range(Range("f25"), Range("f25").End(xlDown)).Count

Is there a way to ensure that it always picks up the value currently in f25.

View 9 Replies View Related

Get Max And Min Values From Dynamic Array

May 16, 2014

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

[Code]....

View 1 Replies View Related

Dynamic Checkbox Array

Aug 10, 2006

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?

View 9 Replies View Related

Combo Box Dynamic Range Variable

Jul 22, 2008

If I had a combo box that look at 10 item in a range A1:A10 and the output value was in B1

Say a user added a new Item in A11. How could I get the range of the combo box to automatically extend to A11 or A12 if two new items are added.

I am trying to avoid having the input range as A:A and having lots of empty values?

View 9 Replies View Related

Dynamic Array In Index Function?

Oct 1, 2013

I'm trying to create an index array that resizes its array starting location.

{=IFERROR(INDEX($V$17:$AU$17,1,MATCH(TRUE,V18:AU18

View 3 Replies View Related

Sqaure Every Element In A Dynamic Array

Nov 11, 2009

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.

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved