Why Declare The Variables

Sep 17, 2009

I know that we should declare all variables at the beginning of a subroutine, in fact I'm told it's good practice to use Option explicit to 'force' variables to be declared, my question is why?

If I don't declare a variable the routine still seems to work OK so what is the downside of not declaring them upfront? Is it just for neatness or common practice or is there another reason?

View 2 Replies


ADVERTISEMENT

Which Variables To Declare

Dec 10, 2008

As a rule which variables should I declare?

Sub checkPO()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim Rng As Range, MainSheet As Worksheet, item As Variant ', CurrWidth As Integer, UnitWidth As Integer

Set MainSheet = Sheets("Orders To Chase")
Set Rng = MainSheet.Range("B5", MainSheet.Range("B60000").End(xlUp))
totalqueries = MainSheet.Range("B5", MainSheet.Range("B60000").End(xlUp)).Cells.Count

UnitWidth = 282 / totalqueries

With progbar '\displays the please wait box
.Show False

.prog.Width = 0 '\ updates progress bar
.Repaint
End With

If I declare the variable my progress bar goes off screen. remove it and its back to within its box.

I haven't declared any of these variables either. Does this matter?

POnum = .Range("G5").Value
Supplier = .Range("A5").Value
Req = .Range("B5").Value
Ordered = .Range("E5").Value

View 9 Replies View Related

Declare Variables In Classes?

Apr 14, 2014

Why it seems not possible to declare variables in Class modules like so:

[Code] ....

View 3 Replies View Related

Declare Set Of Valid Variables Acceptable?

Oct 25, 2013

i need to set the range of variables that user can add to the range.

For Example:

AA_*
BB_*
CC_*
ABCD_*

so we accept variables STARTING with AA_ OR BB_ OR CC_ OR ABCD_. If the user enters sth else, then I want to disable the "Enter" key. (If the Cell is Empty than it is also OK!!)

If disabling the Enter key is not possible then maybe i can use Conditioning Formatting? But the question is then if i can use for single condition OR statement.

View 1 Replies View Related

Match And Copy: Declare The Variables Correctly And Have The Operations Done In A Series Of Do/ Loops Or For/Nexts

Feb 2, 2007

I have a series of operations to carry out and, while I can do the code for each individual one, how to declare the variables correctly and have the operations done in a series of Do/ Loops or For/Nexts. Especially the declaring of named ranges as variables. Also a bit uncertain of the best way to find and coy the match. I have attached a simplified version of the workbook, with explanations on it.

Basically what I need to do is loop through a series of named ranges and then loop through the names in each, match each name with a name in a master list (with a flag as an image), add an e-mail hyperlink to that flagged name and copy both to a new cell.

View 2 Replies View Related

Constant Variables - Declare Name In Separate UserForm As Constant

May 8, 2012

I have a userForm (Form1) that contains a persons name that I would like to reference in a separate UserForm (Form2). In the separate UserForm (Form2) I need to reference this persons name many times, so I was wondering if there was a was to declare this name in the separate UserForm (Form2) as a constant. Only thing is that a constant, to the best of my knowledge, must be an expression and not a variable. Mainly, I'm trying to avoid declaring the myName variable in each Sub within Form2, which it will be needed for a ton of Sub's.

Code for Form2: Const myName As String = Form1.txtName.Value

View 5 Replies View Related

Declare Multiple Subs On 1 Line Just Like Multiple Variables

May 25, 2014

I'm making a Form with multiple pages and on every page there are atleast 36 Textboxes.

[Amount] [Description] [price]

I want to run a small sub updating the price on changing the amount. My code now looks like this:

Code:
Private Sub TextBox1_Change()
Call UpdatePrice
End Sub

Private Sub TextBox2_Change()
Call UpdatePrice
End Sub

Private Sub TextBox3_Change()
Call UpdatePrice
End Sub

I have to do this on 8 pages, 24 to 36 times, which makes the code extremely long. Is there a better way to do this?

Something like:

Code:
Private Sub TextBox1_Change(), TextBox2_Change() etc...
Call UpdatePrice
End sub

View 2 Replies View Related

Declare Constant

May 15, 2007

what does the symbol # means in VBA? (but I couldn't put the # in the subject of my message )

I'm trying to understand someone's code... at some point he wrote:
sum_LU_Area = 0#

I thought that the # was used to declare a constant but I'm not too sure because in his code earlier he declared
Public sum_LU_Area As Double

Beside, sum_LU_Area is calculated somewhere further in the program.

