Avatar of KP_SoCal
KP_SoCalFlag for United States of America

asked on 

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
Microsoft ExcelVisual Basic ClassicVBA

Avatar of undefined
Last Comment
KP_SoCal
ASKER CERTIFIED SOLUTION
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of KP_SoCal
KP_SoCal
Flag of United States of America image

ASKER

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?
Avatar of KP_SoCal
KP_SoCal
Flag of United States of America image

ASKER

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.
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...
Avatar of KP_SoCal
KP_SoCal
Flag of United States of America image

ASKER

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
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo