Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
tom tom is offline
external usenet poster
 
Posts: 570
Default What does 'Set rng = Range("Database")' do?

I'm a professional programmer but new to programming Excel. I recently found
an example on the internet that has the line 'Set rng = Range("Database")' in
it. Everything works fine in the example. When I modify the code and try to
use it on my spreadsheet I get an error on the 'Set rng = Range("Database")'
line. The error is: Run-time error '1004': Method 'Range' of object
'_Global' failed. Any ideas as to why it works on the example but errors on
my spreadsheet?

Thanks,
Tom
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default What does 'Set rng = Range("Database")' do?

Hi Tom,

Set rng = Range("Database")'


sets the range object rng to a worksheet range named Database.

If you do not have a range named Database on your sheet you will get an
error message of the type that you have encountered.

The solution is either to name the requisite worksheet range in Excel or,
alternatively, define the range directly, e,g,

Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

---
Regards,
Norman



"Tom" wrote in message
...
I'm a professional programmer but new to programming Excel. I recently
found
an example on the internet that has the line 'Set rng = Range("Database")'
in
it. Everything works fine in the example. When I modify the code and try
to
use it on my spreadsheet I get an error on the 'Set rng =
Range("Database")'
line. The error is: Run-time error '1004': Method 'Range' of object
'_Global' failed. Any ideas as to why it works on the example but errors
on
my spreadsheet?

Thanks,
Tom



  #3   Report Post  
Posted to microsoft.public.excel.programming
tom tom is offline
external usenet poster
 
Posts: 570
Default What does 'Set rng = Range("Database")' do?

Norman, thanks you shed some light on my problem. I still have a question or
two. You stated
If you do not have a range named Database on your sheet you will get an error message of the type that you have encountered.

that makes perfect sense but how do I "create a range named Database" and
why don't I see anything named Database on the example?

Your suggestion of
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

also makes perfect sense however, both the number of columns and rows will
vary from time to time. How can I be sure I select all the data? Sorry to
bother everyone with such a simple question but I have wasted an entire day
on this and I am stumped!

Thanks,
Tom


"Norman Jones" wrote:

Hi Tom,

Set rng = Range("Database")'


sets the range object rng to a worksheet range named Database.

If you do not have a range named Database on your sheet you will get an
error message of the type that you have encountered.

The solution is either to name the requisite worksheet range in Excel or,
alternatively, define the range directly, e,g,

Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

---
Regards,
Norman



"Tom" wrote in message
...
I'm a professional programmer but new to programming Excel. I recently
found
an example on the internet that has the line 'Set rng = Range("Database")'
in
it. Everything works fine in the example. When I modify the code and try
to
use it on my spreadsheet I get an error on the 'Set rng =
Range("Database")'
line. The error is: Run-time error '1004': Method 'Range' of object
'_Global' failed. Any ideas as to why it works on the example but errors
on
my spreadsheet?

Thanks,
Tom




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default What does 'Set rng = Range("Database")' do?

In article ,
Tom wrote:

I still have a question or two. You stated
If you do not have a range named Database on your sheet you will
get an error message of the type that you have encountered.

that makes perfect sense but how do I "create a range named Database"
and why don't I see anything named Database on the example?


One way is to select a range, then type "Database" in the Name box on
the left side of the formula bar, then press Return.

You can do it in code with

Range("A1:A100").Name = "Database"

IIRC, Database is also a built-in name for the area associated with a
DataForm.

As to why your example didn't define it, who knows? All you said was
that you found it "on the internet". One can find al *kinds* of examples
on the internet.

Your suggestion of
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

also makes perfect sense however, both the number of columns and rows
will vary from time to time. How can I be sure I select all the
data? Sorry to bother everyone with such a simple question but I
have wasted an entire day on this and I am stumped!


How will your range vary?

If you just want to get all the contiguous data around a particular cell
(i.e., all data not separated from the cell by a blank vertical or
horizontal range), use the .CurrentRegion property of the Range object

Set rng = Range("A1").CurrentRegion
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default What does 'Set rng = Range("Database")' do?

Just another way.

Most of the time, I can pick out a column that always has entries (some
index/key perhaps). And I can pick out a row that always has entries
(headers???).

Then I can do things like:

dim myRng as range
dim LastRow as long
dim LastCol as long

with worksheets("sheet1")
lastrow = .cells(.rows.count,"A").end(xlup).row
lastcol = .cells(1,.columns.count).end(xltoleft).column
set myRng = .range("a1",.cells(lastrow,lastcol))
end with

====
Another option if you can trust to use xl's lastused cell:

dim myRng as range
with worksheets("sheet1")
set myrng = .range("a1",.cells.specialcells(xlcelltypelastused cell))
end with

But excel keeps pretty strict track of what you've ever used. If you put a
value in x99 and then clear contents, you can see what I mean if you hit
ctrl-End. You'll be taken to whatever excel thinks is the lastusedcell--not
always what you would have thought.

