Make a string equal to what a named range refers to? - vba

I have a named range called tempPrintArea
it refers to ="A1:J59"
I created it with VBA and the scope became local. I don't know if the scope matters.
I used this line to create the named range:
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
Now I want to set the value of a string to "A1:J59". I've tried the following:
Dim test As String
test = Range("tempPrintArea").RefersTo
but I get the error message Method 'Range' out of object '_Gloabl' failed
What can I change in these code lines to get this working?

When you're using
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
code doesn't create named range that refers to A1:J59, instead code creates named range with text "A1:J59".
For creating named range use this one instead:
wks.Names.Add Name:="tempPrintArea", RefersTo:=wks.Range("A1:J59")
and then
Dim test As String
test = wks.Range("tempPrintArea").Address(False, False) ' returns A1:J59

Related

Used named ranges in VBA

from1,from2,from3,to1,to2,to3 are all pre-defined named ranges. They all have the same dimension. I'm just trying to replace all the to's with all the from's.
The following code keeps giving me a type mismatch error on this line
ThisWorkbook.Names(to_ranges(i)) = ThisWorkbook.Names(from_ranges(i))
Could someone help? Thank you!
Sub named_ranges()
Dim from_ranges() As Variant, to_ranges() As Variant
from_ranges() = Array("from_1", "from_2", "from_3")
to_ranges() = Array("to_1", "to_2", "to_3")
Dim i As Integer
For i = 0 To UBound(from_ranges)
ThisWorkbook.Names(to_ranges(i)) = ThisWorkbook.Names(from_ranges(i))
Next i
End Sub
When you define a named range you define it's scope (worksheet or workbook). So:
Range(to_ranges(i)) = Range(from_ranges(i))
Will probably work just fine as the default scope for a named range is "Workbook".
That being said, you can use the .Names object here as well, you just need to get it's .RefersToRange property otherwise you aren't copying a range, instead you are copying the default property of the Name object which is its "RefersToRange" which is just a string.
Essentially you are doing:
Names(to_ranges(i)).RefersToRange = Names(from_ranges(i)).RefersToRange
Which is incorrect.
Instead (and this is total overkill compared to the first example above):
Range(Names(to_ranges(i)).RefersToRange).Value = Range(Names(from_ranges(i)).RefersToRange).Value
And... ultimately this doesn't solve your initial concern of Name scopes (workbook vs worksheet) as the RefersToRange will be scoped as it was was defined and may still end up hitting your ActiveSheet if it improperly scoped AND left unqualified here.

defining code on vba excel to simplify code writing process

I am attempting to reduce the amount of clutter on my code by creating "shortcuts" if you will
For instance, I always have to type
ThisWorkBook.ActiveSheet.Range
Is there a way for me to define the above to create a less wordy macro? I have tried convert to range and string and the former returns an error (but I could still get intellisense recognize and attempt to autofill) while the string version doesnt work.
Just like in any programming language, you can use variables to store data
For example:
Dim myrange As Range: Set myrange = Sheets("Sheet1").Range("B5")
Alternatively, if you will be working with the same object multiple times, you can use the With keyword
For example. instead of writing you want to work with table every time on every new line you can do
With Sheets("Sheet1").ListObjects("Table1")
.ListRows.Add
.ListColumns(2).Range(3) = "Hello World!"
' ... and so on
End With
Also, please on a sidenote: Avoid using Select/ActiveSheet/ActiveWorkbook and so on!
More info on how to here
You can create functions or customized properties, which are always evaluated when called
Property Get pARng As Range
Set pARng = ThisWorkBook.ActiveSheet.Range
End Property
Function fARng As Range
Set fARng = ThisWorkBook.ActiveSheet.Range
End Function
'Usage
Sub xxx
'...
pARng.Rows(1).Activate
'Same as ThisWorkBook.ActiveSheet.Range.Rows(1).Activate
fARng.Rows(1).Activate
'using function instead achieves same result
End Sub

A range variable used as an argument

I'm writing a VBA function that outputs a range object. After I initialize the range object, I have trouble trying to use said object further as an argument (or, property). Here is what I'm trying to do:
Set HelpRange = Worksheets("sheet1").Cells(StartRow + Counter, StartCol) 'works fine
Worksheets("sheet1").HelpRange.something ...
I'm obviously aiming for worksheets(a).range(b), how can I do this when range(b) is a variable? It's obviously not working my way.

Retrieve a string value from an Object / Class?

Dim tc As Cells = newWorksheet.Cells
Dim tccell As Cell
tccell = tc.Find("PT9", Nothing, findOptions)
Note: The “tccell” object declared above As “type Cell” contains a string “J1” that I want to retrieve.
Using VS2010 when I inspect “tccell” it contains the following:
{Aspose.Cells.cell [J1; ValueType: IsString; Value: ABCD]}
How can I extract the value “J1” from “tccell”?
If I try to display tccell:
MsgBox(tccell) -->Argument 'Prompt' cannot be converted to type 'String'.
How can I use the result from the Find Method as shown above?
Thanks
The Cell class has Row and Column properties. If you want those properties in the alphanumeric format, use one of the CellsHelper methods to get that:
string cellname = CellsHelper.CellIndexToName(row, col)
You might also find that the alphanumeric location of the cell already resides in its Name property.

Error when trying to cast db query to string

I am trying to extract one integer from the database but am getting errors
My code in the controller is:
Dim SeqNumQuery = From a In db.RequestedServices
Where a.RequestedServiceId = id
Select a.StationId
Dim StationId As Integer = SeqNumQuery.ToString
And error is:
Range variable 'StationId' hides a variable in an enclosing block or a range variable previously defined in the query expression.
I think the issue is that your variable, StationId, is being declared somewhere already within the same scope. Try changing to:
Dim SeqNumQuery = From a In db.RequestedServices
Where a.RequestedServiceId = id
Select a.StationId
StationId = SeqNumQuery.ToString
But look into what the other variable is being used for. If you need it, use something other than StationId for your variable name, for example RequestedServicesStationId.
More information regarding this error:
Range variable hides a variable in an enclosing block or a range variable previously defined in the query expression.
On a side note, I don't understand why you are using .ToString extension method. This won't cause your script to throw an exception, however I would recommend changing this to CInt():
StationId = CInt(SeqNumQuery)