Macros to autosort dates in column B

NANSAL0222

New Member
Joined
May 29, 2019
Messages
1
I am getting a code break error in my formula - I need to auto sort wit a command key by column B but have all my columns pull into the sort - through column M. This is my first attempt at writing a macro. Please let me know what I am doing incorrectly.

Public Sub Move_and_sort()
Dim IR As Long
Range("A3:M3").Copy.Range ("A" & IR & "B" & IR)
Range("A3: M3").Cell ClearContents
Range("A3:M" & IR).Sort Key1:=.Range("B3"), order1:=xlAscending, Header:=xlYes
Range("B3").Select
End With
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Welcome to the Board!

A few issues. You are missing the : from this line:
Code:
[COLOR=#333333]Range("A3:M3").Copy.Range ("A" & IR & "B" & IR)[/COLOR]
It should be:
Code:
[COLOR=#333333]Range("A3:M3").Copy.Range ("A" & IR & ":B" & IR)[/COLOR]

But more importantly, you haven't sent your IR variable equal to anything!
So, its values will be 0, and row 0 is not valid and will cause errors.
You need to set IR equal to something before using it.
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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