Dynamic Macro, Self updating macro

rafaelhok

New Member
Joined
Jul 1, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
The issue: I have a macro that works based on the current sheet layout. However, if I add new column or rows, the macro will run but put the data in the old column and the rows referenced keep the old row number that was initially used. Hopefully the attached excel provides a clear message of what is going on.

I am trying to find a way to make the macro dynamic, that way if rows/columns get added/deleted, it will continue to run and pull data into the correct columns/rows.

Here is the macro
Sub Fill_Test()

Range("AL31").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-37]="""","""",IFERROR(XLOOKUP(RC[-37],'Data'!R9C3:R9C78,'Data'!R18C3:R18C78),""""))"

Selection.AutoFill Destination:=Range("AL31:AL211")
Range("AL31:AL211").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("AL29").Select
End Sub


Any advice on this?

Also, how does one share an entire workbook?

Dynamic Macro.xlsm
ABCDEFGHIJKLMNO
1
2
3
4
5
6
7
8
9ABCDEFGHIJK
10
11
12
13
14
15
16
17
181234567891011
19
20
21
22
Data




Dynamic Macro.xlsm
AAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBI
22forumla used in macro
23=IF(A31="","",IFERROR(XLOOKUP(A31,Data!$C$9:$BZ$9,Data!$C$18:$BZ$18),""))
24
25
26
27This was a recorded macro.
28I first had the formula already copied to my clipboard.
29Lookup ValueData ValueI hit record, clicked on the cell AL31, pasted the formula in the formula bar, dragged the formula down, hit CTRL+C, right clicked and pasted as values, hit escape, clicked on another cell, and stopped recording.
30LettersNumbers
31A1
32B2The issue:
33C3when I add a new row or column (see next tab "Calcs (2)"), the macro does does not update to reflect the new changes.
34D4
35E5
36F6
37G7
38H8
39I9
40J10
41K11
42
43
44
Calcs





Dynamic Macro.xlsm
AAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBC
22forumla used in macro
23=IF(A31="","",IFERROR(XLOOKUP(A31,Data!$C$9:$BZ$9,Data!$C$18:$BZ$18),""))
24
25
26
27
28
29Lookup ValueNew columnData Value
30New row
31LettersNumbers
32A11
33B22The issue:
34C33now the macro runs but still puts the data in the AL column
35D44and starts at row 31
36E55
37F66Is there a way to write/manipulate the macro to update if a new row or column is insterted?
38G77
39H88
40I99
41J1010
42K1111
43
44
45
46
Calcs (2)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Dynamic Macro, Auto update macro reference if adding/deleting rows/columns. [SOLVED]
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,216,729
Messages
6,132,381
Members
449,723
Latest member
Ghufran

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