# Can Use Variable Names To Identify / Assign To Other Variables Or Arrays?

Apr 10, 2014

I have set up a number of arrays e.g. DataSetA (1 to 100), DataSetB (1 to 100) etc.

Is it possible to use a string variable (e.g. DataIndentifier) to identify and assign values to certain arrays.

For example, under certain conditions

DataIdentifier would be set to "DataSetA" and under other conditions
DataIdentifier would be set to "DataSetB".

Having done this, I want to assign values such as DataIdentifier(1) = 3.4 or DataIdentifier (10) = 6.2 etc.

This obviously won't work because DataIdentifier is defined as a string not an array. How do I enable the string DataIdentifier to be used as an identifier for the array I want to assign values to? Therefore if DataIdentifier is set to "DataSetA" then effectively I want something like "DataIdentifier(1) = 3.4" to assign 3.4 to the array DataSetA(1).

## Storing Variables As Arrays - Code

Jun 29, 2007

I have the following code for a user-defined function:

Function SeriesSum2(A, B, x, C, D, y, z, Num)

Summation = 0

For i = 1 To Num

Summation = Summation + (((A - B) * (((0.01 * B / (A - B)) _
^ (1 / (y - 1))) ^ i) + B - x) / ((1 + x) ^ i))

Next i

Product = 1

For i = 1 To Num

Product = Product * (1 + ((C - D) * (((0.01 * D / (C - D)) _
^ (1 / (z - 1))) ^ i) + D))

Next i

SeriesSum2 = WorksheetFunction.SumProduct(Summation, Product)

End Function

To simplify, suppose Num = 3, and the three terms of Summation are {3, 5, 9}. Call these S1, S2, S3. Further, suppose the first three terms of Product are {1, 4, 12}. Call these P1, P2, P3.

My desired result of the function is 3*1 + 5*4 + 9*12 = 131. Instead, I'm getting 3*12+5*12+9*12 = 204. That is, the function is returning S1*P3+S2*P3+S3*P3, while I want S1*P1+S2*P2+S3*P3.

Can anyone instruct me as to what I need to do to get my desired result? I'm guessing it has something to do with storing Product as an array, but I'm a very novice VB'er, so I'm not sure.

Of course, if there are any other glaring errors in my code,

## Query Tables DataTypes And ColumnWidths Arrays As Variables

Jul 12, 2011

How do I set the datatypes and columnwidths as variables? I get invalid procedure call or argument. Will import different text files and set dt and cw based on names.

Dim dt As Variant
Dim cw As Variant
dt = "2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2"
cw = "7, 7, 5, 2, 7, 16, 3, 15, 11, 9, 9, 9, 7, 3, 3"

[Code] ........

## Assign Multiple Variables

Feb 16, 2007

It's possible to declare multiple variables on a single line as such:

Dim x, y, z As Integer

Is it possible to assign these variables on a single line instead of:

x = 1
y = 2
z = 3

## How To Assign Formula Containing Values From Variables In VBA

Jan 15, 2009

I want to assign a cell a formula that changes base on variable, example

myVar=3
Cell(1,1).value = "=average(1:{myVar})"

what do i do to make this work instead of {myVar}?

## Assign Name To Range Using Variables And Cell Values

May 23, 2009

For example, I have the numerical value "400" in R1C2 and that value would be used in the name "D400" which is to be assigned to R1C1:R5C2.

The code I have is:

Range("R" & DRow & "C1:R" & DRow + 4 & "C2") = "D" & Cells(DRow, 2).Value

but is throwing up the error:

"Run-time error '1004':
Method 'Range' of object '_Global' failed

DRow increases dependant on other variable data.

Also, am I right to be using R1C1 instead of A1, or can the same be achieved using A1 references? If so, I think I may not be able to see the wood for the trees.

## Assign Formula Values Based On Variables

Jun 6, 2007

I have three variables; Independent variables x & y, and dependent variable z. x & y can take the values 1,2,3 and can equal each other z can also take on values 1,2,3 but can never equal x or y. I tried using if/and statements to solve this (see below), but I would have to do it for each case and it exceeds Excels embedded function limit.

