Passing Variables Between Excel VBA And VB

May 28, 2009

I have an Excel workbook with a command button on it. When this button is clicked I wish to pass the filename of the Excel workbook to my Visual Basic program, I have so far got the following


Public Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

View 9 Replies


ADVERTISEMENT

Passing Variables Between Subs?

Nov 3, 2009

I think I am confused as to how to declare a variable in one sub and use it as an input into another sub. I have attached some code below that assigns a value to two variables and then calls a sub that uses those values. I am brand new to programming so I guess I am confused how to implement this.

View 3 Replies View Related

Passing Variables To Other Subroutines In Same Mod

Nov 3, 2009

I'm having issue with passing along a variable. I learned this morning how to pass these along. But for this instance something isn't working correctly.

The code is below:

I cut out all the subs in between so you can see the problem.I think it has something to do with "File search"

The problem is after sub New_Pro_Test runs then returns back to Sub RunFolder I get subscript out of range error. It is suppose to open the next workbook in that folder.

View 7 Replies View Related

Passing Variables From A Userform

Dec 21, 2009

I am trying to eliminate a lot of the global variables from my program by passing the variables to my functions and subs as arguments. I am stuck though when it comes to variables created in userforms.

The program starts with a series of userforms that asks the user for information that will be used throughout the rest of the program. Data is assigned to the variables on the click events. Is it possible, without using global variables, to pass those variables to the rest of the program?

Example:

View 3 Replies View Related

Passing Variables To Subroutine

Feb 20, 2010

My code passes a string created by a function to procedure. It all works great, but i need to add something so that if the string = false then sub doesn't run. What's the best way to do that. Here is some of the

View 6 Replies View Related

Passing Variables Between Subs

Oct 30, 2013

I have some code I'm running:

Code:
Sub Main ()
Dim Value1 as integer
Value1=5
Call firstroutine
Call secondroutine
End Sub

[code].....

Somehow I need to pass Value1 & Value2 to secondroutine.

View 5 Replies View Related

Passing Variables Between Macros

Jul 20, 2007

I am familiar with the normal method of evaluating a variable and passing it to another macro using Call MacroX (var1, var2).

In this instance, I call macro 1 and within macro 1, I call macro 2. Macro 2 establishes some variables, finishes at Exit Sub and hands control back to macro 1.

I want these variable returned to macro 1. How do I do that?

View 9 Replies View Related

Calling A Sub From Another Sub - Passing Variables

Aug 1, 2007

problems with the syntax involved in calling one subroutine from within another. I want to pass two variables calculated in a 'main' sub into the parentheses of another I am calling (see pseudocode below). When I pass one variable, everything works fine, however, when I try to include another, I get an error message - something about expecting an "=" in the syntax, which is clearly not correct.

Sub main()

row = .....

column = .....

counting(row, column)

End sub()

Sub counting (row, column)

.....

End sub

View 9 Replies View Related

Passing Date Variables To SQL

Jun 1, 2006

I am trying to write a query to pull data using Basis ODBC from our accounting system into Excel. I want to limit the results to a specific range of dates. I can hard code the start and end dates into the Critirea in QueryBuilder and get the results I want, but I would like to enable my users to enter variable Start and End dates in cells in the Workbook so they could select ranges at runtime.

I currently have a StartDate named range in cell C2 and and EndDate named range in cell D2. The data columns would contain fields for "Order Date", "Customer Name", "Order Total" and the like. I want to be able to limit by "Order Date" using the StartDate and EndDate variables.

View 9 Replies View Related

Passing Variables Between Forms

Jun 28, 2006

Is it possible to pass a variable between forms? For example, I have a textbox on a form that takes a date from a user. Next to the text box is a button that calls another form that is a selectable calender with each of the dates on the calender as buttons. If a date button is pressed a variable is created which I want to pass back to the first form and place it into the textbox.

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

Passing Variables From A Macro To Sub-Routine?

