VBA to loop through worksheet and paste data from source relative to sheet name

In the attached Sample file, I have a tab called ‘DataLists’ that I need to loop through the existing “State” tabs and paste in the “State” and “County” information starting in cell B4 that is copied from ‘DataLists’.  The data pasted should only be relative to the “State” listed in column G of ‘DataLists’.

Any help would be greatly appreciated.  Thank you!

P.S. I'm using Excel 2010
Sample.xlsx
KP_SoCalAsked:
Who is Participating?
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.

Saurabh Singh TeotiaCommented:
Use this code..What it does it deletes the old worksheet on your workbook and creates new worksheets from scratch...

Sub movedata()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim ws As Worksheet, ws1 As Worksheet
Dim ws2 As Worksheet
Dim lrow As Long, cell As Range, rng As Range, r As Range
Dim lr As Long
Set ws1 = Sheets("DataList")
lrow = ws1.Cells(Cells.Rows.Count, "G").End(xlUp).Row

For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> ws1.Name Then ws.Delete
Next ws

Set rng = ws1.Range("G2:G" & lrow)

For Each cell In rng
Set r = ws1.Range("G2:G" & cell.Row)

If Application.WorksheetFunction.CountIf(r, cell.Value) = 1 Then
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = cell.Value
Set ws2 = ActiveSheet
ws1.Range("G1:H1").Copy ws2.Range("b4")
Else
Set ws2 = Sheets(cell.Value)
End If


lr = ws2.Cells(Cells.Rows.Count, "B").End(xlUp).Row + 1

ws2.Range("B" & lr) = cell.Value
ws2.Range("c" & lr) = cell.Offset(0, 1).Value


Next cell




Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Open in new window


Enclosed is your workbook for your reference...
Sample.xlsm

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
KP_SoCalAuthor Commented:
Thank you for the quick response.  However, I'm not looking to recreate the existing tab.  Reason being, I have special formatting and page layouts applied to each tab of the actual document.  So I only want to copy and paste into the applicable 'State' tab.  Any ideas on how to accomplish?
KP_SoCalAuthor Commented:
Here's a link to a thread that is very close to what I'm looking for accomplish. http://stackoverflow.com/questions/29472855/excel-vba-to-search-worksheet-names-and-copy-a-range-into-active-sheet.

However the sub routine from the link referenced above is looking to copy from the tabs I'm intending to use as the actual target for my situation.
Saurabh Singh TeotiaCommented:
In that case use this code...

ub movedata()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim ws As Worksheet, ws1 As Worksheet
Dim ws2 As Worksheet
Dim lrow As Long, cell As Range, rng As Range, r As Range
Dim lr As Long
Set ws1 = Sheets("DataList")
lrow = ws1.Cells(Cells.Rows.Count, "G").End(xlUp).Row

Set rng = ws1.Range("G2:G" & lrow)

For Each cell In rng
Set r = ws1.Range("G2:G" & cell.Row)

If Application.WorksheetFunction.CountIf(r, cell.Value) = 1 Then
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = cell.Value
Set ws2 = ActiveSheet
ws1.Range("G1:H1").Copy ws2.Range("b4")
Else
Set ws2 = Sheets(cell.Value)
End If


lr = ws2.Cells(Cells.Rows.Count, "B").End(xlUp).Row + 1

ws2.Range("B" & lr) = cell.Value
ws2.Range("c" & lr) = cell.Offset(0, 1).Value


Next cell

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Open in new window


Saurabh...
KP_SoCalAuthor Commented:
I couldn't get the other solution to work. This one doesn't accomplish exactly what I'm looking for, but I can work with it.

Thank you,

KP
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
Microsoft Excel

From novice to tech pro — start learning today.