Macro for RearrangeColumnsWithRows and sort them

CristianFirpo

New Member
Joined
Mar 5, 2024
Messages
1
Office Version
  1. 365
Hello Folks
I have this macro that runs wel. Here is my macro

Sub RearrangeColumnsWithRows()
Dim srcRange As Range
Dim destRange As Range
Dim columnsOrder As Variant
Dim i As Long

' Set the source range where the columns and rows are currently located
Set srcRange = Range("A1:BL20411") ' Replace "A1:BL60500" with the range of your row report, including the rows

' Define the desired order of columns
' For example, if you want the columns to be arranged as "C, A, D, B, E, F"
columnsOrder = Array(1,10,8,31,29,18,13,14,15,16,23,24,35,6,5,36,37,38,47,43,45,32,34,61,63,26,2,62,3,4,7,9,11,12,17,19,20,21,22,25,27,28,30,33,39,40,40,41,42,44,46,48,49,50,51,52,53,55,56,57,58,59,60,64) ' Modify the column order as required

' Set the destination range where the columns and rows will be placed
' Make sure to adjust the destination range size
Set destRange = Range("A20412").Resize(srcRange.Rows.Count, UBound(columnsOrder) + 1)

' Rearrange the columns and rows
For i = LBound(columnsOrder) To UBound(columnsOrder)
srcRange.Columns(columnsOrder(i)).Copy Destination:=destRange.Columns(i + 1)
Next i

' Clear the original range
srcRange.ClearContents

End Sub



I would like to add sort column to that macro as Column B ( country Name), Column C (STUDY_SITE_NUMBER), Column D (SUBJECT_ID), and Column F (COST_DATE) and finally deleted the empty cell.
DRUGTRIAL_NAMECOUNTRY_NAMESTUDY_SITE_NUMBERSUBJECT_IDACTIVITY_NAMECOST_DATE
270603​
United States
7801​
E7801002Concomitant medications
9/27/2022​
270603​
United States
7801​
E7801005Concomitant medications
10/4/2022​
So execute both macros rearrangeColumnsWithRows and sort column and delete empty cell at once

Thank you very much for your help
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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