Nov 1, 2012

In my code I am using several variables to create a concatenated string and this is making my code quite lengthy..Now I have manged to use it in the SELECT CASE but it still remains big..

SO I was thinking if it were possible to break it into smaller parts and create a Sub-Routine which can be called in the Main code in the select case for the specific condition this way I can use it in Multiple places without hassles..

find the code which I need passing the variables..

Code:
Sub Condition_I()
Dimm Search_Value as string
Dim INo As Long
Dim Whole_Text As String
Dim Lookup_No1 As String
Dim Lookup_No2 As String
Dim Condition_I_Text As String
Dim WksI As Worksheet

[code].....

I need to pass the Search_Value from the Main Code and also the whole_text back to the Main Code..

View 9 Replies View Related

Check Boxes, Variables And Passing Them All

Mar 29, 2008

I have a worksheet with a command button, this button opens a userform with check boxes. the user has 4 options and can select them all if they choose, they click the command button on the userform and then the original sheet gets populated with only specific data based on the variablesbut nothing happens.

Private Sub CmdStructStrategy_Click()
FrmStrategy.Lbl1.Caption = "2007 Roll"
FrmStrategy.Lbl2.Caption = "Flat Price"
FrmStrategy.Lbl3.Caption = "Options"
FrmStrategy.Lbl4.Caption = "Time Spreads"
FrmStrategy.Show
End Sub

View 9 Replies View Related

Passing Variables Within Procedures On A Worksheet...

Apr 5, 2007

passing variables within procedures on a worksheet. i have:

Private Sub lblGoToMaterials_Click()
'email to:
'----------------
'grab current position
myRow = ActiveCell.Row
mycol = ActiveCell.Column

and i would like to pass the values gotten in myRow and mycol to:

Private Sub cmdGoBackTo_Click()
Application.Goto Reference:="R" & myRow & "C" & mycol

View 3 Replies View Related

Drop Down Change With Passing Variables

Aug 2, 2007

I have 18 drop down lists that are identical and named DropDown1, DropDown2, DropDown3, etc. Each one references cells (cell link) E1, E2, E3, etc respectively in another worksheet Lookup that is hidden. I am trying to write up some code that when the user selects any drop down, depending on which one it is, it passes certain values to another Sub which has cases. I got my first ideas from an old thread Drop Down List Box but there were no variables being passed.

My code originally worked with one variable being passed (x), but now I am trying to pass 2 (x and nrow) and I get an error in the Sub DropDown1_Change(): ByRef argument type mismatch. Below is my coding, I only included a few drop downs for this example. The Cases are based on the cell link (ie Case 2 is when the cell link shows '2' in the Lookup sheet) I only used a message box for testing purposes. I will have to do some other stuff but I need these variables passed first before I can continue

Public x, nrow As Integer
Sub DropDown1_Change()
x = 1
nrow = 2
Call get_surcharge(x, nrow) 'this is where I get the error
End Sub
Sub DropDown2_Change()
x = 2
nrow = 3
Call get_surcharge(x, nrow)
End Sub............

View 3 Replies View Related

Passing Variables Back From A Called Subroutine?

Nov 3, 2009

I have a question. Can this be done. Pass a couple of varibles back from a called sub routine?

The calling sub calls the called sub does which has code plus a couple of varibles needing to come back.

Scope: The called sub is a series of case statements with code and sets varibles which are needed back in the calling sub.

View 6 Replies View Related

Passing Global Variables / Values To A Procedure

Mar 24, 2014

I understand the use of Global variables in the sense that they can be used throughout the project. However, I also understand (I think) that unless these variables (like any) are released from memory or the values changed by some other means they will retain their values when accessed inside a another procedure regardless of where the calling procedure is Public or Private. This seems to be a convenient way to access the values of these variables without having to make ByVal or ByRef declarations in the procedure calling process.

Firstly, is my understanding of this correct? (That the variables and associated values) can be accessed from within a calling procedure without formally passing them in?