=If(and(B7=1,B10=1),riskduniform(E4:E5),if(and(B7=1,B10=2),3,if(and(B7=1,B10=3),2,if(and(B7=2,B10=2),riskduniform(1,3),if(and(B7=2,B10=1),3,if(and(B7=2,B10=1),3,if(and(B7=3,B10=3),riskduniform(1,2),if(and(B7=3,B10=1),2,if(and(B7=3,B10=2),1)))))))))

## Identify Range Names With Specific Value In Them

Aug 27, 2006

I am developing a macro to select cells within a worksheet based on certain criteria and to make a list of their range names. My approach is to copy the range names to a column of cells using something like the following text:

Sheets("mySheet").Range("myRange").Name.Copy

but I get an error message saying "Object doesn't support this property or method". Why can't I copy a range name, and is there a way to work around this?

## Assign Values To Names In A Dropdown List?

Mar 8, 2005

Is it possible to assign values to names in a list, so that when you validate it as a drop-down list, you can select a name from the drop-down and it's corresponding value will be added to separate cell? Basically, I have survey questions which have five possible responses: Excellent - Very Good - Good - Fair - Poor. We want to assign a value to each, 5 for Excellent, 4 for Very Good and so on, so that when a response is selected from a drop-down, it's value appears in a separate cell (so that we can calculate a total and average score from the selections).

## Assign Names With Specific Numerical Values

Feb 27, 2008

My company is soon to be rolling out a new payment system, and I'd like to be able to track commission for everything I sell. Briefly, we get paid via a percentage of the company's gross profit, and the way in which the company gets paid is tiered. (the cellphone business)

So, If a customer activates a plan thats below \$39.99, we get paid X amount. If it's below \$59.99 we get paid Y amount, and so on and so forth, there are multiple different tiers.

What I want to do is set up a list via data validation so that I can pick what plan they have (via the name of the plan) and have it return a numerical value (ie \$39.99) and also have the ability to pick text messaging, etc as options so that the workbook will add the monthly rates (39.99 + 14.95) and then have it return value for the right priceplan tier.

If this is confusing, I apologize for not being more clear, but attached is an example. The top one is what I'm actually trying to code, but the bottom is completely filled in, so you can get a more clear sense of what I'm trying to do. Honestly I'm not even sure if it can be done.

## Assign Variable For Next Available Row?

Jan 15, 2010

This code finds the second avaliable row from the top of the sheet.

## Assign Names Dynamically Based On Cell Content

Jul 10, 2009

I do have a data set from A1:C8 as follows:-
Brand Name(A1)Sales Value(B1)Margin(C1)Levi's100002000Arrow150002500Allen Solly120001300Hanes80001000John Miller250004500Disney280003200Ray Ban180002100

The above brands are categorized as follows:-
Brand NameProposed CategoryLevi'sConsignmentArrowBrandAllen SollyBrandHanesBrandJohn MillerPrivate LabelDisneyBrandRay BanConcessionaire

I would like to put formula from D1:D8 in such a way so that the Proposed categories are extracted automatically through formulae. This will help in getting the contribution calculated from different categories.
Brand Name(A1)Sales Value(B1)Margin(C1)Category(Desired Result)(D1)Levi's100002000ConsignmentArrow150002500BrandAllen Solly120001300BrandHanes80001000BrandJohn Miller250004500Private LabelDisney280003200BrandRay Ban180002100Concessionaire

## Assign Group Names Based On Cell Values

Feb 19, 2010

I have two tables below. First table contains the list of domain names (given just for example, actual list may contain several more such Domain names) with the group names assigned to them. Second table shows the criteria for assigning them the group names:

