Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Accessing worksheet cells from VBA macro?

Hi.

How can I access a range of cells or a particular cell from within a
VBA macro? Can I pass a worksheet range in as a parameter to the
macro? If I can, is there a way I can access a cell offset from an
item in the range, i.e., do something like the OFFSET worksheet
function from within a VBA macro?

Thanks!

Ken
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Accessing worksheet cells from VBA macro?

Hi Ken
many questions :-)
0. You may consider buying a good VBA book :-)
1. Access a range - Some examples:
sub foo ()
dim rng as range
dim cell as range
set rng = selection 'set rng to the current range selection of the
active sheet

set rng = Range("A1:A100")
set rng = range(cells(1,1),cells(5,5))
for each cell in rng
msgbox cell.value
next
end sub

2. Pass a worksheet range - call this function in your worksheet with
=FUNC_FOO(A1:A10)
Public function func_foo(rng as range)
dim cell as range
dim ret_value
for each cell in rng
ret_value = ret_value + cell.value 'a simple sum function
end if
func_foo = ret_value
end function

3. Offset function - Range.offset(row_index,col_index)

dim rng as range
set rng = range("B1")
rng.offset(1,1).value = "test" 'one cell up, one cell to the right





--
Regards
Frank Kabel
Frankfurt, Germany

Ken wrote:
Hi.

How can I access a range of cells or a particular cell from within a
VBA macro? Can I pass a worksheet range in as a parameter to the
macro? If I can, is there a way I can access a cell offset from an
item in the range, i.e., do something like the OFFSET worksheet
function from within a VBA macro?

Thanks!

Ken


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Accessing worksheet cells from VBA macro?

Thanks! That's exactly what I was looking for.

Are there any VBA books that you'd recommend? My only hesitation with
that is they tend to be very expensive, quickly antiquated, and my
need for using VBA is infrequent. Still, if there is a good reference
you'd recommend, perhaps I can find a used copy out there.

Thanks again!

- Ken

"Frank Kabel" wrote in message ...
Hi Ken
many questions :-)
0. You may consider buying a good VBA book :-)
1. Access a range - Some examples:
sub foo ()
dim rng as range
dim cell as range
set rng = selection 'set rng to the current range selection of the
active sheet

set rng = Range("A1:A100")
set rng = range(cells(1,1),cells(5,5))
for each cell in rng
msgbox cell.value
next
end sub

2. Pass a worksheet range - call this function in your worksheet with
=FUNC_FOO(A1:A10)
Public function func_foo(rng as range)
dim cell as range
dim ret_value
for each cell in rng
ret_value = ret_value + cell.value 'a simple sum function
end if
func_foo = ret_value
end function

3. Offset function - Range.offset(row_index,col_index)

dim rng as range
set rng = range("B1")
rng.offset(1,1).value = "test" 'one cell up, one cell to the right





--
Regards
Frank Kabel
Frankfurt, Germany

Ken wrote:
Hi.

How can I access a range of cells or a particular cell from within a
VBA macro? Can I pass a worksheet range in as a parameter to the
macro? If I can, is there a way I can access a cell offset from an
item in the range, i.e., do something like the OFFSET worksheet
function from within a VBA macro?

Thanks!

Ken

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
error accessing a protected worksheet Erik Jahre Excel Worksheet Functions 0 February 24th 06 08:32 AM
Accessing Cells in Separate Worksheet mrjeffy321 Excel Discussion (Misc queries) 3 January 19th 06 11:58 PM
Accessing a single worksheet in a workbook jluckhoff Excel Programming 0 February 3rd 04 09:22 PM
VBA Functions accessing value from worksheet sachinshah Excel Programming 1 September 15th 03 04:07 PM
accessing CheckBox on Worksheet Mike Tomasura Excel Programming 4 July 24th 03 01:19 PM


All times are GMT +1. The time now is 04:01 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"