+ Reply to Thread
Results 1 to 12 of 12

Export Columns to New Worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-24-2008
    Location
    London
    Posts
    43

    Export Columns to New Worksheet

    I am using the following Macro to copy certain columns to a new Worksheet in a particular order which is working fine however I have two issues:

    1 - When the new worksheet is created the column widths are not being exported over so I have go to the new worksheet an manually adjust the width again.

    2 - I have a number of columns which will be move when additional columns are added i.e. from column AB to AC which means I have to change the code again.Is there a way to track these so I do not have to change the code again. In row 5 each of the columns has a unique heading.

    Sub CreateExportSheet()
        Dim sh As Worksheet
        Dim DestSh As Worksheet
        Dim Last As Long
        
        If SheetExists("Export Sheet") = True Then
            MsgBox "The sheet Export Sheet already exist"
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        Set DestSh = Worksheets.Add
        DestSh.Name = "Export Sheet"
        Sheets("Sheet1").Columns("D:D").Copy Destination:=Sheets("Export Sheet").Columns("A:A")
        Sheets("Sheet1").Columns("Q:Q").Copy Destination:=Sheets("Export Sheet").Columns("B:B")
        Sheets("Sheet1").Columns("S:S").Copy Destination:=Sheets("Export Sheet").Columns("C:C")
        Sheets("Sheet1").Columns("U:U").Copy Destination:=Sheets("Export Sheet").Columns("D:D")
         Sheets("Sheet1").Columns("AB:AB").Copy Destination:=Sheets("Export Sheet").Columns("P:P")
        Sheets("Sheet1").Columns("AH:AH").Copy Destination:=Sheets("Export Sheet").Columns("E:E")
        Sheets("Sheet1").Columns("N:N").Copy Destination:=Sheets("Export Sheet").Columns("F:F")
        Sheets("Sheet1").Columns("AI:AI").Copy Destination:=Sheets("Export Sheet").Columns("G:G")
        Application.ScreenUpdating = True
    End Sub
    Function SheetExists(SName As String, _
                         Optional ByVal WB As Workbook) As Boolean
        On Error Resume Next
        If WB Is Nothing Then Set WB = ThisWorkbook
        SheetExists = CBool(Len(Sheets(SName).Name))
    End Function
    Can anyone help?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Export Columns to New Worksheet

    If the columns in question have a specific value at the top, a header, then you can spot the location of the column(s) at any given moment by searching for that specific value in the row the headers are found in, typically row1.

    Sub CreateExportSheet()
    Dim ColCopy As Long
    Dim Dest    As Worksheet
        
        If Evaluate("ISREF('Export Sheet'!A1)") Then
            MsgBox "The sheet Export Sheet already exists"
            Exit Sub
        Else
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Export Sheet"
        End If
        
    Application.ScreenUpdating = False
    Set Dest = Sheets("Export Sheet")
    
    On Error Resume Next
    With Sheets("Sheet1")
        ColCopy = .Rows(1).Find("First Name", After:=.Cells(1, .Columns.Count), _
            LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Column
        
        If ColCopy > 0 Then
            .Columns(ColCopy).Copy Dest.Range("A1")
            ColCopy = 0
        End If
        
        ColCopy = .Rows(1).Find("Last Name", After:=.Cells(1, .Columns.Count), _
            LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Column
        
        If ColCopy > 0 Then
            .Columns(ColCopy).Copy Dest.Range("B1")
            ColCopy = 0
        End If
        
        ColCopy = .Rows(1).Find("Telephone", After:=.Cells(1, .Columns.Count), _
            LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Column
        
        If ColCopy > 0 Then
            .Columns(ColCopy).Copy Dest.Range("C1")
            ColCopy = 0
        End If
    
    '...etc
    End Sub

    It's lengthy, but robust...
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Export Columns to New Worksheet

    This is robust, too, you need only expand the two arrays. The first array is the value you're searching for, the second array is the target address for when the value is found.
    Sub CreateExportSheet()
    Dim Dest    As Worksheet
    Dim ColCopy As Long
    Dim Val     As Long
    Dim MyStrs  As Variant
    Dim MyTrgt  As Variant
        
        If Evaluate("ISREF('Export Sheet'!A1)") Then
            MsgBox "The sheet Export Sheet already exists"
            Exit Sub
        Else
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Export Sheet"
        End If
        
    Application.ScreenUpdating = False
    Set Dest = Sheets("Export Sheet")
    
    MyStrs = Array("First Name", "Last Name", "Address", "City")
    MyTrgt = Array("A1", "B1", "C1", "D1")
    
    On Error Resume Next
    With Sheets("Sheet1")
    
        For Val = LBound(MyStrs) To UBound(MyStrs)
            ColCopy = .Rows(1).Find(MyStrs(Val), After:=.Cells(1, .Columns.Count), _
                LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Column
        
            If ColCopy > 0 Then
                .Columns(ColCopy).Copy Dest.Range(MyTrgt(Val))
                ColCopy = 0
            End If
        Next Val
    
    End With
    
    Set Dest = Nothing
    End Sub

  4. #4
    Registered User
    Join Date
    07-24-2008
    Location
    London
    Posts
    43

    Re: Export Columns to New Worksheet

    Thanks. I actually prefer the second piece of code you provided with the array. The only issue is my headings are on Row 5 so that's where I want the Macro to do the search for the heading and then copy the entire column including Row 1,2,3,4 above the headings i.e.all the data in the column after it finds the heading in Row 5.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Export Columns to New Worksheet

    So you change the .Row(1) to .Row(5), yes?

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  6. #6
    Registered User
    Join Date
    07-24-2008
    Location
    London
    Posts
    43

    Re: Export Columns to New Worksheet

    I tried this before and now tried it again after getting confirmation from you with the same result.

    When the headings are on Row1 the export works fine. When the headings are moved to Row5 and the code is changed to Row(5) the export sheet is created but it is BLANK.

    Any suggestions?

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Export Columns to New Worksheet

    First suggestion is always post a sample file where we can simply see the same things at the same time.
    (Go Advanced > Paperclip).

    Maybe this:
            ColCopy = .Rows(5).Find(MyStrs(Val), After:=.Cells(5, .Columns.Count), _
                LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Column

  8. #8
    Registered User
    Join Date
    07-24-2008
    Location
    London
    Posts
    43

    Re: Export Columns to New Worksheet

    This now works fine which is brilliant. The last amendment - I have a large block of columns i.e. from AZ to FC which will never change which I will want in columns FA:JD in the export . I was wondering if I could add these somewhere as AZ:FC so I do not have to enter all the names again. If this cannot be done I will have to add these in manually (108 names). This was quite simple in the code I posted.

    If needed I could post a sample.

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Export Columns to New Worksheet

    Maybe add this:
    With Sheets("Sheet1")
    
        For Val = LBound(MyStrs) To UBound(MyStrs)
            ColCopy = .Rows(5).Find(MyStrs(Val), After:=.Cells(5, .Columns.Count), _
                LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Column
        
            If ColCopy > 0 Then
                .Columns(ColCopy).Copy Dest.Range(MyTrgt(Val))
                ColCopy = 0
            End If
        Next Val
    
        .Range("AZ:FC").Copy Dest.Range("FA1")
    End With

  10. #10
    Registered User
    Join Date
    07-24-2008
    Location
    London
    Posts
    43

    Re: Export Columns to New Worksheet

    Awesome that works perfect!!! Before I mark this as complete is there a way of making certain columns in the export hidden. I am sure there is something quite simple.

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Export Columns to New Worksheet

    Columns in which sheet?
    Which columns?

  12. #12
    Registered User
    Join Date
    07-24-2008
    Location
    London
    Posts
    43

    Re: Export Columns to New Worksheet

    There are a few columns i.e. 'address', 'city' in the original spreadsheet which I want exporting to a new sheet however I want them hidden in the new sheet.

    The plan is to export say Columns A:FC however from within these columns I want certain ones columns hidden when they appear in the export.

    Hope that makes sense.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1