Sheet2ABCDEFG1Table 1Table 22Domain NameGroup NameDomain NameGroup NameDomain NameGroup Name3NCG-W3K-SAT14-0DOSSNCG-W3K-EEC-SUN16-3EECEXCEEC4SVL-WIN-SAT08-0LABSNCG-W3K-SUN16-0DOSSDCDC5SVL-WIN-SAT06-0LABSNCG-W3K-TUE01-0DOSSNCGDOSS6NCG-W3K-MON04-0DOSSNCG-WIN-EXC-SUN21-0EECDMZETIS7NCG-W3K-EXC-MON04-3EECNCG-WIN-EXC-SAT16-0EECSVLLABS8NCG-W3K-EXC-MON03-0EECNCG-W3K-X64-DC-SAT14-0DC9NCG-W3K-EXC-TUE01-0EECDMZ-NCG-W3K-X64-SUN12-0ETIS10NCG-W3K-EXC-SUN15-0EECDMZ-NCG-W2K-SUN12-0ETIS11NCG-W3K-X64-EXC-SUN16-0EECNCG-W2K-SUN20-0DOSS12DMZ-NCG-W3K-SUN11-0ETISNCG-W3K-MON03-3DOSS13NCG-W2K-DC-SUN11-3DCSVL-DMZ-WIN-SUN12-3LABS14NCG-W3K-X64-DC-SUN10-3DCSVL-DMZ-WIN-SAT10-0LABS15SVL-DMZ-W2K-SUN12-3LABSDMZ-NCG-W3K-SUN12-0ETIS16NCG-W3K-EXC2-MON03-3EECDMZ-NCG-W3K-SUN10-0ETIS17NCG-W2K-SAT17-0DOSSNCG-W8K-X64-DC-SUN10-4DC18NCG-W3K-X64-SUN10-0DOSSNCG-W3K-TUE01-3DOSS19NCG-W3K-TUE00-0DOSSNCG-W3K-DC-TUE02-0DC20NCG-W3K-X64-SAT15-0DOSSSVL-WIN-SAT11-0LABS21NCG-W3K-WED00-0DOSSSVL-WIN-SAT07-0LABS22NCG-W8K-X64-SUN19-0DOSSNCG-W2K-SUN17-0DOSS23NCG-W3K-EXC-SUN20-0EECNCG-W3K-SUN20-0DOSS24NCG-W3K-ULZ-SUN14-0DOSSNCG-W3K-TUE22-0DOSS25NCG-W3K-TUE03-0DOSSSVL-WIN-SAT07-3LABS26NCG-W3K-ULZ-TUE00-0DOSSNCG-W3K-MON05-3DOSS27NCG-W3K-SUN19-0DOSSNCG-W8K-SUN18-3DOSS28NCG-W3K-EEC-SUN19-0EECNCG-W8K-SUN22-0DOSS29NCG-W3K-EEC-SUN20-3EECNCG-W8K-X64-DC-SUN11-0DC30NCG-W3K-EEC-THU00-0EECNCG-W3K-SUN17-0DOSSExcel 2007

My concern is to automate this process using a VBA code. How can I search for the keyword given in Table 2 in the first column of Table 1 and assign the group names as it is assigned currently, considering the below conditions and criteria.

1) The correct group name should be assigned to the Domain name containing the keyword given as per criteria in Table 2

2) It is possible that the domain name will include more than one keywords given in Table 2. For example Cell number A7 contains both keywords NCG and EXC, in such cases the preferance should be given to EXC and we will assign the group name as EEC. And in Cell number A12 also contains both NCG and DMZ, again the preferance should be given to DMZ and we have to assign group name as ETIS.

That means we will give second preference to keyword NCG. If domain name contains none of the keywords except NCG as per given criteria then only we will assign grup name DOSS.

## Assign Array To Variable

May 19, 2009

My question is about assigning an entire array to a single variable. In this case, I want to assign an entire array to one element of another array.

## Assign Range To Variable?

Jul 11, 2014

Why the variable DataRange won't set as below. I get Run-time error 9: subscript out of range.

Code:
Sub CreatePivotTable()
Dim DataRowCount As Integer

[Code].....

## How To Assign An Array To A Variable

May 15, 2008

how to do is the assignment of GoodArray1 to CurrentArrayToUse, i.e. "CurrentArrayToUse = GoodArray1" below:

Global NextArrayToUse()
Global CurrentArrayToUse()
Global PreviousArrayToErase()
Global GoodArray1(), GoodArray2(), [etc]
Global CurrentGuessNumber As Integer

