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

Why doesn't this code work?:

Sub test1()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range

Sheets("Sheet2").Activate
Set ws2 = Worksheets("Sheet2")
Set ws1 = Worksheets("Sheet1")
Set rng1 = ws1.Range(Cells(3, 1), Cells(7, 1))
Set rng2 = ws2.Range(Cells(5, 5), Cells(5, 5))

When it gets to the "Set rng2 ..." line it give a "Runtime error '1004' ...."
It seems strange that it works for the "Set rng1" line without problem.

Also, I can't seem to find a good reference that will help me learn the
idiosyncracies of setting/naming ranges, worksheets, and workbooks.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default setting range names

try:
Set rng1 = Range(ws1.Cells(3, 1), ws1.Cells(7, 1))
Set rng2 = Range(ws2.Cells(5, 5), ws2.Cells(5, 5))

--
p45cal


"br549" wrote:

Why doesn't this code work?:

Sub test1()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range

Sheets("Sheet2").Activate
Set ws2 = Worksheets("Sheet2")
Set ws1 = Worksheets("Sheet1")
Set rng1 = ws1.Range(Cells(3, 1), Cells(7, 1))
Set rng2 = ws2.Range(Cells(5, 5), Cells(5, 5))

When it gets to the "Set rng2 ..." line it give a "Runtime error '1004' ...."
It seems strange that it works for the "Set rng1" line without problem.

Also, I can't seem to find a good reference that will help me learn the
idiosyncracies of setting/naming ranges, worksheets, and workbooks.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default setting range names

br549 wrote:
Why doesn't this code work?:

Sub test1()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range

Sheets("Sheet2").Activate
Set ws2 = Worksheets("Sheet2")
Set ws1 = Worksheets("Sheet1")
Set rng1 = ws1.Range(Cells(3, 1), Cells(7, 1))
Set rng2 = ws2.Range(Cells(5, 5), Cells(5, 5))

When it gets to the "Set rng2 ..." line it give a "Runtime error '1004' ...."
It seems strange that it works for the "Set rng1" line without problem.

Also, I can't seem to find a good reference that will help me learn the
idiosyncracies of setting/naming ranges, worksheets, and workbooks.


Try this:

*********************
Sub test()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim rng1 As Range, rng2 As Range

Set ws1 = ActiveWorkbook.Worksheets("Sheet1")
Set ws2 = ActiveWorkbook.Worksheets("Sheet2")

Set rng1 = ws1.Cells(3, 1).Resize(5, 1)
Set rng2 = ws2.Cells(5, 5).Resize(1, 1)

End Sub
*********************

As for your problem, if you would have done something like this:

Set rng1 = ws1.Range(ws1.Cells(3, 1), ws1.Cells(7, 1))

It would have worked.

CoRrRan
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default setting range names

You will need this:

Dim ws2 As Worksheet
Dim rng2 As Range

Set ws2 = Worksheets(2)

With ws2
Set rng2 = .Range(.Cells(5, 5), .Cells(5, 5))
End With

Note the dots before Cells.


RBS

"br549" wrote in message
...
Why doesn't this code work?:

Sub test1()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range

Sheets("Sheet2").Activate
Set ws2 = Worksheets("Sheet2")
Set ws1 = Worksheets("Sheet1")
Set rng1 = ws1.Range(Cells(3, 1), Cells(7, 1))
Set rng2 = ws2.Range(Cells(5, 5), Cells(5, 5))

When it gets to the "Set rng2 ..." line it give a "Runtime error '1004'
...."
It seems strange that it works for the "Set rng1" line without problem.

Also, I can't seem to find a good reference that will help me learn the
idiosyncracies of setting/naming ranges, worksheets, and workbooks.


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
converting email address names in a range of cells to real names John Excel Worksheet Functions 1 May 19th 10 03:44 PM
Setting Tab Names To Dates In Spreadsheet Chris Excel Worksheet Functions 2 February 5th 06 03:37 AM
Setting Tab Names To Dates In Spreadsheet Chris Excel Worksheet Functions 1 February 4th 06 04:02 PM
Setting file names paulu Excel Programming 3 July 8th 05 08:14 PM
Setting variable names in code Frank Kabel Excel Programming 0 May 20th 04 04:39 PM


All times are GMT +1. The time now is 02:22 PM.

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"