Linking Macros & Pass Values/Variables
Oct 18, 2006
I have a worksheet which I've set to read-only, I am trying to get it to pull in information from external worksheets. I've created a Macro that will automatically bring in the data however it will only go into a pre-defined cell/row. I have another Macro which looks up the next available row (which is where I want the data to go) but can't get it to link into the macro's. In short, Macro1 brings in data, Macro2 finds next available row, Macro3 brings in another worksheet, Macro4 finds next available row etc etc.
Sub Macro3()
With ActiveSheet.QueryTables.Add(Connection:= Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:Documents and SettingsjspencerDesktopExcelAMMH.xl" _
, _
"s;Mode=Share Deny Write;Extended Properties=""HDR=NO;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database" _
, _
" Password="""";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking................
View 2 Replies
ADVERTISEMENT
Mar 21, 2012
Today's question concerns one sub calling another, having the called sub do some stuff that then effects what the calling sub does.
Essentially
Code:
public sub sub1
dim x as integer
dim countCall as boolean
x = activesheet.cells(1,1).value
countCall = False
call sub2(x, countCall)
[code]....
Obviously the above is a very simplified version of what I'm trying to do, since everything there could of course be handled in a single sub. It is for illustration purposes only. My actual main sub needs to call the secondary sub repeatedly in some stacked loops and its in-feasible to but the second sub's code into the main thread.
The problem remains though, I know how to call a sub and pass variables to it, i just have no inkling on how to pass results back to the calling sub. I've tried a few things I've found around the web but I seem to be missing something important.
View 1 Replies
View Related
Jan 26, 2007
I have a userform with a button that wil run macro "sub1". If "sub1" encounters an error on a worksheet it wil show a custom
error messagebox(a userform) that contains the option "Don't ask me again". If true it sets a global variable. Do I need to use global variables to pass to the original procedure "sub1" for the ok/cancel buttons too or is there an other method?
View 3 Replies
View Related
Jun 27, 2014
I am having trouble defining a variable in a For loop. When I run the loop the first time everything seems to work fine, but when the loop goes back through the second time it carries its previous value with it. Here in lies the problem if the variable is supposed to be empty (not assigned a value). So, for example: 'Variable' in the code below may or may not be given a value depending on the conditions in the For i loop. If it is given a value then 'Variable' will still have that value on the next pass through the For j loop. And if none of the conditions are met for the If statement in the For i loop the second time through the For j loop, it should be an empty variable, but instead the code is reading it as having a value (from the previous pass through the For j loop).
[Code] .....
I have tried using:
[Code] .....
After the For j line, and instead of using:
[Code] ....
I tried using:
[Code] ....
and I've tried:
[Code] .....
I get an Object error with either line I use. I just want to be able to reset the variables each time through the For j loop so in the case of an empty variable, it doesn't try to calculate based on the previous value.
View 6 Replies
View Related
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
Sep 2, 2007
I am looking at using forms, as in the Userform... Not used them before, but would like to know, if you create a text box on it, is the value global, so any module can use the var? or how can I, so that value entered can be used on the whole book ?
View 3 Replies
View Related
Jan 22, 2008
Here in the code wbResults is a Workbook present in my present folder. In the Sheet1 and Column "I", the result of test cases is entered. The Result of the TEST CASES can be PASS or FAIL. So i am trying to get the total number of PASS test cases and Total FAILED test cases. BUT with the code i have written, i am not able to fetch the right data. Is the method of comparing wrong? or what else? i have tried to debug but nothing comes out.
Do While Not i = 1008
If (wbResults.Worksheets("Sheet1"). Cells(i, 8) = "PASS") Then
PASS = PASS + 1
End If
i = i +1
Loop
View 4 Replies
View Related
Nov 17, 2007
When working in an userform, and you call another sub routine within the userform, will all variables passed automatically be byref (no way to do byval?)?
View 3 Replies
View Related
Jun 11, 2007
I have a macro that nicely select the named range that the active cell is in. I want to chain on to that macro a macro that has a parameter a range with that active selection. I dont see how to "take" the active selection on the worksheet from within the macro and pass it to another. I assume I could change the cellInRange macro to return a range, however I dont yet see how to do that.
View 2 Replies
View Related
Feb 10, 2012
I am attempting to pass a boolean variable between two macros. The SUBMIT macro calls the STATUS macro to do some verifications and the STATUS macro should return the boolean variable RDY back to the SUBMIT macro, but does not.
I am using Excel 2007. Both macros are in the same Module. The STATUS macro is called from several places in the code.
Public Rdy As Boolean
Sub Submit()
[code]
Call Status
If Rdy=True then msgbox("You have completed your form") else msgbox ("Not ready")
[code]
end sub
[Code]....
View 1 Replies
View Related
Jan 10, 2014
I am trying to implement the idea of creating an application with two workbooks: a hidden code workbook (CodeWb) that holds all of the forms and macros, and a separate visible data workbook (DataWb) that uses the macros in CodeWb. The DataWb houses a little bit of VBA code to open the CodeWb, which then supplies the programming power to handle click events, etc., performed by the user in the DataWb.
I have advanced to the point of being able to open CodeWb from DataWb and trigger execution of a macro named "DataFileOpen" in CodeWb. This macro initializes things in DataWb and creates buttons there on a particular sheet, using the line:
Application.Run "CodeWb.xlsm!DataFileOpen"
So far so good. This works fine. My problem is that nothing happens when I click on the buttons created in DataWb, with their event handling code residing in CodeWb. Obviously, I'm missing something that maintains a connection between the two workbooks.
How can I use the code in CodeWb to handle all events occurring in DataWb?
View 2 Replies
View Related
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
Jul 6, 2012
I have two columns one with the variables names and the other with the values now I have to assign vales to teh variables dynamically using the macros and vba code.
View 7 Replies
View Related
Oct 27, 2008
i have two arrays that I want to use in a trend function. I don't think i can just use the array as is in the fucntion so my guess is that I need to pass the array into a range of data, and help on how I can do this? (also this is in VBA, fyi)
View 9 Replies
View Related
Sep 18, 2009
i'm trying to do my homework which requires me to pass values from an array in excel worksheet to VBA and print it in a msg box
i've tried
dim arr as variant
arr = range("A1:A6").value
msgbox arr
but it didn't work.
that's the first step of the homework the second is i have 2 list of array
year:1999 - 2002
profit:10,20,30,40
i have to find the max profit and get the year when it occurs
View 9 Replies
View Related
Nov 20, 2008
I have Workbooks("A") and Workbooks("B") open.
Workbooks("A") contains Sheets("Sheet1"). Range("mySource").
Workbooks("B") contains Sheets("Sheet1").Range("myTarget").
Both ranges have been created by joining multiple ranges, in this way:
Union(Range("C1:C13"), Range("K1:K2"), Range("K5:K9"), Range("K14"), Range("Q6"), _
Range("I18"), Range("B20:P20"), Range("B24"), Range("C26:E26"), Range("C29"), _
Range("B34:B40"), Range("B44:Q50")).Name = "mySource" 'or "myTarget"
So: both ranges contain the same number of cells with the same addresses, and they have been added in the same order. However, if now I try to pass all values from mySource to myTarget, in this way:
Workbooks("B").Sheets("Sheet1").Range("myTarget").Value = _
Workbooks("A").Sheets("Sheet1").Range("mySource").Value
the result is a complete mess. Only the first "subrange" of mySource ("C1:C13") is passed to myTarget, and pasted in each of its "subranges", sometimes by rows and sometimes by columns...
View 2 Replies
View Related
Nov 21, 2008
You have an array and a range of the same size and you have to put the array values into the range, something like this:
Dim i As Integer
Dim myCell As Range
Dim myArray(10) As Double
i=0
For Each myCell In Range("A")
myCell.Value = myArray(i)
i = i + 1
Next myCell
except that this code looks a bit awkward to me.
View 2 Replies
View Related
Nov 20, 2007
I'm just starting to experiment with passing values between workbooks and between modules and so far I've managed to get it working. However, I now can not run the receiving module independently because of the passed value ? Let me explain ...
I use start/end dates within my modules to create date sensitive reports. I have several files (for different departments) and each file has a module called "AbsenceChecker", I have been working on a way to create a Report Master workbook to run and amalgamate the "AbsenceChecker" module from each department.
Sub RunAllLoaders()
Dim AbsenceStart As Date, AbsenceEnd As Date
Dim PassVar1 As Date, PassVar2 As Date
Dim DateStart As Date, DateEnd As Date
On Error Resume Next
'start of data validation script continued in private sub.
GetValidDates FromDate:=PassVar1, _
ToDate:=PassVar2, _
MinDate:=DateSerial(2007, 1, 1), _
MaxDate:=DateSerial(2007, 12, 31)
Excel.Application.EnableEvents = False
Workbooks.Open Filename:="R:RostersRosterALPHA.xls"
Application.Run "RosterALPHA.xls!AbsenceChecker", PassVar1, PassVar2
End Sub...................
View 2 Replies
View Related
Nov 30, 2007
I am trying to convert some text from a number of cells to shape in another workbook. The problem is, if the text in one of the cells is too long (from testing it by too long i pretty much mean roughly 100 characters) then it doesn't pass anything at all to the shape. Is there a way around this so that all text will be converted to the shape regardless of its size?
By the way, the code is:
ActiveWorkbook. Sheets("Sheet1").Activate
ActiveSheet.Shapes("Text Box 1").TextFrame.Characters.Text = "1. " & Priority1Range.Value & Chr(10) & "2. " & Priority2Range.Value _
& Chr(10) & "3. " & Priority3Range.Value & Chr(10) & "4. " & Priority4Range.Value & Chr(10) & "5. " & _
Priority5Range.Value
The variables 'Priority1Range' represent the cell the text is in that I am trying to pass and it's variable type is Range. I have tried declaring the Priority1Range.Value as a string variable and using this instead but this doesn't work.
View 9 Replies
View Related
Jan 16, 2008
I have userform1 with commandbutton1 and listbox1. From listbox1 you make a selection and then you click commandbutton1 and it calls up another form (userform2.) On userform2 I have a label that I need to capture the value highlighted in listbox1 form userform1. How do I write this code to have label update to the listbox selection?
View 2 Replies
View Related
Jan 1, 2008
how is the best method to link up forms to "talk" to my modules? (aka set variables inside my module script)
the way i understand it with my C++ background, it seems like separate forms and modules act much like private declarations do inside class calls.
I know that in C++ i can declare a friend class to grant access to communication between classes... and i'm wondering if such a procedure is possible in VBA as well...
or to break it down for those not familiar with C++, quite simply, is there a method of code i can implement to have different user forms and modules to talk to eachother? as it seems that any variable declaration is all inclusive to that one object.
The only way i can think of passing variables is by having my code create a worksheet, paste variables one by one into cells, bounce to the destination module, and import those declarations one by one through the cell contents, and remove the sheet after use...
I know i can do it that way, but i really dont want to as that is pretty darn sloppy, and a mickey mouse way to tie my program together.
View 3 Replies
View Related
Apr 19, 2008
I have been trying to use a multi-select listbox as the argument for a subroutine. For some reason, I keep getting a run time error, type mismatch. I'm baffled because when I define the specific listbox, it works fine, but when I attempt to pass the listbox I get an error.
Public Sub OtherDirectTotal(Expense As ComboBox, CostCat As ComboBox, _
Cost As TextBox, Years As Msforms.listbox)
Dim IDC As Double
Dim IDCt As Double
IDC = Val(UserForm2.txtIDC) / 100
If Years.Selected(0) = True Then
If Expense = "Sponsored" Then
If UserForm2.ChkODC = False Then
Select Case CostCat......................
View 2 Replies
View Related
Aug 30, 2006
how can i store the values of an autofilter's list in a array using VBA.
View 4 Replies
View Related
Feb 27, 2008
I've created a variable number (i) of text boxes at run time (i also named them, "txt"&i, at the same time), i'm now trying to get the values entered in these to fit into a column that has been inserted during the same sub routine. I'm having a bit of trouble refering to the text boxes though, this is the patch of code i'm struggling with:
Dim TextBox As String
For i = 0 To 2 * NumVar - 1
TextBox = "txt" & i
ActiveCell.Offset(i, 0).Value = TextBox.Value
Next i
I also tried this:
Dim TextBox As Object
For i = 0 To 2 * NumVar - 1
TextBox.Name = "txt" & i
ActiveCell.Offset(i, 0).Value = TextBox.Value
Next i
View 8 Replies
View Related
Nov 15, 2013
I have in the attached file data for three months and I am trying to create a less cumbersome way of linking the three monthly Contributions for a product to calculate its contribution for the full 3-month period. For example, Product A had contributions of 0.90%, -1.52% and -1.99% for the three months. In column H the formula used to link the monthly contributions into a 3-month value (-2.62%) is demonstrated.
As you can see in column F, the process of compounding the contributions for each product over the 3 months is a cumbersome process, and it becomes even more unwieldy if the calculation period covers more months, so either a formula or a few lines of VBA code, to link together more efficiently the contributions for any number of months?
View 1 Replies
View Related
Dec 3, 2013
I am trying to attempt to create a payroll spreadsheet with certain aspects and with multiple worksheets. so what im trying to accomplish is this
1/ on my main worksheet would have the payroll template there would be 2 columns (1) would be "routes" (2) would be the dollar value of that route.
2/ i want to be able to pick from a drop down list in colume (1) which will have various routes such as A,
B, C, D, E etc
3/ once i have selected a route in colum (1) i want to have the dollar value that is associated to that route to show up in column (2)
ex/ route A worth $1
route B worth $2
create a dropdown list but thats about it, i have not been able to link any values together.
View 11 Replies
View Related
Jan 27, 2009
I noticed that sometimes if I stop a macro midway (completely stop, not just pause), and then re-run it from the beginning, my variables will still hold the value they were last set to right before i stopped the macro. But sometimes, this doesn't happen - the variables reset.
I'm wondering what defines how long the variable's value are held for? I'm considering writing a macro that can hold variables so that even after the macro is finished, the user can re-run and the variables will still be available. This is useful mostly for text boxes in userforms that will probably have the same values the next time the user runs, but the user can still change them if he wants.
View 3 Replies
View Related
Oct 28, 2011
I have four variables a1, a2, cr and s in the macro below.
Code:
Public Const cr As Long = 4
Sub Mode5()
'4 are the constants you can vary to test different scenarios
Const a1 As Single = 0.1
Const a2 As Single = 0.4
Const s As Single = 3
It is easier if think of these variables in this format (a1, b1, cr, s).
So taking the code above as an example they can be written as (0.1, 0.4, 4, 3).
Ultimately I would like to try different combinations of these values to get a desired outcome. Currently I change the values manually each time.
Variable a can range from 0.1 - 2.5 (25 numbers)
Variable b can range from 0.1 - 2.5 (25 numbers)
Variable s can range from 1 - 10 (10 numbers)
Variable s can range from 1 -10 (10 numbers)
write some code to iterate through different combinations of these variables. I presume it would be some sort of loop which increments each variable a certain amount each time until the max value is reached.
For example the first combination would be (0.1, 0.1, 1, 0.1) and the next would be (0.2, 0.1, 1, 0.1) and the next (0.3, 0.1, 1, 0.1) etc and the last one would be (2.5, 2.5, 10, 10).
Altogether there can be 25 x 25 x 10 x 10 = 62,500 combinations.
View 5 Replies
View Related
Sep 28, 2006
I have a spreadsheet where in the past we have used a turnover period (always 12 yrs) as our constant to calculate our average factors. For example we use formulas to calculate up to 12 years and the we just restart at year 13- providing that one of the formulas does not return a 0 value. I need to be able to choose our turnover period- say 8 years and have the values start then but in the next prokect I may need a 10 year turnover.
View 3 Replies
View Related
Jun 5, 2013
The attached table mimics the massive table that I am working with.
I wanted to populate values of two variables.
For e.g., I wanted to populate the yellow portion of the table with the value "A"; blue portion with the value "B"; Green portion with the value "3/1/1993" and so on (different colors were used to aid my question only, there are no such colors in the actual table).
I have about 700 such portions to be fill in.
My approach: I just started to copy and paste manually which is tedious.
Question: Would there be any automated way to do this?
View 4 Replies
View Related