Excel 2007 :: VBA Storing Variables In Array?

Sep 7, 2012

Excel 2007.

I'm basically copying and pasting a bunch of columns. Currently, my code is very long because I'm not using a loop to plug in the column header. how I'd store all the column headers in an array (I think) and loop through 1-by-1.

Here's a look at what I'm doing now:

Code:
' ''Project Number
x = "Project number"
i = Sheets("RawData").Rows(FirstRow).Find(x).Column

[Code]....
how to store these column headers in an array and then pull them?

View 4 Replies


ADVERTISEMENT

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,

View 9 Replies View Related

Compare Variables Storing Times

Dec 27, 2006

I have tried lots of alternative for making Excel Dates & Times calculation work in vba. I can't find the place its creating problem. I have attached spreadsheet to have better look at it. When I am using time in one column (Calender!C) in VBA code, it works fine and update values in R1,R2... columns. But, when I am using time in another column (Calender!D), its not working properly and not updating appropriate columns. I will appreciate if someone can look at vba code in attached file. It looks like I am missing some kind of setting in excel/vba.

View 3 Replies View Related

Storing In Array

Feb 20, 2007

Here's the code linked with excel spreadsheet:

Function Function1(DailyClose, EFBillsYield)

TradingDays = Application.WorksheetFunction.Count(DailyClose)

For i_cnt = 1 To TradingDays - 1
DailyReturn = Application.WorksheetFunction.Ln(DailyClose(i_cnt) / DailyClose(i_cnt + 1))
Next i_cnt

AnnualReturn = Application.WorksheetFunction.Average(DailyReturn) * TradingDays
AnnualVolatility = Application.WorksheetFunction.StDev(DailyReturn) * Sqr(TradingDays)
RiskFreeRate = Application.WorksheetFunction.Ln(1 + EFBillsYield)

Function1 = (AnnualReturn - RiskFreeRate) / AnnualVolatility

End Function

When I enter the parameters DailyClose (which is an array of numeric), and EFBillsYield, which is a numeric also, the function will calculate the DailyReturn (in array) for each array element in DailyClose.

The problem is, I don't know how to catch the DailyReturn for each DailyClose element. The above code only loops and calculates DailyReturn one by one, but not storing it as an array.

How should it be modified so that the DailyReturn can be numeric array also, so that it can perform average and SD functions below the loop?

View 4 Replies View Related

Storing Textbox Contents In Array

Feb 13, 2012

I've created a textbox, call it Textbox1, where I've set EnterKeyBehavior and WordWrap to be true. If someone types something like

apples
bananas
cherries

in the textbox, and I use the following code

Code:

Dim TextBoxVal as String
TextBoxVal = Textbox1.Value
then TextBoxVal will be applesbananascherries.

I'd like to have the array be something like

apples,bananas,cherries, or
applesX_X_XbananasX_X_Xcherries,

or something else so I can tell when the user has hit the enter key. Is there any way to do this?

View 3 Replies View Related

Excel 2007 :: Declaring Worksheet And Range As Variables

Dec 22, 2011

Using Excel 2007, I'm trying to figure out (and not succeeding!) on how to declare worksheet and range - in order to:

input a formula to cell xfd1, then copy down to xfd2:xfd100 - and here's what I've been playing about with

Code:
Sub filldownxfd()
Dim src As Range, out As Range, wks As Worksheet
Dim sRangeName As String
Workbooks.Item(1).Sheets.Item ("Sheet1")
Dim example As Range
Set example = Range("xfd2:xfd100")

[code]....

View 2 Replies View Related

Excel 2007 :: Sum Data Based On Row And Column Variables

Sep 25, 2012

I'm working on a workbook that will combine a cash flow report by category and also by date. The categories can change (which I think is the easiest part) but so also can the frequency of data. For instance, you choose how you want to see the data, monthly, quarterly, annually and this will change the Row that the data should be summarized based off of.