Sometimes just using .usedrange in code will reset that lastusedcell (sometimes
not).


dim myRng as range
with worksheets("sheet1")
set myrng = .usedrange 'try to reset usedrange
set myrng = .range("a1",.cells.specialcells(xlcelltypelastused cell))
end with



Tom wrote:

Norman, thanks you shed some light on my problem. I still have a question or
two. You stated
If you do not have a range named Database on your sheet you will get an error message of the type that you have encountered.

that makes perfect sense but how do I "create a range named Database" and
why don't I see anything named Database on the example?

Your suggestion of
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

also makes perfect sense however, both the number of columns and rows will
vary from time to time. How can I be sure I select all the data? Sorry to
bother everyone with such a simple question but I have wasted an entire day
on this and I am stumped!

Thanks,
Tom

"Norman Jones" wrote:

Hi Tom,

Set rng = Range("Database")'


sets the range object rng to a worksheet range named Database.

If you do not have a range named Database on your sheet you will get an
error message of the type that you have encountered.

The solution is either to name the requisite worksheet range in Excel or,
alternatively, define the range directly, e,g,

Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

---
Regards,
Norman



"Tom" wrote in message
...
I'm a professional programmer but new to programming Excel. I recently
found
an example on the internet that has the line 'Set rng = Range("Database")'
in
it. Everything works fine in the example. When I modify the code and try
to
use it on my spreadsheet I get an error on the 'Set rng =
Range("Database")'
line. The error is: Run-time error '1004': Method 'Range' of object
'_Global' failed. Any ideas as to why it works on the example but errors
on
my spreadsheet?

Thanks,
Tom





--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
tom tom is offline
external usenet poster
 
Posts: 570
Default What does 'Set rng = Range("Database")' do?

Thanks to all, you answered my questions and I solved my problem!

Tom

"Dave Peterson" wrote:

Just another way.

Most of the time, I can pick out a column that always has entries (some
index/key perhaps). And I can pick out a row that always has entries
(headers???).

Then I can do things like:

dim myRng as range
dim LastRow as long
dim LastCol as long

with worksheets("sheet1")
lastrow = .cells(.rows.count,"A").end(xlup).row
lastcol = .cells(1,.columns.count).end(xltoleft).column
set myRng = .range("a1",.cells(lastrow,lastcol))
end with

====
Another option if you can trust to use xl's lastused cell:

dim myRng as range
with worksheets("sheet1")
set myrng = .range("a1",.cells.specialcells(xlcelltypelastused cell))
end with

But excel keeps pretty strict track of what you've ever used. If you put a
value in x99 and then clear contents, you can see what I mean if you hit
ctrl-End. You'll be taken to whatever excel thinks is the lastusedcell--not
always what you would have thought.

Sometimes just using .usedrange in code will reset that lastusedcell (sometimes
not).


dim myRng as range
with worksheets("sheet1")
set myrng = .usedrange 'try to reset usedrange
set myrng = .range("a1",.cells.specialcells(xlcelltypelastused cell))
end with



Tom wrote:

Norman, thanks you shed some light on my problem. I still have a question or
two. You stated
If you do not have a range named Database on your sheet you will get an error message of the type that you have encountered.

that makes perfect sense but how do I "create a range named Database" and
why don't I see anything named Database on the example?

Your suggestion of
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

also makes perfect sense however, both the number of columns and rows will
vary from time to time. How can I be sure I select all the data? Sorry to
bother everyone with such a simple question but I have wasted an entire day
on this and I am stumped!

Thanks,
Tom

"Norman Jones" wrote:

Hi Tom,

Set rng = Range("Database")'

sets the range object rng to a worksheet range named Database.

If you do not have a range named Database on your sheet you will get an
error message of the type that you have encountered.

The solution is either to name the requisite worksheet range in Excel or,
alternatively, define the range directly, e,g,

Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1:D100")

---
Regards,
Norman



"Tom" wrote in message
...
I'm a professional programmer but new to programming Excel. I recently
found
an example on the internet that has the line 'Set rng = Range("Database")'
in
it. Everything works fine in the example. When I modify the code and try
to
use it on my spreadsheet I get an error on the 'Set rng =
Range("Database")'
line. The error is: Run-time error '1004': Method 'Range' of object
'_Global' failed. Any ideas as to why it works on the example but errors
on
my spreadsheet?

Thanks,
Tom




--

Dave Peterson

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
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
Pivot Table Range ("Database") Helen2245 Excel Discussion (Misc queries) 1 August 7th 08 05:33 PM
Using "Cells" to write "Range("A:A,H:H").Select" Trip Ives[_2_] Excel Programming 3 June 5th 04 03:13 PM
Change "relative" to "absolute" (database) Len Dolby[_2_] Excel Programming 4 November 3rd 03 05:36 PM


All times are GMT +1. The time now is 08:30 PM.

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"