Situation: I'm using Excel 10. I have a named variable that uses a formula to compute it's value. This is a named variable, not a named range (in the 'Name Manager' the name "MaxDate" refers to "=MAX(Sheet1!B:B)" where column B is a list of dates). The computed value does not appear in any cells by itself, but rather is used in various formulas within the spreadsheet.
My problem: How can I reference this named variable in VBA code? Using range("MaxDate").value does not work. I know I can simply put the value into a cell and reference the cell, but I'd like to find a way to reference the variable directly. Any ideas?Thanks.
Here's some examples of working with your named variable:
Sub TestNamedVariable()
Dim varFormula As String
Dim varValue As Variant
'This will print the formula if you need it:
varFormula = ActiveWorkbook.Names("MaxDate")
'This will evaluate the named variable/formula
varValue = Application.Evaluate("MaxDate")
End Sub
For a given workbook with a Named variable "MyVar", valued "=MAX(Sheet1!B:B)".
Try the following code:
Evaluate(ActiveWorkbook.Names("MyVar").RefersTo)
Replace the ActiveWorkbook with the object you're referring if you need to.
Related
I'm trying to loop through the multiple named ranges, every range is just one cell, on a column, so it starts with item1, item2...item100.Each of these ranges has a cell reference in it (like "AM100").
I want to be able to set up a for/while loop that just selects(and does something) each of the referenced cells in those named ranges.
I'm stuck at VBA not considering "item"&"1" the same with "item"&"i" when i =1 and from what I deduct this is purely a data type issue. It's able to refer to a pure string range but when it's variant/string type (which any concatenation of string or converted string variable results in).
item1 range has in it $AM$10 reference. That cell, has a value in it. Eventually I want to change the values in multiple similar cells by referring to the name ranges that hold their reference.
Without the "for" loop, I've tested the following:
Sub test()
Dim i as integer
i=1
'These don't work
Range([indirect("item"&CSTR(i))]).Select
Range([indirect("item"&i)]).Select
'What works is, but this is not useful since I want to loop through i:
Range([indirect("item" & "1")]).Select
Range([indirect("item1")]).Select
Sub Test()
Dim oDefName As Name
For Each oDefName In ThisWorkbook.Names
If Left(UCase(oDefName.Name), 4) = "ITEM" Then
Range(oDefName.RefersToRange.Value).Select
End If
Next
End Sub
Note: There is no error checking to ensure that the value within the named range is actually a cell reference.
Edit- Above is how I would solve the problem. Indirect is an in cell function and not usable directly in vba. Below is how you would get the indirect functionality in VBA with a counter.
Range(Range("Item" & oCounter).Value).Select
I am trying to split my workbook, however when I do so the named ranges in the new workbooks refer to the original workbook. I do not want this, as I have copied the source for these references into the new workbooks as well. Thus I want to delete the reference to the old workbook in the named range. I have tried to change this by replacing the reference to nothing, however it does not work. What am I doing wrong or is there any other way? I have also tried to delete the named range and replace them by new named ranges, but the refers to part of the named range is a function with =offset(...). This does not seem to work either in VBA.
Sub RenameNamedRanges()
Dim all_names, n
all_names = Array("1stNamedRange", "2ndNamedRange", "3rdNamedRange")
For Each n In all_names
Names(n).Replace (RefersTo, "[Original_workbook.xlsm]", "")
Next
End Sub
Thanks in advance!
One way would be to delete the named range, and then create a new one.
Names(n).Delete
ActiveWorkbook.Names.Add _
Name:="newNamedRange", _
RefersTo:="=Sheet1!$A$1:$D$3"
Can check the output of what the existing range refers to and assign to a variable to use when adding the new range.
Debug.Print(Names(n).RefersTo)
Can also use the Replace function to replace part of the string you want to replace
Replace(Names(n).RefersTo, "[Original_workbook.xlsm]", "")
How do I reference/select a named range based on a string variable with the text being the name ranged name?
Example:
'Target is range variable which is set to cell that is double clicked.
'For this example lets say the cell value is "A1A"
Dim binName As String
binName = "B1" & Target(1).Value
MsgBox(binName) 'This displays "B1A1A" which is the name of the named range
Range(binName).Select 'I want to select the range with the name B1A1A
This gives me
Run-time error '1004':
Method 'Range' of object'_Worksheet' failed
on the last line of the above code.
I understand that Range() is looking for an object, but I dont know how to refer call a range name from a string variable.
Thank in advanced for any help.
Resolution Edit:
Per Davids recommendation below, I changed
Range(binName).Select
to
Application.Goto ThisWorkbook.Worksheets("B1").Range(binName) 'where "B1" is the worksheet name
It's possible to have a Workbook scoped range that is still explicitly assigned to a single worksheet, which I gather is what you want (Name refers to some range on some other worksheet, etc.).
If you're doing this as UI, rather than Select try using the following:
Application.GoTo Range(binName)
Or:
Application.GoTo [binName]
Tested, and seems to work even when a range is explicitly on another worksheet, Application.GoTo takes care of toggling to that sheet and selecting that range.
Application.Evaluate(binName).Select
Application Evaluate will evaluate a string and resolve the reference. It accepts strings as arguments, and doesnt need to resolve to a range (for example, Application.Evaluate("10") would result in a return of 10).
For more information, check out the MSDN documentation: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-evaluate-method-excel.
EDIT: Worth noting, this isnt properly error handled, so it could potentially return a Nothing reference, or an error, if the input string is not a valid named range.
Additionally, ThisWorkbook.Sheets("SomeRangeName").Select should work (and does work) just fine. It is more likely that there is something wrong with the name of your range.
Evening you fine people!
I am struggling with a particular issue, there are two separate solutions that I have identified but I haven't specifically solved them
I have a list of 37 sheet names in a sheet (A1:A37) and the code I want to run is shown below - I don't know how to set 'z' to the particular cell reference - for example if A1 was Sheet1 I want Z to be Sheet1 and work as a variable. I am using a For loop to loop through the cells.
Workbooks("ED Test.xlsx").Sheets(z).Range("E2:E21").Value = Workbooks("TPT.xlsm").Sheets(z).Range("A2:A21").Value
The second method, more messy was to have the variables set within VBA and using the For loop (i.e. For x = 1 to 37) to concatenate the two values into a variable (e.g. "Sheet" and x) When I do this it gives a different error as it treats the concatenation as a string and not a variable
Please halp :)
You'll need a loop. If you are trying to set E2:E21 in each worksheet to whatever is in that same worksheet's (but in another workbook) range A2:A21 you will loop through those sheets and do pretty much what you have above:
Sub dothething()
Dim cellSheetName As Range
'loop through all the cells holding sheet names in sheet1 (assuming here)
For Each cellSheetName In Sheet1.Range("A1:A37").Cells
'Copy the values in whatever sheet we found
'Noting that the sheetname is held in the cell's value (cellSheetName.value)
Workbooks("ED Test.xlsx").Sheets(cellSheetName.Value).Range("E2:E21").Value = Workbooks("TPT.xlsm").Sheets(cellSheetName.Value).Range("A2:A21").Value
Next cellSheetName
End Sub
This could get more robust, but it's a good starting point.
I have some VBA code that works on named ranges that are set up externally. How can I get the actual cell reference from this range? For example
Dim rangeID As String
rangeID = ActiveSheet.Range("MyNamedRange").UnknownFunction
Sets rangeID to "CG13" if the named range "MyNamedRange" refers to CG13
ActiveSheet.Range("MyNamedRange") already is the "actual cell reference." It references the cell object. You should directly use that object reference anywhere you need a cell in your code.
The textual representation of the address of that cell is ActiveSheet.Range("MyNamedRange").Address. You can provide parameters to get the address in the form you want (e.g. to get "CG13" you call Address(False, False, xlA1)).