Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default string to range

Hi folks,

Sorry, it's been a while since I've done this and can't seem to find
the question answered elsewhere. Basically I have a line of code that
creates a string from a variable. What I'm struggling with is to set a
range object variable to the range suggested by the string.

Sub Test (sColLetter As String)
Dim rng As Range
Dim sRng As String

sRng = "ThisWorkbook.Sheets(" & Chr(34) & "InputMatrix" & Chr(34) &
").Range(" & Chr(34) & sColLetter & "7:" & sColLetter & "110" &
Chr(34) & ")"

Set rng = ?

End Sub

If sColLetter happens to be "F", then sRng reads:
ThisWorkbook.Sheets("InputMatrix").Range("E7:E110" )

I've tried Set rng = Range(sRng) to no avail....

Any help much appreciated...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default string to range

First, this seems like a pretty unusual question.

I take it that you can't use something like:

Option Explicit
Sub Test(sColLetter As String)
Dim rng As Range

Set rng = ThisWorkbook.Worksheets("inputmatrix") _
.Cells(7, sColLetter).Resize(94, 1)

Debug.Print rng.Address(external:=True)

End Sub
Sub test1()
Call Test("F")
End Sub

==========
I'm gonna guess that you're parsing/building something else and it's a lot more
painful than the simple example you showed.

If that's true, then maybe this would help:

Option Explicit
Sub Test(sColLetter As String)
Dim rng As Range
Dim sRng As String

sRng = "'[" & ThisWorkbook.Name & "]inputmatrix'!" _
& sColLetter & "7:" & sColLetter & "110"

Set rng = Application.Range(sRng)

Debug.Print rng.Address(external:=True)

End Sub
Sub test2()
Call Test("F")
End Sub

Notice that it's using application.range()--not just Range().

=============
This line:
Set rng = ThisWorkbook.Worksheets("inputmatrix") _
.Cells(7, sColLetter).Resize(94, 1)
could also be written as:

with thisworkbook.worksheets("inputmatrix")
Set rng = .range(.Cells(7, sColLetter), .cells(110, sColLetter))
end with

or

set rng = thisworkbook.worksheets("inputmatrix") _
.range(sColLetter & "7:" & sColLetter & 110)

Whichever you find the easiest to modify...




gromit12 wrote:

Hi folks,

Sorry, it's been a while since I've done this and can't seem to find
the question answered elsewhere. Basically I have a line of code that
creates a string from a variable. What I'm struggling with is to set a
range object variable to the range suggested by the string.

Sub Test (sColLetter As String)
Dim rng As Range
Dim sRng As String

sRng = "ThisWorkbook.Sheets(" & Chr(34) & "InputMatrix" & Chr(34) &
").Range(" & Chr(34) & sColLetter & "7:" & sColLetter & "110" &
Chr(34) & ")"

Set rng = ?

End Sub

If sColLetter happens to be "F", then sRng reads:
ThisWorkbook.Sheets("InputMatrix").Range("E7:E110" )

I've tried Set rng = Range(sRng) to no avail....

Any help much appreciated...


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default string to range

On Feb 18, 5:25*pm, Dave Peterson wrote:
First, this seems like a pretty unusual question.

I take it that you can't use something like:

Option Explicit
Sub Test(sColLetter As String)
* * Dim rng As Range

* * Set rng = ThisWorkbook.Worksheets("inputmatrix") _
* * * * * * * * * .Cells(7, sColLetter).Resize(94, 1)

* * Debug.Print rng.Address(external:=True)

End Sub
Sub test1()
* * Call Test("F")
End Sub

==========
I'm gonna guess that you're parsing/building something else and it's a lot more
painful than the simple example you showed.

If that's true, then maybe this would help:

Option Explicit
Sub Test(sColLetter As String)
* Dim rng As Range
* Dim sRng As String

* sRng = "'[" & ThisWorkbook.Name & "]inputmatrix'!" _
* * * * * & sColLetter & "7:" & sColLetter & "110"

* Set rng = Application.Range(sRng)

* Debug.Print rng.Address(external:=True)

End Sub
Sub test2()
* * Call Test("F")
End Sub

Notice that it's using application.range()--not just Range().

=============
This line:
* * Set rng = ThisWorkbook.Worksheets("inputmatrix") _
* * * * * * * * * .Cells(7, sColLetter).Resize(94, 1)
could also be written as:

with thisworkbook.worksheets("inputmatrix")
* * Set rng = .range(.Cells(7, sColLetter), .cells(110, sColLetter))
end with

or

set rng = thisworkbook.worksheets("inputmatrix") _
* * * * * * .range(sColLetter & "7:" & sColLetter & 110)

Whichever you find the easiest to modify...





gromit12 wrote:

Hi folks,


Sorry, it's been a while since I've done this and can't seem to find
the question answered elsewhere. Basically I have a line of code that
creates a string from a variable. What I'm struggling with is to set a
range object variable to the range suggested by the string.


Sub Test (sColLetter As String)
Dim rng As Range
Dim sRng As String


sRng = "ThisWorkbook.Sheets(" & Chr(34) & "InputMatrix" & Chr(34) &
").Range(" & Chr(34) & sColLetter & "7:" & sColLetter & "110" &
Chr(34) & ")"


Set rng = ?


End Sub


If sColLetter happens to be "F", then sRng reads:
ThisWorkbook.Sheets("InputMatrix").Range("E7:E110" )


I've tried Set rng = Range(sRng) to no avail....


Any help much appreciated...


--

Dave Peterson- Hide quoted text -

- Show quoted text -


Dave, thank you very much for the help with this. Both options are an
education for me. Much appreciated!
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to select range from active cell range name string aelbob Excel Programming 2 July 14th 08 09:19 PM
Find the POSITION IN A RANGE of text in a string that matches value(s) in a range Cornell1992 Excel Programming 0 March 14th 06 07:19 PM
Retrieving range string from named range clapper Excel Programming 4 October 13th 05 03:09 PM
Translate range name passed as string to a custom function to range addresses! agarwaldvk[_25_] Excel Programming 3 September 7th 04 12:47 PM
Passing a String in Array to Range as String [email protected] Excel Programming 2 September 1st 04 01:13 AM


All times are GMT +1. The time now is 11:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"