Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Range name as varible

I am having trouble understanding why this sub works when used
Range("name") but not Range(NameAsVar). See below. any ideas how to
make this work? Do I have to convert the strings passed to a range
object? How would i do that?


Sub SubstituteVals(var1name As String, var2name As String, _
var1val As Double, var2val As Double)

'Works with specific named ranges

'Range("PurchasePrice") = var1val
'Range("GrossSalesTotal") = var2val

'Does not work if range name is a varible
'Method 'Range' of Object '_Global' failed

Range(var1name) = var1val
Range(var2name) = var2val

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Range name as varible


Perhaps you need to specify the worksheet when using variables instead of
hard-coded range names?

Tim

wrote in message
oups.com...
I am having trouble understanding why this sub works when used
Range("name") but not Range(NameAsVar). See below. any ideas how to
make this work? Do I have to convert the strings passed to a range
object? How would i do that?


Sub SubstituteVals(var1name As String, var2name As String, _
var1val As Double, var2val As Double)

'Works with specific named ranges

'Range("PurchasePrice") = var1val
'Range("GrossSalesTotal") = var2val

'Does not work if range name is a varible
'Method 'Range' of Object '_Global' failed

Range(var1name) = var1val
Range(var2name) = var2val

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Range name as varible

It works fine with hard-coaded range names without a worksheet name.
Range("PurchasePrice") = var1val
Range("GrossSalesTotal") = var2val


The problem is it does not work when I replace the range name with a
string variable.
Range(var1name) = var1val
Range(var2name) = var2val

Any other ideas?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Range name as varible

Works for me (XL 2002 SP3)

Sub tester()

Const S_RNG As String = "test"
Dim sRange As String

sRange = "test"

Range("test").Value = "one"
Range(S_RNG).Value = "two"
Range(sRange).Value = "three"
End Sub

Tim


wrote in message
oups.com...
It works fine with hard-coaded range names without a worksheet name.
Range("PurchasePrice") = var1val
Range("GrossSalesTotal") = var2val


The problem is it does not work when I replace the range name with a
string variable.
Range(var1name) = var1val
Range(var2name) = var2val

Any other ideas?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Range name as varible

Thank you. That helped me see where the error was coming from.

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
same varible in formula jheiser57 Excel Worksheet Functions 0 June 21st 10 04:49 PM
varible calculation kinsey New Users to Excel 7 September 30th 07 08:50 AM
varible table not spxer Excel Worksheet Functions 3 August 4th 06 05:13 PM
varible table spxer Excel Worksheet Functions 1 August 3rd 06 08:30 PM
Varible in a Range mushy_peas[_11_] Excel Programming 3 January 17th 04 02:06 AM


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

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

About Us

"It's about Microsoft Excel"