+ Reply to Thread
Results 1 to 2 of 2

Macro to Split Separate Worksheets with User Input for Range Select (so close to working)

Hybrid View

  1. #1
    Registered User
    Join Date
    12-13-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    40

    Question Macro to Split Separate Worksheets with User Input for Range Select (so close to working)

    This macro is close to working but still running into error after the first run. I am trying to let the user select the column that contains the values that are to be split out into their own worksheet. First I am trying to identify the unique values in that column and copying them to a temporary location. Then each unique value from that column will take the entire rows for them and put them in a separate worksheet. This works great up until I come to a part where I am trying to clear the copied data and it errors out after the first run in the If WksExists(c.Value) Then Sheets(c.Value).Cells.Clear part. My intent is to also replace the piece of code where the data is copied to based on where the last empty column and first cell in that column is available as this will vary for each workbook this macro will be used for.

    Here is what I have for my code so far that I am testing with:

    Sub MultiSheets()
    Dim ws1 As Worksheet
    Dim wsNew As Worksheet
    Dim rng As Range
    Dim r As String
    Dim c As Range
    Dim rs As Range
    Set ws1 = Sheets(ActiveSheet.Name)
    Dim N As Integer
    Dim M As Integer
    Dim FirstWSToSort As Integer
    Dim LastWSToSort As Integer
    Dim SortDescending As Boolean
    Dim lastCell As String
    
    Set rs = Application.InputBox(Prompt:="Select the Column for Lookup", Title:="Range Select", Type:=8)
     rs.Name = "Lookup"
    
    'this will find the last column available
    ActiveCell.End(xlToRight).Select
    'move one cell to the right from the last used cell
    ActiveCell.Offset(0, 1).Select
    
    
    ActiveSheet.Columns("A").SpecialCells(xlLastCell).Select
    lastCell = Split(ActiveCell.Address, "$")(1) & Split(ActiveCell.Address, "$")(2)
    Set rng = Range("A1:" & lastCell)
    'rng.Name = "SplitSheets"
    
       Range("A2").Select
    'extract a list of Modality Codes
    ws1.Columns("A:A").Copy _
      Destination:=Range("M1")
    ws1.Columns("M:M").AdvancedFilter _
      Action:=xlFilterCopy, _
      CopyToRange:=Range("L1"), Unique:=True
    r = Cells(Rows.Count, "L").End(xlUp).row
    'r = lastCell = Split(ActiveCell.Address, "$")(1) & Split(ActiveCell.Address, "$")(2)
    
    'set up Criteria Area
    Range("L1").Value = Range("A1").Value
    
    For Each c In Range("L2:L" & r)
      'add to the criteria area
      ws1.Range("M2").Value = c.Value
      'add new sheet (if required)
      'and run advanced filter
      If WksExists(c.Value) Then
        Sheets(c.Value).Cells.Clear
      rng.AdvancedFilter Action:=xlFilterCopy, _
            CriteriaRange:=ws1.Range("M1:M2"), _
            CopyToRange:=Sheets(c.Value).Range("A1"), _
            Unique:=False
            Columns("A:K").Select
        Columns("A:K").EntireColumn.AutoFit
      'Columns.AutoFit
            
      Else
        Set wsNew = Sheets.Add
        wsNew.Move After:=Worksheets(Worksheets.Count)
        wsNew.Name = c.Value
        rng.AdvancedFilter Action:=xlFilterCopy, _
            CriteriaRange:=ws1.Range("M1:M2"), _
            CopyToRange:=wsNew.Range("A1"), _
            Unique:=False
            Columns("A:K").Select
        Columns("A:K").EntireColumn.AutoFit
               'Columns.AutoFit
      End If
    Next
    ws1.Select
    ws1.Columns("L:M").Delete
    SortDescending = False
    
    If ActiveWindow.SelectedSheets.Count = 1 Then
        FirstWSToSort = 1
        LastWSToSort = Worksheets.Count
    Else
        With ActiveWindow.SelectedSheets
            For N = 2 To .Count
                If .Item(N - 1).Index <> .Item(N).Index - 1 Then
                    MsgBox "You cannot sort non-adjacent sheets"
                    Exit Sub
                End If
            Next N
            FirstWSToSort = .Item(1).Index
            LastWSToSort = .Item(.Count).Index
         End With
    End If
    
    For M = FirstWSToSort To LastWSToSort
        For N = M To LastWSToSort
            If SortDescending = True Then
                If UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) Then
                    Worksheets(N).Move Before:=Worksheets(M)
                End If
            Else
                If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
                   Worksheets(N).Move Before:=Worksheets(M)
                End If
            End If
         Next N
    Next M
     Range("A2").Select
     
    
    End Sub
    Function WksExists(wksName As String) As Boolean
    
       On Error Resume Next
       WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
    
    End Function

  2. #2
    Registered User
    Join Date
    12-13-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Macro to Split Separate Worksheets with User Input for Range Select (so close to worki

    This seems to be working well for me so far.

    Sub AnotherTestSplit()
    Dim lastrow As Long
    Dim LastCol As Integer
    Dim i As Long
    Dim iStart As Long
    Dim iEnd As Long
    Dim ws As Worksheet
    Dim Master As String
    Dim FirstWSToSort As Integer
    Dim LastWSToSort As Integer
    Dim s As Integer
    Dim j As Integer
    Dim ColLookup As String
    
    Set rs = Application.InputBox(Prompt:="Select the Column for Lookup", Title:="Range Select", Type:=8)
    ColLookup = rs.Address
    
     ColLookup = Left(rs.Address(False, False), 1 + -1 * (rs.Column > 26))
    
    lastCell = Split(ActiveCell.Address, "$")(1) & Split(ActiveCell.Address, "$")(2)
    
    Application.ScreenUpdating = False
    With ActiveSheet
    Master = .Name
        Application.DisplayAlerts = False
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name <> .Name Then ws.Delete
        Next ws
        Application.DisplayAlerts = True
        lastrow = .Cells(Rows.Count, ColLookup).End(xlUp).Row
        LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        .Range(.Cells(2, 1), Cells(lastrow, LastCol)).Sort Key1:=Range(ColLookup & 2), Order1:=xlAscending, _
            Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        iStart = 2
        For i = 2 To lastrow
            If .Range(ColLookup & i).Value <> .Range(ColLookup & i + 1).Value Then
                iEnd = i
                Sheets.Add after:=Sheets(Sheets.Count)
                Set ws = ActiveSheet
                On Error Resume Next
                ws.Name = .Range(ColLookup & iStart).Value
                On Error GoTo 0
                ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
                With ws.Rows(1)
                    .HorizontalAlignment = xlCenter
                    With .Font
                      '  .ColorIndex = 5
                        .Bold = True
                    End With
                End With
                .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
                iStart = iEnd + 1
            End If
            
            For s = 1 To Sheets.Count - 1
           For j = s + 1 To Sheets.Count
              If StrComp(Sheets(s).Name, Sheets(j).Name) > 0 Then Sheets(j).Move Sheets(s)
           Next
        Next
          
            Next i
        
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    Sheets(Master).Activate
    End Sub
    
    Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
    End Function

+ 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