Second, even if this is true is it considered to be bad practice because the variables will store the last known value which may or may not be correct if you aren't paying attention to how the variable was last set.

View 2 Replies View Related

AddFields Method Of The PivotTable Class Failed Passing Variables

Aug 20, 2007

I am trying to pass variables to a sub which creates a pivottable.

The following line of code works:


pt.AddFields RowFields:=Array("BG_DETECTION_DATE", "BG_SEVERITY"), ColumnFields:=Array("BG_PROJECT_DB", "BG_USER_01")

I want to create multiple pivottables so I created a seperate sub and intend to pass information to them. I have tried doing it as follows but it doesn't work:

Dim row_fields As String
Dim column_fields As String

Row_Fields = """BG_DETECTION_DATE"", ""BG_SEVERITY""" .....................

View 72 Replies View Related

Passing Variables And Values Between Sheet Code And Module Code

May 13, 2009

I have some buttons in different sheets in an excel file, each button has its own code, that is the reason I can not move the code related to each object to another location (sheet or module).

And I have one piece of code in Module1 (Auto_load) in order to execute automatically this routine every time file is opened. Inside "auto_load" routine I initialize some values of some check buttons,options buttons and positions of some objects in diferent sheets, but I can not pass the value of variables between Module and Sheet's code even when I declare as public variables and/or function.

I have the following structure: ...

View 11 Replies View Related

Passing Range To A Formula In Excel From Another Cell

Oct 19, 2013

Not sure if possible but im trying to pass a range to a formula in excel from another cell.

[URL] .......

There is a sum formula in range B5 that sums the range (B9:13). I would like to have a formula in cell D5 that passes the new range in cells(D9:D13) to the formula in cell B5 and give the result in cell D5. Is this possible using Excel functions or would i need to create some sort of user defined function for this?

View 9 Replies View Related

Passing Word Variable To Excel In Office 2010

Apr 2, 2013

I'm trying to pass a variable from Word to Excel. Basically I have a Word document with a plain text content control in it. I'll have users populate this field. I know how to create a reference to that content contol in Word VBA that'll tell me what's in that content control (eg.

Code:
MyWordVar = ActiveDocument.ContentControls(1).Range.Text
).

What I can't figure out is how to pass the value of "MyWordVar" to a variable in Excel.

View 4 Replies View Related

Select An Excel Sheet With 2 Variables In It?

May 17, 2013

So far my line of code is:

Sheets(" & Cstr(y) & " - " & Cstr(i) & " load" & ").Select"

Selecting a worksheet with 2 variables in it and some text,where y and i are integer variables. P

View 1 Replies View Related

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

Excel Formula To Check Three Variables And Return Value?

Dec 19, 2012

