Amending VBA Sub / Function To Pass Results To Other Subs
Feb 5, 2014
Basically someone has coded a VBA script to pull a users details from AD when a button is clicked, using the staff id as the search criteria.
I've amended the criteria so it uses Environ$(USERNAME) to find details of the current logged on user, but i am struggling to get the resulting information out of the Sub to be used in other Subs! It currently just fills a cell with the required info.
I have a simple/dumb question... How do you "capture" a value that is returned by a custom function.
I have tried searching the forums for this & I know that I should by all rights know how to do this by now... but I just can't figure it out.
Here is a Function that I copied from Ktrasler in this thread: Week Numbers
Public Function MyWeek(DateArg As Date) As Byte
Const BaseDate = "30/12/2001"
DateArg = CDate(DateArg) - (Weekday(DateArg) - 1) MyWeek = Int(((DateArg - CDate(BaseDate)) / 7) Mod 52) If MyWeek = 0 Then MyWeek = 52
End Function
I know how to pass variables to Functions, and tried this one out & it worked splendidly for my needs, but how do I take the value of "MyWeek" and use it in the subroutine that I am calling it from?
This is the code after editing to make it more clear
Public Sub 1() BookA= activeworkbook. name BookB=Application.Workbooks.Add Workbook(BookB).activate End Sub
Public Sub 2() BookB=Activeworkbook.name
With BookB. sheets(1) .range("A1")=BookA.sheets(1).range("B1") End Sub()
At the end of public sub 1, BookB is the active workbook. What I want to do in public sub 2 is to copy some data from BookA to BookB. Unfortunately, when moving from public sub 1 to public sub 2, BookA needs to be defined again. The code above is the code that I use in my add-ins. I figured out for non add-ins code I can define BookA with thisworkbook.name when BookB is active as I before work with BookA. This does not apply for add-ins as thisworkbook will refer to my add-ins code. Is there anyway of keeping definition of BookA is constant from one public sub to another public sub? This is simplified code. In fact, I can't merge public sub 2 with public sub 1 due to some reason which I don't say it here.
I have a series of procedures I am calling from a function that each run fine when executed individually. These procedure are titled: DataValidation, Test1, Test2
The function will not run past Test2- I do not get the msgbox "Stage 2"
Code: Public ArrayD Public ArrayD2 As Variant Public RngD3 As Range
I am attempting to call a Function from a cell and pass it a range and an Integer. The Integer works fine, but I cannot get the Range to pass into the function correctly.
I have a simple function defined in one of my worksheets (Sheet1):
Function AddFuel(fuel As String) MsgBox fuel End Function
How would I be able to call this from a form button event?
Private Sub CommandButton1_Click() Sheet1.AddFuel(TextBox1) End Sub
Everytime I try running this code I receive the error: Run-time error '1004'; Application-defined or, Object-defined error. I've even tried Application.Run("Sheet1.AddFuel", TextBox1) but still no luck. I think this is a pretty common question but I couldn't find any answer to it on the forums.
I understand that the SumProduct doesn't have the same capabilities in VBA as it does in worksheet formulas. I am trying to construct it in an evaluate statement in VBA but the problem is I am using it in a userform, so the conditionals are passed from two combo boxes. Here is the worksheet formula
The values C3 and M3 refer to a cell known as "Home" & "Away". It's comparing to the two teams selected to a table with game stats to show wins and losses. The user form has a combo box list with those teams. Combo Box 1 passes to a variable known as hTeam and Combo Box 2 passes to a variable known as aTeam. I need those values passed through to C3 and M3. Here's what I constructed but get a type mismatch
I am trying to write a User Defined Function in VBA to perform a simple two-dimensional table lookup. I have the temperature distribution in a solid given in a table in Sheet2. The first column of the table contains the time values, the first row contains the spacial values (radii), and the intersections contain temperature values. These ranges are named times, radii, and temperatures. In Excel, I can perform the lookup using: =index(temperatures, match(time, times, 1), match(radius, radii, 1))
But how do you do this in VBA? More specifically, how do you deal with passing ranges to the WorksheetFunctions?
Function temperature_ref(time_range As Range, radii_range As Range, temperatures_range As Range, time As Double, raduis As Double)
r = WorksheetFunction.Match(time, time_range, 1) c = WorksheetFunction.Match(radius, radii_range, 1) temperature = WorksheetFunction.Index(temperatures_range, r, c) End Function
This function would be called (from any worksheet) as..................
I have 5 worksheets each with tables that are formatted the same on each sheet. I have named each table as a named range (ex:filter5tsd15BSF). I have a userdefined function that interpolates values for a single table in a vba module. I can make this function work for a single table by calling the function as =Linterp(filter5tsd15BSF,C12) so I know the function is OK. However, I really rather program this some way that all the tables get passed to the function and then a select case for the conditions for which filter and which TSD (15 or 25) will then select the correct table.
I am trying to call a function that calculates forecast error (wMAPE). This function needs to be able to handle passing in non-contiguous ranges. I can't seem to figure out how to do that.
Function wMAPE(Forecasts As Range, Actuals As Range, Weights As Range) As Variant Dim Denominator As Double Dim Numerator As Double Dim i As Long Dim Fcst As Variant Dim Act As Variant Dim Wt As Variant
If Forecasts.Cells.Count Actuals.Cells.Count Then MsgBox ("Error: Arrays not same size") If Forecasts.Cells.Count Weights.Cells.Count Then MsgBox ("Error: Arrays not same size")
i have this formula below which works but if the criteria is not there it says FALSE because of the B22:C45,2,0) bit i would rather it says something like "no dissertation" is that possible? ...
I am using Excel 2003 and I have two columns of data, one column shows how much work for a particular worktype should be done in an hour and the second column shows what I’m expected to get in over the next month. I have added a third column to show how long it would take to complete the expected work.
What I would like that third column to show the data in hours and minutes rather than a percentage of 100, so on a linked TAB on the main sheet it will show clearly as a time so staff hours can be allocate to a worktype. Is this simple to do. I have attached an example workbook.
Attached is my sample data Sample Data_Combine Sheets.xlsm.
What the code in the attached sheet does is that it combines the data in Sheets Set1 and Set2 into the worksheet named "Combine".
Both input sheets share same columns. Code I use is as below:
VB: Sub BuildPivotData() Dim wrsht As Variant Dim i As Integer
Sheets("Combine").Range("A2:AI500").Clear wrsht = [{"Set1", "Set2"}] For i = 1 To UBound(wrsht) Sheets(wrsht(i)).[a2].CurrentRegion.Offset(1).Copy Sheets("Combine").Range("A65536").End(xlUp)(2) Next i End Sub
I want to amend the output in such a way that when the macro is run , in the "Combine" worksheet an additional column is added at the start(col A) with header as "Category".
The category should be nothing but the input Sheet name i.e Set1 or Set2.
I'm using the following phrase out of a much longer expression to demonstrate that the return value of the MID function seems to be nothing I can understand.
This expression:
=MID("6/10x",1,FIND("x","6/10x)-1))
returns 6/10 as text which makes sense. However I was under the impression that when text in Excel contains numeric characters, it can be interpreted as a number. So then how does the following expression,
=MID("6/10x",1,FIND("x","6/10x)-1)*1
return the result 38878? Does it still think it's text?
I am looking for a formula that will yield the following: IFAH9>AI9 return the word Green, IFAH9=AI9 return the word Blue, IFAH9<AI9 return the word Yellow. If AI9 is blank stay blank.
I am trying to sum the details from sheet2 in sheet3. I am currently using a plain formula to sum-up the values. However, create a indirect and vlookup function to sum the values.
In sheet3, I have to sum the values from sheet2 based on the contacts. Sheet1, sheet2 and Sheet3 are connected and any update(insert,delete.. etc) will be made in sheet 1 only. Sheet2 automatically updates based on sheet1. Similarly, Sheet3 should also update based on sheet1 and sum the values based on contacts from sheet2.
I have added the formula which I am currently using in the sheet.
I have a macro that loops through a column of cells to retrieve a price that is buried in a string. The cells in this column are formatted one of two ways. Either they are completely empty, or the following pattern of string is entered: "$181.27 ea. for 12 on 5/21/14" (the quotes are not part of the string entered). What I need my macro to do is pull out "$181.27" and then drop the dollar sign and then do some more math before inputting a result in a different cell, but the prices range from less than a dollar to over $100,000 so I need to use split instead of left. Should be simple, yes? Here is what I have:
[Code] ......
But I get an error 9: subscript out of range on the line that calculates PriceSplit whenever it encounters a blank cell.
I'm trying to have three different possible results from one formula based on one cell.
If A1 is greater than 10 I want B1 show 5, AND if A1 is less than -10 I want B1 to show -5, AND if A1 is between 9 and -9 I want B1 to show the value of A1/2.
I am forecasting some numbers and I wanted to see which function would be best suited for my task. I used the TREND function and then the FORECAST, I was expecting the same results but appeared to have slight differences.
Using data of unknown origin in Excel I found Isblank was giving a FALSE result on some apparently blank cells while giving a TRUE result on others.
Even if I used TRIM and CLEAN functions on the offending data and pasted the resulting values back the the original locations, the Isblank result was still FALSE. Also, the font colour was not set to white or transparent.
However, if I selected the cell with the 'invisible' data, clicked in the Formula Bar and pressed Enter without entering any new data, the problem disappeared for that cell. The problem also disappeared if I selected the problem 'blank' cell and pressed delete.
Can anyone explain this 'invisible data' and tell me how I can detect it using a function or formula?