[bunch of code, part of which assigns a number to CurrentGuessNumber, then the following...]

Select Case CurrentGuessNumber
Case 1
CurrentArrayToUse = GoodArray1
NextArrayToUse = GoodArray2
Case 2
CurrentArrayToUse = GoodArray2
NextArrayToUse = GoodArray3
PreviousArrayToErase = GoodArray1
ReDim PreviousArrayToErase(0, 0)

## Assign Range To Variable

Dec 24, 2007

I am trying to assign a range of cells to a variable but it isn't working. My code is as follows:

Dim VarRateTable As Range

Private Sub CalcBudgets(rng As String)
' Dim VarRateTable As Range
Dim CurrentRng As Range
Set CurrentRng = Selection
Set VarRateTable = Range(rng)

Private Sub NEWCALC(datarow)
Dim vt As Double

vt = Application.WorksheetFunction.VLookup(Cells(datarow, 4).Value, VarRateTable, 3, 0)

but the Set statement gives me an error: <method 'range'of object '_global' failed>

## Number Enter Creates New SHEETS And Auto Assign Names

Jun 29, 2007

B20 is the number that the user enters. It can be any number and it will create new sheets below and text of sheet name in B21:F21.

If i change the sheet name below, B21:F21 would also automatically change.

## Assign Value From Userorm Textbox To Variable?

Aug 18, 2008

I have created a userform with 3 textboxes and I want to return the values entered to variables for use in a procedure. I cannot figure out how to return the values.

Behind the command button I am unloading the form so that the calling procedure continues executing but my vars cannot see the controls.

I have tried making them public outside of the calling sub, but nothing. Can anyone point out to me what I'm doing wrong?

## Assign Variable To Worksheet Formulas

Aug 23, 2008

Split from Copy, Cut, Fill Relative Or Absolute Formulas

Originally Posted by AAE
Welcome to Ozgrid.

Assume the following:
Cell A1: is the input cell
Cell B1: contains a number
Cell C1: formula = A1*B1 or \$A\$1*\$B\$1

Your formulas must contains absolute cell references, other wise Excel will attempt to update them to the new cell references when copying/pasting.
If you "cut", rather than copy, Excel will maintain the original cell references in the formula.

Both of these questions are well within the experience level of a user with above average skill level as your profile shows. Please lower it (before one of the Admins does it for you) to a more realistic level.

See this training page on how to toggle between relative/absolute cell references: [url]

Well cutting is a nifty trick, thank you for that, I don't believe you quite understood my first question.

Lets use my calculator for instance. Using something it has designated K (for no apparent reason) I can enter a formula, such as K=*2-4, and after that is entered every number I then type and press equals to it will run that formula and spit out an answer. (after entering that function, if I press "5" "equals" it will give me "6") Now what I'm wondering is if excel has a "K" function that I can assign to certain cells, namely a column, so that whenever a figure is typed in any of the cells in that column, the required operation is done, namely *.9685.

## Assign Variable To Selected Range

Dec 21, 2006

What I need is a macro that assigns manually selected cells/ranges (which may or may not be continious) to a Variables.

## Open Workbook And Assign To Variable

Jul 16, 2007

Back to coding in VBA after long months of Matlab, and banging my head around this simple code snippet:

Sub openfile(FilePath)
Dim myWBk As New Workbook
'Now open Form
myWbk=Workbooks.Open(FilePath)
End Sub

This returns a nasty error "Activex Component couldn't create object". It opens the file given by FilePath though. What I would like is to assign the open workbook to the myWbk variable, such that it is easier to work with.

## Assign Variable To Different Data Types

Oct 12, 2007

Putting the final touches on a project I have and the last element doesn't work 100%. the scenario: I have a function that I wrote based on the user's input of the desired column to perform the operation. As a method of error-trapping and ease of use, I set instructions for the code to activate:

1) if the user highlights multiple cells of the desired column
2) enters the letter of the column (up to "z", not case sensitive)
3) enters the number of the column

