+ Reply to Thread
Results 1 to 3 of 3

Macros in Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2009
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2003
    Posts
    1

    Macros in Excel

    Hi,
    I have to split an excel sheet into 3 sheets.
    The split should happen column wise.
    For example..if there are 10 columns in a sheet ,i have to split this sheet in three sheets.
    The first sheet should contain first 3 columns,the second sheet must contain the next 3 columns and the last sheet should contain the remaining 4 columns.
    I am supposed to do this using some code and not manually cut and paste the columns.
    Please help me and send the code for this.
    Thank you.

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Macros in Excel

    Hi,

    Turn on your macro recorder ... and perform he task ...
    The code will be written for you ...

    HTH

  3. #3
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Macros in Excel

    I think this will help you
    Sub CopyCol()
    
    Dim LastCol As Long
    Dim x As Long, WSCount
    Dim ColStr(1 To 2) As String
    
      LastCol = Worksheets("Sheet1").Range("A1").SpecialCells(xlCellTypeLastCell).Column '10
      ColNums = Int(LastCol / 3) '3
      WSCount = Worksheets.Count
     
      For x = 1 To ColNums '1,2,3
        If x = ColNums Then 'Last may have more or less columns than 3
          ColStr(2) = String(1, LastCol + 64)
        Else
          ColStr(2) = String(1, 3 * x + 64)
        End If
          ColStr(1) = String(1, 3 * x - 2 + 64)
          Sheets.Add After:=Worksheets(WSCount + x - 1)
          Worksheets(WSCount + x).Name = ColStr(1) & "-" & ColStr(2)
          Worksheets("Sheet1").Columns(ColStr(1) & ":" & ColStr(2)).Copy
          Sheets(WSCount + x).Paste Destination:=Worksheets(WSCount + x).Range("A1")
      Next x
      Application.CutCopyMode = False
    End Sub
    Note that the Source data is on "Sheet1"
    New Sheets will be added to the WB.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

+ 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