Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default Create copy of sheet using VB with columns in different order

I have a data sheet with column headings. I have a second sheet with
column A containing the preferred order of the columns and only the
needed columns. This can be changed to just be column headings if it's
easier.

I want to make a third sheet (or use the second one if we just use
column headings) with the columns of data taken from the first sheet
in the right order, skipping any columns not on the list and somehow
letting me know if a required column didnn't exist on the first data
sheet. Make sense? Please help me get started on this. I'm still
somewhat new to VB as I do about one little task with it every year or
two.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Create copy of sheet using VB with columns in different order

I'm gonna guess that you have headings in A1:Axx (on that column heading
sheet). And A1 is the header for column A, A2 for B, A3 for C, ...

Option Explicit
Sub testme()

Dim HeaderWks As Worksheet
Dim DataWks As Worksheet
Dim NewWks As Worksheet
Dim myRng As Range
Dim myCell As Range
Dim FoundCell As Range
Dim oCol As Long

Set HeaderWks = Worksheets("sheet1")
Set DataWks = Worksheets("sheet2")
Set NewWks = Worksheets.Add

With HeaderWks
Set myRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
End With

oCol = 0
For Each myCell In myRng.Cells
With DataWks.Rows(1)
Set FoundCell = .Cells.Find(What:=myCell.Value, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlNext, _
searchdirection:=xlByColumns, _
MatchCase:=False)
End With
If FoundCell Is Nothing Then
MsgBox "Design error with: " & myCell.Value
Else
oCol = oCol + 1
FoundCell.EntireColumn.Copy _
Destination:=NewWks.Cells(1, oCol)
End If
Next myCell

End Sub

(If you have the headers in column A and the order in column B, then sort by
column B to put it in nice order for the macro first.)

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm



Bryan wrote:

I have a data sheet with column headings. I have a second sheet with
column A containing the preferred order of the columns and only the
needed columns. This can be changed to just be column headings if it's
easier.

I want to make a third sheet (or use the second one if we just use
column headings) with the columns of data taken from the first sheet
in the right order, skipping any columns not on the list and somehow
letting me know if a required column didnn't exist on the first data
sheet. Make sense? Please help me get started on this. I'm still
somewhat new to VB as I do about one little task with it every year or
two.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Sheet & Copy Stuff littleredhairedgirl Excel Worksheet Functions 0 October 24th 09 01:22 PM
macro to create, name sheet and copy. sutha Excel Discussion (Misc queries) 4 December 23rd 08 01:53 PM
copy columns with order reversed Karra Excel Discussion (Misc queries) 1 December 11th 07 06:18 PM
How do I create a fundraiser order sheet? kathy Excel Discussion (Misc queries) 0 August 23rd 05 08:44 PM
How to create a Macro to Copy Information in one sheet to another sheet. poppy Excel Programming 3 July 28th 04 07:26 AM


All times are GMT +1. The time now is 07:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"