copy to new sheet based on column change

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
528
Office Version
  1. 365
Platform
  1. Windows
hi
i have the below code



Code:
[COLOR=#333333]Sub sheets_from_colb()[/COLOR][COLOR=#333333]<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">
Const cl& = 3
Dim a As Variant, x As Worksheet, sh As Worksheet
Dim rws&, cls&, p&, i&, rr&, b As Boolean
Application.ScreenUpdating = False
Sheets("main sheet ").Activate
rws = Cells.Find("*", , , , xlByRows, xlPrevious).Row
cls = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
Set x = Sheets.Add(After:=Sheets("main sheet "))
Sheets("main sheet ").Cells(1).Resize(rws, cls).Copy x.Cells(1)
Set a = x.Cells(1).Resize(rws, cls)
a.Sort a(1, cl), 2, Header:=xlYes
a = a.Resize(rws + 1)
    p = 2
    For i = p To rws + 1
        If a(i, cl) <> a(p, cl) Then
            b = False
            For Each sh In Worksheets
                If sh.Name = a(p, cl) Then b = True: Exit For
            Next
            If Not b Then 'Sheets.Add.Name = a(p, cl)
                Sheets.Add.Name = a(p, cl)
                With Sheets(a(p, cl))
                    x.Cells(1).Resize(, cls).Copy .Cells(1)
                    rr = .Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
                    x.Cells(p, 1).Resize(i - p, cls).Cut .Cells(rr, 1)
                End With
            End If
            p = i
        End If
    Next i
Application.DisplayAlerts = False
    x.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True


End Sub</code><code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">
</code>
[/COLOR]

Two questions

#1 how can i modify the code that i can select in an input box where the header ends suppose i want my header to be row 1,2,3
#2 instead of changing the sheet name in the code ,how can i have this code modified, to be the active sheet that i'm on right now
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

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