Sub Button5_Click()
Dim ColumnUsed As Variant
' find number of columns selected
For MyColNum = 1 To Selection.Columns.Count
Cols = Cols + 1
Next MyColNum
'find number of rows selected
For MyRowNum = 1 To Selection.Rows.Count
Rows = Rows + 1
Next MyRowNum..............................

## Assign External Cell Reference To Variable In VBA?

Jun 29, 2006

I'd like to assign a value located in a cell in a workbook to a variable in a VBA program in another workbook. I do NOT want to open the workbook that contains the cell with the value. I can't get the hang of referencing an external cell reference. Let's say there's a value in a cell named "nbr" in a workbook named "tst.xls" located in a path "c: est" that I want to assign to a variable "tstnbr" in a VBA program, what is the best way to code that?

## Assign Multiple Cell's Values To A Variable

Jul 23, 2006

Need exact VBA code syntax to assign a workbook (to be closed) sheet's cells J4 to J72 values to a variable called "ColJValues" to be assigned to another sheet (to be opened later in the macro). The values are all dates. Once the other workbook is opened later in the macro, need the exact syntax to assign the value in the above variable, "ColJValues", to it's cells J4 to J72.

## Assigning Variables To Worksheet Names VBA

Jun 6, 2014

How to assign a "WS(i)" worksheet name and have the assigned variable (i) in the name equal the consecutive steps in a total page count.

Here's the code that does not work:

Code:
Dim WB as Workbook
Dim WS as Worksheet

Set WB = ThisWorkbook
For i = 1 To WB.Sheets.Count
Set WS(i) = WB.Sheets(i)
Next

I can see why it does not work. I can't figure out what to declare in my Dim or how to word the statement using the (i) variable in order to capture the value to attach to "WS."

Details: The issue is that the number of pages created will vary day to day depending on staff needs. There's an array of staff names to be assigned to page tabs and there will be skips and deletions along the way depending on daily attendance so "WS(i)" worksheet names can't be assigned at creation. What happens if they are is that the succession of worksheet names winds up being (e.g.) "WS1, WS2, WS5, WS12," etc. instead of consecutive.

There's a very complex data-sort-and-assign system that depends upon how many staff-assigned sheets there are, and as such each sheet has to have a "WS" worksheet name for the process to stay smooth. If the names are not consecutive, though, the sort-and-assign process becomes confused. This step takes place at the end of the initial workbook set up process, immediately before the data are addressed, so there really isn't an earlier opportunity. It can't be later since from this point on the system depends upon the "WS(i)" names being there.

It started fine when there were 5 people. Now there are 37.

## Assign To A Variable, The Total Number Of Text Entries In Col

Feb 28, 2009

I need to assign to a variable, the total number of text entries in col. A (or alternatively in a named range).

## For Statements For Multiple Variables With Similar Names

Feb 17, 2013

Basically I have a list that user can select up to 6 values from. I have set them as individual strings.

Code:
Public IH1 As String
Public IH2 As String
Public IH3 As String
Public IH4 As String
Public IH5 As String
Public IH6 As String

What I want to be able to do is loop through them and do certain actions. How can I get a for statement to do that.

I tried something like that but it doesnt work. How to get it working I need to do tests on the variable strings and I want to be able to reference them and I'm not sure sure how

Code:
For i = 1 To 6
If "IH" & i = vbNullString Then
Else
MsgBox "IH" & i
End If

## Switching Between Workbooks Whose Names Are Stored As Variables

Jul 20, 2006

I'm having trouble finding a way to switch between two workbooks that I have open whose names are stored as variables. If the variable name were variable, for example, I have tried the following:

workbook(variable).activate
workbook.activate variable
workbook(variable).select
workbook.select variable

none of these work and I've tried a ton of other ways to get it to work but I just can't figure it out!

## Range Names Created From String Variables

Aug 30, 2006

I work with data that varies in row numbers but is consisten in column width. I am trying to write code that will create a named range for the data but be flexible to expand or contract based on the amount of data that is pulled in. Below is the

Const lngLastPossRow As Long = 65536
Dim strDataRng As String
strDataRng = ActiveSheet.Name & "!R4C1:R" & Range("a" & lngLastPossRow).End(xlUp).Row & "C17"
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select