tags:

views:

47

answers:

3

Hi,

Is there anyway to convert a string value to a Range object ? I'm having a function which takes a Range object as a argument and need to pass a single string parameter to it

Thank You

A: 

A string with a cell address? if so:

Dim r As Range: Set r = Range("B3")
MsgBox r.ColumnWidth
Alex K.
Actually it can be a any string value, not necessarily be a cell address, that is why I'm having problem in converting other string value to a Range object
nimo
Can you give some examples of the string data?
Alex K.
Dim myStirng as StringmyString = "AnyValue"
nimo
Ah reread your Q, so you have a function that expects a Range() but you can only pass a string? Can you change the function proto to make the Range param optional?Why can you only pass a string? Does the function work if the range it expects isn't valid?
Alex K.
A: 

I don't like this one bit, but if you can't change the function that requires a range, you could create a function that converts a string to a range. You'd want to be sure that the only thing the first function cares about is the Value or Text properties.

Function FuncThatTakesRange(rng As Range)

    FuncThatTakesRange = rng.Value

End Function

Function ConvertStringToRange(sInput As String) As Range

    Dim ws As Worksheet

    Set ws = Workbooks.Add.Sheets(1)

    ws.Range("A1").Value = sInput

    Set ConvertStringToRange = ws.Range("A1")

    Application.OnTime Now + TimeSerial(0, 0, 1), "'CloseWB """ & ws.Parent.Name & """'"

End Function

Sub CloseWb(sWb As String)

    On Error Resume Next
        Workbooks(sWb).Close False

End Sub

Use in the Immediate Window like

?functhattakesrange(convertstringtorange("Myvalue"))
Dick Kusleika
A: 

Why not change the function argument to a variant and then in the function determine Using VarType etc) if you have been passed a Range and use error handling to check for a string which can be converted to a range or a string that cannot be converted to a range ?

Charles Williams