Splitting a Workbook by Values in Two Columns

Mane

New Member
Joined
Jun 26, 2012
Messages
20
Hi, I'm trying to split a workbook by the values in two different columns. I have a workbook that has letters in column J (A-R) and numbers in column K (1-10).

I can succesfully use the code here: http://www.mrexcel.com/forum/showthread.php?352057-Split-Worksheet-to-Multiple-Worksheets to split the workbook into seperate sheets named A-R. I could run the macro again and change the column to split on, but this has a few problems; It will only run on one worksheet, so I'd have to run it multiple times and, even if I modify the line:

Code:
ws.Name = .Range(splitCol & iStart).Value
to read:
Code:
ws.Name = ws.Name & .Range(splitCol & iStart).Value

it still doesn't name the new worksheets correctly.

So basically i want to try and do it in one hit. I know I'll need a two dimensional (recursive?) loop, with a 'j' counter as well as an 'i' counter but that's about as far as I've got. My VBA is pretty bad and I don't fully understand what happens in the loop.

It would also be helpful if it could modify the current cell to force upper case for the letter column because at the moment I have to select the column and run this first:

Code:
'Convert Selection to Upper Case
Sub UpperCase()
Dim Cell As RangeFor Each Cell In Selection.Cells[INDENT]If Cell.HasFormula = False Then[/INDENT]
[INDENT=2]Cell = UCase(Cell)[/INDENT]
[INDENT]End If[/INDENT]
Next
End Sub

Which also modfies my header row.

Here's the code I'm currently using which I've modified to include a splitCol variable storing the column I wish to split by:

Code:
'Split by Single Col
Sub SplitByCol()
Dim lastrow As Long, LastCol As Integer, i As Long, iStart As Long, iEnd As Long
Dim ws As Worksheet
Dim splitCol As String
Application.ScreenUpdating = False
With ActiveSheet
    splitCol = "K"
    lastrow = .Cells(Rows.Count, splitCol).End(xlUp).Row
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    .Range(.Cells(2, 1), Cells(lastrow, LastCol)).Sort Key1:=Range(splitCol & "2"), Order1:=xlAscending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=True, Orientation:=xlTopToBottom
    iStart = 2
    For i = 2 To lastrow
        If .Range(splitCol & i).Value <> .Range(splitCol & i + 1).Value Then
            iEnd = i
            Sheets.Add after:=Sheets(Sheets.Count)
            Set ws = ActiveSheet
            On Error Resume Next
            ws.Name = .Range(splitCol & 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
    Next i
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

I've also had a look at this thread: http://www.mrexcel.com/forum/showthread.php?t=396069 which I might use to save them to different workbooks when I've solved this problem.

Any help you can give greatly appreciated! Ta.
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,214,416
Messages
6,119,384
Members
448,889
Latest member
TS_711

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top