View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Phrank Phrank is offline
external usenet poster
 
Posts: 153
Default Rearranging columns in a large spreadsheet

I'll give that a try tonight and get back to you. Thank you!!

Frank

On Mon, 8 Jun 2015 09:55:59 +0200, Claus Busch
wrote:

Hi Frank,

Am Mon, 08 Jun 2015 00:59:09 -0400 schrieb Phrank:

I've got a large spreadsheet (goes to column CF), which is created
from another set of macros that pull in data from multiple sources.
After the spreadsheet is created, I would like to reorder the columns
in a more consumable order. How would I go about looping through the
columns to get the header names, and then putting the columns that I
want more to the front of the spreadsheet where the user will
immediately see the data when the workbook is opened?


try:

Sub MoveCols()
Dim strCols As String, strHeaders As String
Dim varCols As Variant, varHeaders As Variant
Dim c As Range
Dim i As Long

Application.ScreenUpdating = False
'Modify the headers and the target columns
'In this example the column with "Header10" goes to column B
'column with "Header12" goes to column D and so on
strHeaders = "Header10,Header12,Header20,Header28,Header35,Head er40"
strCols = "B,D,E,G,H,I"
varHeaders = Split(strHeaders, ",")
varCols = Split(strCols, ",")

For i = LBound(varHeaders) To UBound(varHeaders)
Set c = Range("1:1").Find(varHeaders(i), , xlValues, xlWhole)
If Not c Is Nothing Then
Columns(c.Column).Cut
Columns(varCols(i)).Insert Shift:=xlToRight
End If
Next
Application.ScreenUpdating = True
End Sub


Regards
Claus B.