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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,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