Examples:
1) Annual: Sheet 1: Row = Rental Revenue and Column = 2001. I want to pull from sheet 2 all of the Rows that are mapped to Rental Revenue and all of the Columns that are mapped to 2001.
2) Quarterly: same idea but by quarter.
3) Monthly: No issue, just a SUMIF.

In Sheet 2 I have added a mapping at the top of each column for the Quarter, Year or Month that should be included in the Sheet 1 heading Row.

I tried using a sumproduct on both row and column using the whole data set underlying, but this did not work.

"=SUMPRODUCT(1*('Sheet 2'!$A$9:$A$140='Sheet 1'!$C28)*('Sheet 2'!$E$9:$GA$9='Sheet 1'!E$27)*('Sheet 2'!$E$10:$GA$140))",

It gives me a #N/A result.

I have tried to attach a table of what I am trying to do.

Sheet 1
Choose Frequency
1
Annual

[Code]....

View 4 Replies View Related

Storing Userform Textbox Inputs As Currency In Array?

Jun 23, 2014

I have a userform where the user enters prices for up to 12 items in textbox controls, and these prices are stored in an array. The textboxes are titled tbPrice1, tbPrice2, etc. I'm having an issue with the line of code I am using to store the values in an array. Every time I attempt to store them, I get runtime error 13 type mismatch, but I don't understand why.

dim PartPrices(1 to 12) as Currency

For x = 1 to 12PartPrices(x) = IIf(Trim(Me.Controls("tbPrice" & x).Value) & vbNullString = vbNullString, CCur(0), CCur(Trim(Me.Controls("tbPrice" & x).Value)))Next

A little more explanation here. Not all 12 parts will have prices, so I use the IIf statement to store the value 0 whenever the user has left a price field blank. I use trim in case they leave spaces in the price textbox or something. When the field is not blank, I take the value entered in the textbox, convert it to currency and attempt to store it in the relevant element of the array.

View 2 Replies View Related

Excel 2007 :: Finding Next Highest Value In Array

Sep 12, 2010

I am using VLOOKUP with the not_exact_match set to True, however instead of finding the next largest value that is less than value, I want to find a way of returning the next largest value that is greater than value. I have looked at using MATCH and OFFSET to try and increment the returned value by 1.

View 9 Replies View Related

Excel 2007 :: How To Give Array Value To Different Comboboxes In VBA

Oct 22, 2011

I have 4 comboboxes in a formcontrol (excel 2007, vba), each combobox has the name: Combobox_01 Combobox_02 Combobox_03 Combobox_04 I want to give all comboboxes the same array values "A","B","C","D","E","F" How do I do this efficiently?, I tried a For-NEXT loop, but I am not sure how to assign the For value from 1 to 4 to each combobox and it gives me an error...

View 9 Replies View Related

Excel 2007 :: Create String From Array?

Jul 23, 2012

I have this formula, ( which i found the basis of on a You tube video and Richard Scholar was accredited with improving the soloution)

=SUMPRODUCT(--ISNUMBER(F4:AH4)*10^{-29,-28,-27,-26,-25,-24,-23,-22,-21,-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1})

This forumla generates a number for each player, the higher the number the more inline they are to get a game

Problems are this works for the 29 weeks of this year but more weeks need added to the end of the year as we get there. Can i generate the array numbers from a formula and shorten.

This is a sample data ignore row 2( just a count of players) and data actually goes back to 6th Jan

Excel 2007BCDEFGHIJ1Wk21Wk22Wk23Wk24Wk25Wk26Wk27Wk28210101010101010103Player 1111104Player 210001105Player 300101016Player 4010101107Player 50110118Player 600009Player 7111000110Player 8011100011Player 9000012Player 101011113Player 11101114Player 121010115Player 13016Player 141010017Player 15118Player 160101Sheet1

View 7 Replies View Related

Excel 2007 :: Dynamic Range Into Array?

Aug 10, 2012

