Using relative references in macros

Moonbeam111

Board Regular
Joined
Sep 24, 2018
Messages
64
Office Version
  1. 365
  2. 2010
Hi all. I have a worksheet with alot of macros in it. I want to insert a column at Column B but, if I do, I know this will make my ranges incorrect in my macros by 1 column. Is there a quick and/or easy way to fix this? (i.e. without having to manually change all my ranges in my worksheet)?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Put the range in a named range, then use the named range in the macro.
 
Upvote 0
Hi Akuini. I'm still a little bit confused. Here's a sample of a code from one of my macros. it wont works as intended anymore since the ranges are referring to the wrong column.

VBA Code:
For Each pic In ActiveSheet.Pictures
        If Not Intersect(pic.TopLeftCell, Range("C21:C22")) Is Nothing Then
            pic.Delete
        End If
    Next pic

    Range("C153:D153").Copy
    Range("C21").Select
    ActiveSheet.Paste
    
    Range("E159:E160").Copy
    Range("E26").Select
    ActiveSheet.Paste
    
    Range("C155:D160").Copy
    Range("C22").Select
    ActiveSheet.Paste

If I named a range "myRange". How would I refer all these ranges to the right place?

VBA Code:
ActiveWorkbook.Names.Add "myRange", "?" 'what should I put in the question mark
 
Upvote 0
Try it on a clean sheet first:
Say range "C153:D153", then:

VBA Code:
ActiveWorkbook.Names.Add "myRange", Range("C153:D153")

after running that code you can see "myRange" in Name Manager window refer to "C153:D153"
Now, select col A then insert a column, you will see myRange now refers to "D153:E153". So it shifts 1 column.

Now in your actual sheet, run that code.
Then in your code, replace this part:

VBA Code:
Range("C153:D153").Copy
with:

VBA Code:
Range("MyRange").Copy

You need to create different named ranges for each range in your code.
 
Last edited:
Upvote 0
Solution
You're welcome, glad to help & thanks for the feedback.:)
Also, you can simplify your code for copy-paste. Like this part:

VBA Code:
    Range("C153:D153").Copy
    Range("C21").Select
    ActiveSheet.Paste

can be written like this:

VBA Code:
Range("C153:D153").Copy Range("C21")

and of course in your new code you need to change the range address to the named range.
 
Upvote 0
Ok i'll do that. I know that select is frowned upon being used in the VBA community.

Thanks for the bonus tip.

(y)
 
Upvote 0

Forum statistics

Threads
1,215,079
Messages
6,122,998
Members
449,092
Latest member
masterms

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