VBA that edits itself when run

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52
I have a general question.

Is it possible to write a macro with a bit at the end that edits and saves the macro.

A simple example might be a piece of code that selects a cell (A1), inputs a value, then edits the code so that the next time it is run it selects another Cell (A2) etc.

Many thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

FOr your example you won't need to edit the code you will need a FOR NEXT loop.

Can you be more specific?
 

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52
OK, sorry if this gets a bit complicated!

I have a macro which basicaly populates a couple of cells by referencing them to cells in another sheet (see below)

Code:
    Range("L12").Select
    ActiveCell.FormulaR1C1 = "='CS Belfast'!R[-4]C"
    Range("M12").Select
    ActiveCell.FormulaR1C1 = "='CS Belfast'!R[-4]C"

When the macro is next run (i.e. a week later) I need it to reference the next cell down (i.e. 'CS Belfast'!R[-5]C')

I have tried to simplify the example. Essentially what I want is a piece of code to go at the end that edits the code and saves the macro for use at a later date. Each week i need the same macro to reference a cell 1 down from the previous week.

If you could help that would be great.
 

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

Why not just use a cell value?

i.e. in O1 enter 4 and use

Code:
Range("L12").FormulaR1C1 = "=R[-" & Range("O1").Value & "]C"
Range("M12").FormulaR1C1 = "=R[-" & Range("O1").Value & "]C"
Range("O1").Value = Range("O1").Value + 1
 

Forum statistics

Threads
1,141,626
Messages
5,707,491
Members
421,511
Latest member
mgroah1

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