Consulting

Results 1 to 5 of 5

Thread: Sleeper: Copy columns to new sheets

  1. #1

    Sleeper: Copy columns to new sheets

    My desired format is like this: col A+col B -> sheet 1
    col A+col C -> sheet 2
    col A+col D -> sheet 3, and so on...

    Col A is the customer list, so I need to copy it to each new sheet.
    There is around 160 columns in my spreadsheet, I'm a bit worried
    and what's the fastest way to loop until last used column and create
    160 new sheets?

    The sheet names will be the value of first row per each column.

    Any ideas?

    Thanks.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Dreamer
    My desired format is like this: col A+col B -> sheet 1
    col A+col C -> sheet 2
    col A+col D -> sheet 3, and so on...
    Dim oThis As Worksheet
    Dim oWs As Worksheet
    Dim iLastCol As Long
    Dim i As Long
    Set oThis = ActiveSheet
        iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        For i = 2 To iLastCol
            Set oWs = Worksheets.Add(after:=Worksheets(Worksheets.Count))
            oThis.Columns("A").Copy oWs.Columns("A")
            oThis.Columns(i).Copy oWs.Columns("B")
        Next i
        oThis.Activate

  3. #3
    Thx! Could you please tell me how to name those sheets as well? ^^

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Dim oThis As Worksheet
    Dim oWs As Worksheet
    Dim iLastCol As Long
    Dim i As Long
        
        Set oThis = ActiveSheet
        iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        For i = 2 To iLastCol
            Set oWs = Worksheets.Add(after:=Worksheets(Worksheets.Count))
            oWs.Name = "mySheet" & i
            oThis.Columns("A").Copy oWs.Columns("A")
            oThis.Columns(i).Copy oWs.Columns("B")
        Next i
        oThis.Activate

  5. #5
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Lightbulb

    Quote Originally Posted by xld
    Dim oThis As Worksheet
    Dim oWs As Worksheet
    Dim iLastCol As Long
    Dim i As Long
    Set oThis = ActiveSheet
    iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 2 To iLastCol
    Set oWs = Worksheets.Add(after:=Worksheets(Worksheets.Count))
    oWs.Name = "mySheet" & i
    oThis.Columns("A").Copy oWs.Columns("A")
    oThis.Columns(i).Copy oWs.Columns("B")
    Next i
    oThis.Activate

    You can also enter manually your desired name for each sheet. Thanks , xld for the code. I'm just edit one line on the input box. Try it. I works for me.


    Dim oThis As Worksheet
    Dim oWs As Worksheet
    Dim iLastCol As Long
    Dim i As Long
    Set oThis = ActiveSheet
    iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 2 To iLastCol
    Set oWs = Worksheets.Add(after:=Worksheets(Worksheets.Count))
    oWs.Name = InputBox("Enter sheet name:", "Name for Sheet") 'Enter your desired name for sheets
    'oWs.Name = "mySheet" & i
    oThis.Columns("A").Copy oWs.Columns("A")
    oThis.Columns(i).Copy oWs.Columns("B")
    Next i
    oThis.Activate
    Thanks.

Posting Permissions

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