Neaten up Macro Code

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following recorded macro below Sub Macro_Recorded()

I have tried to neaten it up -see Sub Move_Columns(), but need some help to get it to work

Your assistance in this regard is most appreciated






Code:
Sub Macro_Recorded()

Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("D:D").Select
Selection.Cut Destination:=Columns("A:A")
Columns("B:C").Select
Range("C1").Activate
Selection.Cut Destination:=Columns("C:D")
Columns("E:E").Select
Selection.Cut Destination:=Columns("B:B")
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("G:G").Select
Selection.Cut Destination:=Columns("C:C")
Columns("D:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("F:F").Select
Selection.Cut Destination:=Columns("D:D")
Columns("F:H").Select
Range("H1").Activate
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B5").Select
Selection.Copy
Range("C5").Select
Range("C6").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],2)"
Range("C6").Select
Selection.AutoFill Destination:=Range("C6:C285")
Range("C6:C85").Select
Columns("B:B").Select
Range("C6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("B6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Rows("85:85").Select
Selection.Copy
End Sub


Code:
 Sub Move_Columns()

With Sheets("imported Data")


.Range("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    .Range("D:D").Cut Destination:=Columns("A:A")

    .Range("B:C").Select

    .Range("C1").Cut Destination:=Columns("C:D")

    .Range("E:E").Cut Destination:=Columns("B:B")

    .Range("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    .Range("G:G").Cut Destination:=Columns("C:C")

    .Range("D:D").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    .Range("F:F").Cut Destination:=Columns("D:D")

    .Range("F:H").Select

    .Range("H1").Delete Shift:=xlToLeft

    .Range("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    .Range("B5").Copy

    .Range("C5:C6").CutCopyMode = False

    .Range("C5:C6").FormulaR1C1 = "=LEFT(RC[-1],2)"

    .Range("C6").AutoFill Destination:=Range("C6:C285")

    .Range("C6:C85").Select

    .Range("B:B").Select

    .Range("C6").Select

    .Range(Selection, Selection.End(xlDown)).Copy

    .Range("B6").PasteSpecial Paste:=xlPasteValues

    .Range("C:C").CutCopyMode = False

    .Selection.Delete Shift:=xlToLeft

    .Rows("85:85").Copy

End With


End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Rather than cutting/pasting columns, why not use the advanced filter to re-arrange the columns?
 
Upvote 0
I agree with you. I know exactly how to do this and much more quicker and simpler
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,473
Members
448,967
Latest member
visheshkotha

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