Relative Referencing 101 Problems

Andy0311

Board Regular
Joined
Oct 16, 2019
Messages
118
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,
I am trying to learn relative referencing for excel macros, and I am having embarassing problems. I tried setting up a very simple sheet like so:

[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD]1
[/TD]
[TD][/TD]
[TD]1[/TD]
[TD][/TD]
[TD]5[/TD]
[TD][/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]1[/TD]
[TD][/TD]
[TD]5[/TD]
[TD][/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD][/TD]
[TD]3[/TD]
[TD][/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

The first column starts in cell A1, and so forth. I click on relative reference, then record macro, and then do the following:

With the active cell in A1, do a Contrl Shift down arrow to select the range, click on the Data tab, click on Remove Duplicates, and end up with the correct result, which in this case was 3 remaining values. And I click Stop Recording. I set the same column up again and reran the macro. Correct again. When I try to run the macro for any other range, it doesn't work. It merely highlights the range. Is the fact I started the macro in cell A1 part of the problem, or should I have clicked on A1 to start recording? Thanks to anyone who could offer me some help.

Andy
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
It would help if could post the code that you've got.
 
Upvote 0
It would help if could post the code that you've got.

Sub Testrr()
'
' Testrr Macro
'


'
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Range("$A$1:$A$5").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
 
Upvote 0
The problem is that the second line of the code has the rangeA1:A5 hard coded.
Try
Code:
Sub Macro2()
    Range(selection, selection.End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
 
Upvote 0
The problem is that the second line of the code has the rangeA1:A5 hard coded.
Try
Code:
Sub Macro2()
    Range(selection, selection.End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

Thank you so much, Fluff. It worked perfectly. The code I sent you was produced from a macro. What did I do to hard code the range when I recorded? Again, thanks so much for your help. A
 
Upvote 0
You're welcome & thanks for the feedback


What did I do to hard code the range when I recorded?
Nothing, it's just the way the recorder works.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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