copy data to new sheets each time name changes

Can an expert provide VBA code for this please.

For each change of name in column B I need to copy both the header and all the data from rows A to O to a new sheet and name the sheet with the name that is in Cell B

have attached a file to help explain.

Many thanks
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JagwarmanAuthor Commented:
Hmmm file did not attach

pls try

Sub macro1()

Set OriWs = Sheets("Consolidated")
OriWs.UsedRange.Sort key1:=ActiveSheet.Range("B1"), Header:=xlYes

For Each c In Range(Range("B2"), Range("B" & Cells.Rows.Count).End(xlUp))
    If c.Value <> lastvalue Then
        lastvalue = c.Value
        Set newWs = ActiveWorkbook.Sheets.Add(after:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))
        newWs.Name = c.Value
        OriWs.UsedRange.AutoFilter Field:=2, Criteria1:=c.Value
        OriWs.Cells.Copy newWs.Range("a1")
    End If
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JagwarmanAuthor Commented:
Rgonzo, you are amazing just what I need many many thanks
Excellent Rgonzo1971

i am a excel learner, so i have two question on this code.

1- i added some new data with different in column B and Column A and then ran the macro then i get an error in line 11 and it says "Run time error 1004
Cannot Rename a sheet to the same name as another sheet a referenced object library or a workbook referenced by Visual Basic."

2- i got confused with "lastvalue"  you have not set any value or i do not see any reference before for "lastvalue" can you please explain why have you used If c.Value <> lastvalue Then
        lastvalue = c.Value    becuase "lastvalue"  obiously has no value and c.value is definitely is not equal to c.value , so wouldn't this work instead  lastvalue=c.Value

grateful if you could shed light on this.

i opened a question for this here

please answer it there, so that you could also get the points for it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.