Excel 2007 is my version. I'm trying to build an array from a dynamic range. I will know the column letter and lastrow.I think I could do this via a FOR and NEXT routine but there has to be a more efficient way. Here is my inefficient code idea:

Code:
Dim orgctarray(65000) As Variant 'because I don't know how long the array will need to be
Dim cnt as integer
Dim startrow as integer[code]....

So as you see, technically, I need to take sheet1.range("B3:B" & lastrow) and turn it into a vb array (I don't want to copy the data to some hidden worksheet and do the work there)I'd also like to remove duplicates and sort orgctarray alphabetically after I have completed building it.

View 5 Replies View Related

Excel 2007 :: Autofilter Not Equal VBA With Array

Mar 21, 2013

Code:
Selection.AutoFilter Field:=5, Criteria1:=Array("CHF", "DKK", "EUR", "GBP", "NOK", "SEK", "USD")

I am trying to use VBA to filter a list for not equal to. See line above. I want to filter a table I have for unknown Currencies basically.

View 1 Replies View Related

Excel 2007 :: Create Array - Round Value When Concatenating

Feb 17, 2012

Excel 2007, Windows XP

I am concatenating some cells into an array. The amount fields should always have just 2 rounded digits following the decimal. What should change in the following VBA code to achieve that result?

Currently Cells(r, 6) & Cells(r, 7) could have these values:
1.5
24.78945678
45.2341

What I want is rounded values to 2 decimals:
1.50
24.79
45.23

The array is used as an input parameter in a remotely called function module, after logging into the remote system, SAP.

' delim is a | character

' Populate Myarray with data from all rows
' - Only from rows which are not hidden ' 05/23/2008

For r = 1 To row_count 'r is row number
if worksheets("JEMASTER").rows(r).hidden = false then
i = i + 1 'increment myarray index by 1
myarray.AppendRow

[Code] .........

View 5 Replies View Related

Populate Multiple Array Variables With Same Code By Dynamically Changing Array Name

Sep 9, 2012

I am trying to populate many arrays with the same code using something like this. For this test, assume the following data in A1.

1
2
3
4
5

6
7
8
9
10

11
12
13
14
15

16
17
18
19
20

21
22
23
24
25

Code:
Sub populate()
Dim firstArr(5), secondArr(5), thirdArr(5), fourthArr(5), fifthArr(5) As Integer
Dim r, c, num As Integer

[Code]....

The above code does not work of course and falls over. I am unsure whether I should try and concatenate with something like this eg "" & arrName(i) = Cells (r,c) or go down a different route.

View 6 Replies View Related

Array Variables In VBA

May 5, 2006

Is there any way to add together a portion of an array without using a loop or for statement. i.e.) I have a array variable: arrayvar = array ( 1, 2, 3, 4, 5)

is there a way to sum the last two numbers in the array without using

arrayvar (3) + arrayvar (4) or a loop or for statement.

I am thinking something along the lines of worksheetfunction.sum( arrayvar( 3 to 4)) I know that this does not work but I hope you can see what I am trying to do

View 4 Replies View Related

Array Name Loop Through Variables

Jun 17, 2013

I am trying to loop through different SlicerCaches but it doesn't seem to work.

Here is the code, I am trying,

VB:
Test_Name = Array("[Test - Test Allocation]", "[Test 2]")
For i = LBound(Test_Name) To UBound(Test_Name)

ActiveWorkbook.SlicerCaches("Slicer_Exec_Function_Summary1").VisibleSlicerItemsList = Array("[Mercury].[Exec Function Summary].&" & Test_Name & "")

The code returns a mismatch 13 error.

When I try it without an array, it works fine.

VB:
Test_Name = "[Test - Test Allocation]"
ActiveWorkbook.SlicerCaches("Slicer_Exec_Function_Summary1").VisibleSlicerItemsList = Array("[Mercury].[Exec Function Summary].&" & Test_Name & "")

View 3 Replies View Related

Putting Variables In Array

May 7, 2012

I want to know if I can populate an array with variables..