View 7 Replies View Related

Declare And Initialize An Array?

Mar 23, 2009

I am running into the error, "Procedure too large". I know I need to break the range down into Arrays, so how can I hard code the values into an array? I cannot find an example to follow. Ranges: D:E,K:L,O:P,....etc. I know I can break the rows up into an array too, but one thing at a time. Here is an example of the range for D:E.

View 2 Replies View Related

VBA Declare Data As Array

Mar 4, 2013

I have a column a1:A150 which includes some Data. I now want to declare these data as an array.

Afterwards I want from cell b50 to copy int the value from cell a1 and copying the next value from the array (cell a2) into cell b51 and so on until cell b200.

I want to do it by creating an array and not just by usual Excel formulas.

View 3 Replies View Related

Declare And Set Worksheets In A Loop

Jan 31, 2007

I want to declare and 'Set' a number of worksheets for later use. Like this ...

View 9 Replies View Related

Declare An Array With Variable Size

May 7, 2009

I need to create an array with a variable as it size For instance:

View 2 Replies View Related

Declare Private Constant As Long?

Dec 27, 2012

I am trying to declare lngLr as Long and Constant. But it's buggin out on me. Is this the correct way to do it?

Code:
Private Const lngLr As Long = ".Cells(Rows.Count, 1).End(xlUp).Row"
Sub calculate_active_employees_sheet_years_of_service_w_Oasis()
Application.ScreenUpdating = True

[Code].....

View 4 Replies View Related

Declare Name Of Object From Cell Value Using Dim Statement

Jun 15, 2013

I have an object in a form and I need to get the name of the object from cell value or I need to declare it using Dim statement..

something like this..

NAcctF.Visible = False

where I need to declare like this..

itm= range("A1").value & "F"
itm.Visible = False

because, except "F" at last remaining part keep changing..

View 3 Replies View Related

Declare & Initialize A Variable As Global Using Vba

Feb 10, 2009

how to declare&initialize a variable as Global in vba?

I have a variable ,
listGroup=Array("aaa","bbb","ccc")
now i am using this variable for 3 different functions. so what i am doing is wrote the same code to 3 functions, so how i can declare&initialize this variable as global and access to all functions.

View 9 Replies View Related

Declare String As Array Index

Mar 28, 2007

is it possible to declare an array or anyother datatype like the following in VBA?


Dim myArray() As Integer
myArray("A") = 0
myArray("Test") = 0
myArray("G") = 0

after checking some conditions i need to change the corresponding values too, like the following,


If mycondition = True Then
myArray("Test") = 1
End If

View 9 Replies View Related

Declare Global Variable In Application Level

Sep 17, 2009

I need to declare a global variable in Application level not Module level.

I have declare a variable in Module1 and then Module2 with same name then complile it and get success. That means there has two variable with same name in different Module. I think this was not a proper global variable declaration by which I can allow to declare only one variable in all Module, Class every where. I have used code as below -

View 9 Replies View Related

Declare 2 Dimensional Array With Integers And Strings

Jun 2, 2013

I need to declare a two dimensional array that will return the Row and the cell's string value. My only solution is to return the row as a string like this.

Public GlobalArray(2, 100) As String

How to get more control than this? Maybe I should just declare it as a variant but this might open it up for problems later.

View 3 Replies View Related

Declare & Define Variable As Date Range

Nov 13, 2006

I'm fairly new to VBA and I need to define a variable as a date range
i.e. Period1 = 1Apr2006 to 29Apr2006, Period2= 30Apr2006 to 27May2006 etc

View 9 Replies View Related

Declare A Const Array Of String Type In VBA

Jul 28, 2006

How do I declare a const array of string type in VBA? for example I want to say
Const myStringArray(4) as String = string1, string2, string3, string4

View 5 Replies View Related

Unzip Code - Works Without Variables, Breaks With Variables...

Feb 5, 2009

Unzip Code - Works without Variables, Breaks with Variables.... This has been driving me bananas...

I have the

View 2 Replies View Related

Declare A Sheets(1).Buttons("CommandButton1")

May 13, 2006

I have in sheets(1), a CommandButton. attached from the "forms toolbar". msgbox Sheets(1).Buttons("CommandButton1").Caption. shows me the caption of this commandbutton. I am using this commandbutton so often so... I would like to refer to this button say:

Dim CM As control
cm=Sheets(1).Buttons("CommandButton1")
msgbox cm.caption

* * *
But i am not able to know the declaration I have to use, I mean