I've tried this in formulas (if, if/and, if/vlookup, if/vlookup/and) and I've been trying to write a macro for this (which I'm thinking needs to loop).

Problem: I have a report that is broken into three categories.

Column A includes locations, Column B includes vendors. Across the top, the monthly report is broken into four classifications (trans types), which are then further subdivided by quantity and cost. (Rough estimation is provided below)

Locations
Vendor
Z53

Z50

Z51

Z52

[code]....

My data comes in sorted by location, and subtotaled by vendor and trans type. So I'll have a row of data that includes:

Location 1, Vendor A, Z51, $200, 2
Location 1, Vendor A, Z53, $150, 2
Location 1, Vendor B, Z53, $150, 1
...

What I need to do is pull the data for Z53 based on Location & Vendor. So from the data is would read Location 1 & Vendor A and then find them on the report and paste into the appropriate row (in my report Z53 qty is col E and $ is col F).

I was trying to do a macro that within a determined range (finding the first and last instance of Z53) would match location and vendor to the monthly report and paste the values of Qty and $ into the appropriate fields. Below is what I managed to mangle:

Code:
Dim Holder1 As Range
Dim Holder2 As Range
Dim Holder3 As Range
Dim Holder4 As Range
Dim Found As Range
Dim y As Range
Dim x As Range
Dim Z0 As Range

[code]......

I was originally trying to match from the report to the data and then copy back to the report, but I had the idea of instead taking the location and vendor from the data and finding the match on the report and pasting. That way I wouldn't have to worry about errors (unless a new location was added that was not included on the report - which I think is an easier fix than trying to go the other way, but maybe not).

View 9 Replies View Related

Excel 2010 :: SUMIF With Multiple Variables?

Dec 9, 2013

Excel 2010 Windows 7

There is a lot going on, including a check box, which I dont have much experience with and data on two different sheets. I need to sumif the box is checked (true cell) and the item matches the item on another sheet then I need to sum the qty in the adjacent column.

Inventory sheet:

I will have a similar formula in both C and D, but this will be for what I need in D, and I should be able to figure C out from that. What I have to get my starting number is "=SUM('Proc-Pack SUMMARY'!B16-'Proc-Pack SUMMARY'!C16)". Now I need to subtract what is sold from that number to give me a current on hand number.

Type
date
qty packaged
qty bulk

Inventory sold sheet:

The type column is a drop down box with that matches the type column on the inv sheet. The "cyl" and "lbs" columns have check boxes in them. So if C (cyl) is checked and A matches A on the inventory sheet I need to subtract D.

Type
Cyl
Lbs
Qty

Ive tried various variations of this "=SUM('Proc-Pack SUMMARY'!B16-'Proc-Pack SUMMARY'!C16)-sumif(Sold!F:F,"true",Sold!E:E=Inventory!A:A,Sold!H)"

Also is it possible to link so you can only one of the two check boxes in a row? I know the option button does this, but I didnt think that would work with the formula.

View 7 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 2010 :: Condensing VBA Code - Using Dynamic Variables?

Jul 17, 2012

I have some VBA that dumps various sheets data into an SQL Database.

Part of that requires me to sanitize all of the fields before they make it to the DB, (at least to prevent the code from breaking itself w/ errant ' characters.

right now my code is as follows

Code:
If InStr(aa, "'") > 0 Then
aa = Replace(aa, "'", "''")
End If
If InStr(bb, "'") > 0 Then
bb = Replace(bb, "'", "''")

[Code] ......

I was hoping to condense it to something like the following, however it is not working how I hoped / want it to. .. I had found somewhere out there that this Eval() function possibly could be used to 'reference' dynamic variables, however it does not appear to work at all anymore, and even then it may only have worked to 'read' and not 'write' to the variable. (Excel 2010, on Windows 7 64-bit).

Code:
itemsToSanitize = "aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp,qq,rr,ss,tt,uu,vv,ww,xx,yy,zz,aaa,bbb,ccc,ddd,eee,fff,ggg,hhh"
ITSArray = Split(itemsToSanitize, ",")

For Each thing In ITSArray
If InStr(Eval(thing), "'") > 0 Then
Eval(thing) = Replace(Eval(thing), "'", "''")
End If
Next thing

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

Excel 2010 :: Overflow Error 6 - When All Variables Defined As Integers

Jul 17, 2012

I have tracked down an overflow Error 6 to the following line:

dMax = Int(s * DPoints / SPoints)

Where all variables are defined as Integers. The error goes away when I define s as Long, but I don't understand why this should be required since none of the individual variables ever exceed the scope of an Integer (-32768 to 32767).

Strangely, I get the same overflow problem when I type either of the following calculations in the immediate window:

? Int(328 * 100 / 1000)
? 328 * 100 / 1000
? 328 * 100

Note that no variables are defined, so why the overflow problem? Surely Excel/VBA should be able to work with this very basic calculation without any problem, by assigning the appropriate dimension, no?

Is this a known problem with Excel/VBA?

I am using Windows 7, with Excel 2010 (updated from 2009).

View 9 Replies View Related







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