For example, imagine I've declared 3 variables a, b, c.. and each variable has been assigned a value (which can change)

now I want to say this:

myArray = (a, b, c)

then I want to be able to loop through myArray and retrieve the value attached to each variable..

View 6 Replies View Related

How To Compare Array Variables In VBA

May 22, 2012

I have 2 array variables in a block of code which I need to compare to check if the items in the arrays are exactly the same. The arrays are exactly the same size and I can see the contents of the arrays in the watch window.

When I try to run and IF statement as in: IF VARIABLE1 = VARIABLE2 THEN - I get a compile error saying "type mismatch" on the equal sign.

View 3 Replies View Related

Array Of Variables Want To Match Them

Feb 21, 2008

What would be the best approach/funcvtion to use in excel if I had an array of variables and wanted to match them?

E.g.

1,2,3,4,5,6,7

I would like to lookup up a column and if either of these numbers are in that row to say "yes otherwise "No"

View 9 Replies View Related

Vlookup, 9 Different Variables, 9 Different Table Array

Jun 11, 2009

I need to be able to do a look up in 9 different table arrays using 9 different ranges.

I have attached a worksheet to help explain. My problem is that I can't have 9 IF statements in one formula.

I am not sure how to make this smaller or work.

View 14 Replies View Related

Passing Array Variables Between Sub Routines

Jul 22, 2014

Okay, I made an epic fail on a previous post that i turned to Solved, a real error on my part of not fully testing before i posted Solved, basically i was so excited that i'd solved it myself, well partially.

I have the following code in the first sub routine that collects which option button has been selected.

[Code] .....

The second sub routine is per following.

[Code] .......

Unfortunately when i tested this with only one variable myRev(1) it worked fine but when i added more myRev variables 2 thru 4 if failed, so is there a trick to passing an array variable between sub routines?

View 2 Replies View Related

Passing Array Variables In Functions?

Sep 15, 2012

What I am trying to do is to automatically build a "tree" diagram representing the links in a huge model which is dynamically configured. What I have a problem with is the following:

The tree consists of layers, I start off with the top layer and for each entry in the top layer I can add all its subsidiary layers and draw links between them, this uses a function which takes as its arguments the node name and its layer number (how far down the tree it is) and the number of items in that layer so far.

So I start at the first item in Layer 1 and there are as yet no layers below it. I start at the first one and add the first item in layer 2 then I kick the function off again and that adds the first item connected to item 1 in layer 2 in the layer below (3) starting at the first one, and so on. When I reach the bottom I go up one layer and add the second item in the bottom layer and so on. When I have added all the connctions to the first item in the next to bottom layer I go up one layer and add the second item connected to the first item in that layer and then add all the items connected to it and so on and so on.

In this way I build up the network exhaustively (to make things more complex more than one item in a layer may connect to the same item in the layer below).

I can do almost all that, the issue I am struggling with is I need to keep track of how many items there are in each layer (as some layer 1 items connect to 1, 2, 3 ...8 layer 2 items and so on). my idea is to keep a running total of these in an array LevelCount(1), LevelCount(2) etc. so when I add a new item to a layer I know where to put it. However I cant workout how to do this final step.

Currently I have a function that draws the nodes below a specified node this and takes the correct value from the array LevelCount(n) by passing Levelcount(n) (where n represents how far down the tree you are) into the function and the function then updates the value of LevelCount(n) (ByRef) so that next time I use it it is correct. That is fine but what I want to do is to is to call the same function from within itself when it adds each node which would make it work automatically - it would keep calling instances of the function until it reached bottom and then go back one step at a time to the top but I cant work out how to reference the right value in the array to pass into the second (and subsequent) instances of the function.

I don't think I can simply pass (n) into the function and in the body of the function set LevelCount(n) = LevelCount(n)+1

I also dont know ahead of time how many layers the model will have, nor can I tell which layer a node sits in as it depends on the links that are dynamically configured.

