Can I make my VBA code automatically update the cell location?

h711

Board Regular
Joined
Jun 23, 2008
Messages
164
Hi, if I write VBA code to modify cell B3. Later on I inserted a new row above it, so now B3 became B4. The problem is my VBA code will still modify cell B3 every time I run it, not the new location B4.

Is there a way to make VBA code automatically update the cell location? Just like if you use = assignment in a cell, the referred cell will be automatically updated by excel every time you insert or delete rows/columns.


Thanks for your help.

Jianhua Huang
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
You could make the cell a named range (Insert > Name > Define) then refer to it as the range name e.g. Range("MyRange")
 

h711

Board Regular
Joined
Jun 23, 2008
Messages
164
Thanks for the replay. could you give me a simple example for how to do this?

For example, here is a simple macro I recorded to insert abc to cell B2:

Sub Macro1()

Range ("B2").Select
ActiveCell.FormulaR1C1 = "abc"
Range ("B3").Select

End Sub

Now how can I modify this code to auto update this "B2" to a new location if I insert/delete row/column that changes the cell location?

Thanks you very much for your help!!!!!
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
I named cell B2 as MyRange and ran this macro

Code:
Sub example()
Range("MyRange").Value = "abc"
End Sub

which placed abc in B2. I then deleted the contents of B2, inserted 3 rows above it then ran the macro again. abc was written into B5 (which now has the name MyRange).
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,867
Messages
5,834,087
Members
430,260
Latest member
MANICX100

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top