Reputation: 1115
I have the following headings and columns:
A B C D
H1 H2 H3 H4
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
a5 b5 c5 d5
a6 b6 c6 d6
a7 b7 c7 d7
First I want to replace the headings by copy/pasting from another sheet:
It will be like the following after replacing A1:A4:
A B C D
Table ID Order Price
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
a5 b5 c5 d5
a6 b6 c6 d6
a7 b7 c7 d7
Now once this replacing is done, I will need to order the columns alphabetically from left to right (A to Z).
The result is:
A B C D
ID Order Price Table
b2 c2 d2 a2
b3 c3 d3 a3
b4 c4 d4 a4
b5 c5 d5 a5
b6 c6 d6 a6
b7 c7 d7 a7
How to achieve this using a VBA code?
Thanks
Upvotes: 0
Views: 27
Reputation: 2794
You could do it with the following code, supposing the data is in Sheet1
and the new headings that are stored in the Sheet2
:
Sub replace_sort_headings()
Dim Sheet As Worksheet
Worksheets("Sheet2").Range("A1:D1").Copy
Worksheets("Sheet1").Range("A1:D1").PasteSpecial xlPasteValues
Worksheets("Sheet1").Range("A:D").Sort Key1:=Worksheets("Sheet1").Range("A1:D1"), Order1:=xlAscending, Orientation:=xlLeftToRight
End Sub
Upvotes: 1