Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Position independent code

Greetings.

I wrote a few event handlers that do calculations when some cells are
modified.
For example:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.EnableEvents = False
Range("B6").Value = Range("C6").Value / Range("B13").Value
Application.EnableEvents = True
Else
Application.EnableEvents = False
Range("C6").Value = Range("B6").Value * Range("B13").Value
Application.EnableEvents = True
End If
End Sub

But then I insert rows to my sheets add/remove/reposition some stuff
and all my VBA functions fail since Cell Addresses are hard-coded.

How can I reference cells by name, or some sort of a dynamic reference
(like Excel formulas that get automagically updated when you move
things around)?

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Position independent code

give those cells a range name and use that instead of the address

--


Gary


wrote in message
ups.com...
Greetings.

I wrote a few event handlers that do calculations when some cells are
modified.
For example:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.EnableEvents = False
Range("B6").Value = Range("C6").Value / Range("B13").Value
Application.EnableEvents = True
Else
Application.EnableEvents = False
Range("C6").Value = Range("B6").Value * Range("B13").Value
Application.EnableEvents = True
End If
End Sub

But then I insert rows to my sheets add/remove/reposition some stuff
and all my VBA functions fail since Cell Addresses are hard-coded.

How can I reference cells by name, or some sort of a dynamic reference
(like Excel formulas that get automagically updated when you move
things around)?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Position independent code

Please give an example, what is a range name?

Thank you

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Position independent code

A range name is what it says on the box, a range that has been given a name
(handle) that can be used in formulae, and/or VBA.

To create a name,
select the range in question
goto InsertNameDefine...
add the name, say myRange, to the Names in Workbook (overtyping anything
pre-loaded)
the RefersTo box will alraedy be loaded with the range address
OK out

The code would then be, assuming that if C6 becomes C7, then B6 becomes B7,
B13 becomes B14, etc.

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("myRange")) Is Nothing Then
Application.EnableEvents = False
Target.Offset(0,-1).Value = Target.Value / Target.Offset(7,-1).Value
Application.EnableEvents = True
Else
Application.EnableEvents = False
Target.Value = Target.Offset(0,-1).Value * Target.Offset(7,-1).Value
Application.EnableEvents = True
End If
End Sub
--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

wrote in message
ups.com...
Please give an example, what is a range name?

Thank you



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Position independent code

in place of $C$6, create a range name. you can select the cell and either,
1 type a name into the address box next to the formula bar (it will show C6) and
press enter, or
2. click insert/name/define and type a name and press add
give it a unique name like Grand_Total



then in your code use Range("Grand_Total") instead of Range($C$6)

do this for the cells that will move
--


Gary


wrote in message
ups.com...
Greetings.

I wrote a few event handlers that do calculations when some cells are
modified.
For example:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.EnableEvents = False
Range("B6").Value = Range("C6").Value / Range("B13").Value
Application.EnableEvents = True
Else
Application.EnableEvents = False
Range("C6").Value = Range("B6").Value * Range("B13").Value
Application.EnableEvents = True
End If
End Sub

But then I insert rows to my sheets add/remove/reposition some stuff
and all my VBA functions fail since Cell Addresses are hard-coded.

How can I reference cells by name, or some sort of a dynamic reference
(like Excel formulas that get automagically updated when you move
things around)?

Thanks



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
color code series by an independent column of data rockdoc69 Charts and Charting in Excel 1 September 12th 08 10:54 AM
Position of CellCursor on Screen (absolute position) [email protected] Excel Programming 1 November 23rd 05 02:23 AM
Run a .xla as a independent application triki Excel Programming 3 August 12th 05 05:15 PM
Line position in code sebastienm Excel Programming 0 September 17th 04 05:45 PM
Line position in code Ajit Excel Programming 0 September 17th 04 03:49 PM


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