Dim CM As shape??
Dim CM As As CommandBar??

View 3 Replies View Related

Function Back Variables: Function Give Two Or More Output Variables

Jul 27, 2006

Can a Function give two or more output variables. e.g.

Sub a()
x = 5
result = Y(x)
End Sub

Function Y (x As Integer) As Integer
Dim B
B = ... * x
Y = ... * B

this will give back Y as a result. But if I want to get 2 or more output variables (let's say I need to get also B into sub) from one function, how should I do that?
I need this because function works with large matrix and I want to extract some values appeared in between.

View 2 Replies View Related

Defining Variables Using Variables

Apr 27, 2006

I'm trying to loop through a range in excel from access, checking where the titles (in Excel row 1) match with the fields (in a recordset in Access that is passed to the function) - and where they do, I want to dimension a variable to hold the column number - I'm not sure it's possible, but I'd be interested to know either way. The line I'm asking about is at the bottom of the code - the rest of the code is just to give context...

Sub ImportGeneric(rsImported As ADODB.Recordset, rsConfirmed As ADODB.Recordset)
Dim fd As FileDialog
Dim xl As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Worksheet
Dim iFilePicked As Integer
Dim strFilePath As String
fd.Filters.clear
fd.Filters.Add "Excel files", "*.xls"
fd.ButtonName = "Select"
iFilePicked = fd.Show
If iFilePicked = -1 Then
strFilePath = fd.SelectedItems(1)
Else ..................

View 3 Replies View Related

Erase Variables All The Variables Or To Set The Value Of Them To "0"

Jan 16, 2007

i have a "problem" to empty / reset my variables. I defined them as vHour1_KW2 where the "1" is from 1 to 21 and the "2" starts from 1 to 53. Now I want to erase all of this variables or to set the value of them to "0".
At moment I use following

vHour1_KW1 = 0
vHour1_KW2 = 0
...
vHour1_KW53 = 0

vHour2_KW1 = 0
vHour2_KW2 = 0
...
vHour2_KW53 = 0

until...............................

View 3 Replies View Related

Use 2 IF Variables?

Apr 9, 2014

I'm having a hard time understanding how to accomplish what seems to be a simple result.

I need to display one of two words, based on whether or not a pair of values are above or below the criteria.

FIRST:
IF H6 is greater than 5000
AND
IF AB6 is greater than 25000
Display: Double

SECOND:
IF H6 is less than 5000
AND
IF AB6 is greater than 25000
Display: Single

There is no 3rd scenario, even though logically there should be.

View 13 Replies View Related

Put Variables In URL

Apr 1, 2008

I am trying to put variables in this URL which is related to yahoo finance :

.Name="hp?s=NVDA&a=00&b=31&c=2001&d=11&e=29&f=2006&g=m&y=0"

I defined at the beginning

Dim start_date As Date
Dim end_date As Date
Dim datestring As Variant
start_date = #1/31/2001#
end_date = #11/26/2006#
and put them in datestring

I passed the datestring to a new sub which has the URL:

.Name="hp?s=NVDA&a=00&b=31&c=2001&d=11&e=29&f=2006&g=m&y=0"

So, my question is, i tried to put the (1/31/2001) and (26/11/2007) which is in the above URL which is separated in variables and the URL remain the same

View 11 Replies View Related

COUNTIF With Two Variables

Jul 6, 2012

I am trying to use COUNTIF with two critera. If this isn't possible is there any other way possible of doing this in a range of cells.

What I am trying to do is show the amount of students in a year group who spend x amount of hours on the internet and have a target grade (for example) of Lvl 4

I have been trying use a formula along the lines of =COUNTIF (Q5yr7, "0- 1Hour", Q12yr7, "4")

View 9 Replies View Related

Vlookup With 3 Different Variables?

Jun 26, 2014

vlookup with 3 different variables, for example cells k4 k5 and k6 can be changed to give different variables. Is it possible to have a vlookup function in cell k9 which returns the correct % when the 3 variables are chosen. example, blue boat 48 would return %value of 21%

View 2 Replies View Related

COUNTIF With 2 Variables?

Aug 1, 2014

I am trying to count the status and type of some work so:

Column A would contain the status of the work e.g. open, in progress, closed etc.
Column B would contain the department: ict, development, operations, etc.

I want to do a summary that shows: How many are in ICT are open, closed etc.

I can do a countif to get the total open, in progress etc or total number of ICT jobs but not ICT In progress.

View 1 Replies View Related







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