How can I create code that will enable auto-update of the range location when the Excel Model is updated and the location of the target cells changes?

I_love_VBA

New Member
Joined
Aug 22, 2017
Messages
2
Dear programming enthusiasts and professionals,

I have created macro in Excel VBA to perform iterative operations. The intention of this code is to paste the values from one range of cells, which contain formulas, into another range.

In this code, I have specified the location of the ranges (both for the ones being copied and for the ones being pasted).

During the modification of the Excel Model, I change the number of rows or columns (by adding or deleting), thus changing the actual location of the range of cells. As an example, what used to be Range("N786:BT786") can become Range("N650:BT650").

I would certainly appreciate if you could help to modify my code so that the ranges would not be fixed to the static location, but rather automatically update as the model itself is being updated.

Below you may find my code.

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">Sub calculations_update()

Application
.DisplayStatusBar =False

Application
.ScreenUpdating =False

Dim StartTime AsDouble

Dim SecondsElapsed AsDouble

StartTime
= Timer

DoWhile i <95

i
= i +1

ThisWorkbook
.Worksheets("Electrity").Activate

Range
("N786:BT786").Value = Range("N787: BT787").Value

Range
("R826:BT826").Value = Range("R827: BT827").Value

ThisWorkbook
.Worksheets("Efficiency").Activate

Range
("H814").Value = Range("H815").Value

Range
("H826").Value = Range("H827").Value

Range
("H846").Value = Range("H847").Value

Loop

SecondsElapsed
= Round(Timer - StartTime,2)

MsgBox
"The update was successful in "&SecondsElapsed&" seconds", bInformation

EndSub</code>
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Assign a Name to each range, then in your macro refer to the ranges like this : Range("rangename​")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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
Back
Top