+ Reply to Thread
Results 1 to 3 of 3

Trying to split worksheet based on contents of a column

Hybrid View

  1. #1
    Registered User
    Join Date
    08-22-2013
    Location
    consists ilygovi
    MS-Off Ver
    Office 2010
    Posts
    2

    Trying to split worksheet based on contents of a column

    I've found a code to do the above which I've included. I'm pretty sure this worked yesterday but today I am getting an 'Else without IF' error.

    The contents of my worksheet are something like

    Column1:
    Colour
    Column 2:
    Name
    Column 3:
    Password

    what I'm looking to do is to have a new worksheet created for every unique value in column 1 and any value that has that value to populate the new worksheet - I can't see where I am going wrong! Any suggestions gratefully received.

    Sub parse_data()
    Dim lr As Long
    Dim ws As Worksheet
    Dim vcol, i As Integer
    Dim icol As Long
    Dim myarr As Variant
    Dim title As String
    Dim titlerow As Integer
    'vcol is the column number that you want to split the data based on
    vcol = 1
    'change the name in the speech marks to the name of the worksheet
    Set ws = Sheets("output")
    lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
    'A1:C1 is the range of the title
    title = "A1:C1"
    titlerow = ws.Range(title).Cells(1).Row
    icol = ws.Columns.Count
    ws.Cells(1, icol) = "Unique"
    For i = 2 To lr
    On Error Resume Next
    If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
    ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
    End If
    Next
    myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
    ws.Columns(icol).Clear
    For i = 2 To UBound(myarr)
    ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
    If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
    Else
    Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
    End If
    ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
    Sheets(myarr(i) & "").Columns.AutoFit
    Next
    ws.AutoFilterMode = False
    ws.Activate
    End Sub

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Trying to split worksheet based on contents of a column

    Have a look here:
    http://www.excelforum.com/tips-and-t...-criteria.html
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    08-22-2013
    Location
    consists ilygovi
    MS-Off Ver
    Office 2010
    Posts
    2

    Re: Trying to split worksheet based on contents of a column

    That is fabulous! Thanks very much - works perfectly.

    Just a what if... rather than evaluate a column to evaluate the results of a formula instead? For instance - if a column has a long list of usernames, could I split it based on the first two letters of that column? I'm new at VBA so just trying to figure out what is do-able or not.

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to split worksheet into multiple worksheets based on column content
    By johnboytweed in forum Excel General
    Replies: 12
    Last Post: 10-16-2013, 12:01 PM
  2. [SOLVED] Split one worksheet into multiple worksheet by column
    By vdivgi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2012, 11:05 AM
  3. [SOLVED] Split One Column at Delimiter While Copying Contents of Second Column to Populate New Rows
    By medailSacrosanct in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 08-01-2012, 11:37 AM
  4. Split cell contents over multiple rows based on cell contents
    By naigy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-17-2011, 05:38 PM
  5. Macro to split workbook into several based on cell contents
    By rich@richdavies.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2006, 06:20 AM

Tags for this Thread

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