Beyond this a node can also be subsidiary to nodes in more than one level so it needs to sit at the lowest level - but I suspect if I can work out how to do the first bit i can do this too.

View 7 Replies View Related

Variables To Contruct The Name Of The Active Array

Jun 24, 2008

I am trying to construct an indirect array reference as follows;

Dim A10 As Variant
Dim A11 As Variant
Dim A12 As Variant
For i = 0 To 2
String1 = "A" & (10 + i)
String1 = Cells(6, D1_CNum + i).Resize(2995, 1)
Next

But the above doesn't work, what would be the correct way of creating a reference to an Array using variables to contruct the name of the active Array?

I was hoping String1 would equal "A10", so that on the Cells command Array A10, A11 and A12 would be filled using the value of String1??

View 9 Replies View Related

Create Array Of Variables And Sort

Nov 12, 2006

I have a dynamic number of rows each with three colums of values. These varaibles I want to fill an array with but I don't know how.

Now I've just "concated" these three variables into a string and then I intend to split the string into rows by the third comma. (see below)

I think it's easier though to use an array and I really appreciate some assistance. Please tell me also the best way to sort the array. It will be sorted by var_Status which is an integer. (sort order: max positive to max minimum)

PreString = PreString & var_StartWeek & ", " & var_Status & ", " & var_Totalh & ", "

View 9 Replies View Related

Pass Array Variables Between Modules

Jun 8, 2007

I am trying to pass a public variable to another module in the same workbook. On Module1 I declare at the top

Public stores
Sub Main()
Dim stores(1 To 100, 1 To 10, 1 To 10)

Reader

...which then calls the procedure Reader in Module2

Sub Reader()
let x=1
let y=1
let z=1
let stores (x,y,z)=activecell.value

I've left out the portions of code that seem irrelevant. When the macro runs, I get a type mismatch error on the "let stores" line. If I move the code from Reader into the procedure Main, it works, so it seems to be an issue with passing the variable. I haven't used multiple modules very often so this is probably a very basic issue.

View 3 Replies View Related

VBA How To Read Cells Into Variables/array And Use In String

Apr 15, 2009

I made the formula work great, however I want to cut it down and simplify it and make it easier to manipulate... as opposed to having the filename written every so many times.. replace with a loop instead.... so:

Private Sub Weeklytestt_Click()
Dim varCurDate As String
If DateYesterday.Value = True Then Answer = "1"
If DateToday.Value = True Then Answer = "2"
If DateTyped.Value = True Then Answer = "3"

Select Case Answer
Case Is = "1"
varCurDate = Format(Date, "yyyymmdd")
varCurDate = varCurDate - 1
Case Is = "2"
varCurDate = Format(Date, "yyyymmdd")..........

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

Copy Table Values To Array Variables And Use Them In Another Sheet?

Nov 20, 2012

I have a query about using Array Variables in excel VBA. I have a set of lookup tables and a main data table. The data table will be downloaded everyday. I need to replace the ID's in the main table with actual data from the Lookup tables.

e.g.
Main Data Table
Color Operator
005--325 005
004--326 004
003--327
001--328
002--322

Lookup Color
001 - Red
002 - Blue
003 - Green
004 - Violet
005 - White

Lookup Operator Table
323 - Operator 1
324 - Operator 2
325 - Operator 3
326 - Operator 4
327 - Operator 5
328 - Operator 6

I have a huge amount of data in the main table. So I can't use a lookup formula for automation. Rather I would like to use VBA to create array variables, scan and copy the lookup data into the array and replace the ID's in the main table.

Only that I am unable to achieve this using arrays. I am very basic in executing code related to Arrays.

View 2 Replies View Related

Array Functions With * And | And . And ; (2007)

Jun 13, 2009

I occasionally see array and/or sumproduct formulas that incorporate symbols that I don't really understand in an array context.

These symbols include:

* (asterix)

| (vertbar)

. (period)

; (semicolon)

Also: are there other powerful symbols that have meaning in an array or sumproduct context?

View 3 Replies View Related







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