+ Reply to Thread
Results 1 to 3 of 3

Macro to match cell value to worksheet name and copy all rows and row data

Hybrid View

  1. #1
    Registered User
    Join Date
    10-25-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    7

    Question Macro to match cell value to worksheet name and copy all rows and row data

    Hi, this is my 1st posting, i'm a beginner with vba and have got as far as i can with the problem I've got

    I'm using Excel 2010

    I have a "Data" sheet which holds all information for orders, I have a macro that looks through the sheet and creates a new worksheet based on the Id's in column A.

    What i now need is a Macro to

    1 - Copy the column titles to all created sheets
    2 - Match the ID in column A to the sheet names and copy the matching rows from "Data" into the sheets with the same name.

    this is what i have so far (also attached), thanks in advance.

    Sub CreateTabs()
    
    Dim sheetCount As Long
    Dim sheetName As String
    Dim workbookCount As Integer
    
    With ActiveWorkbook
    sheetCount = Sheets("Data").Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To sheetCount Step 1
    sheetName = .Sheets("Data").Range("A" & i).Value
    If Not sheetName = "" Then
    workbookCount = .Worksheets.Count
    If Not DoesSheetExist(sheetName) Then
        .Sheets.Add After:=Sheets(workbookCount)
        .Sheets(.Worksheets.Count).name = sheetName
    End If
    End If
    
    '.Sheets(.Worksheets.Count).Range("A" & i).Value = .Sheets("Data").Range("A" & i).Value
    Next
    
    End With
    
    Worksheets("Data").Activate
    Application.Run "CopyRows"
    
    End Sub
    ' Determines if a sheet name already exists.
    Function DoesSheetExist(name As String) As Boolean
        blnFound = False
        With ThisWorkbook
            For i = 1 To .Sheets.Count
                    If .Sheets(i).name = name Then
                        blnFound = True
                        Exit For
                    End If
            Next i
        End With
        DoesSheetExist = blnFound
    End Function
    
    ' Copies rows from "Data" to matching worksheet name
    Sub CopyRows()
    Dim bottomD As Integer
    Dim c As Range
    Dim ws As Worksheet
        bottomD = Range("A" & Rows.Count).End(xlUp).Row
        For Each c In Sheets("Data").Range("A" & bottomD)
            For Each ws In Sheets
                ws.Activate
                If ws.name = c Then
                c.EntireRow.Copy Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                End If
            Next ws
        Next c
    End Sub
    inventory example1.xlsm
    Last edited by Fotis1991; 10-28-2013 at 09:02 AM. Reason: pls use code tags around your codes as per forum rules.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to match cell value to worksheet name and copy all rows and row data

    James,
    Welcome to the forum!

    Please read forum's rule regarding code tags.
    Try the attached.
    Attached Files Attached Files
    Last edited by AB33; 10-28-2013 at 08:46 AM.

  3. #3
    Registered User
    Join Date
    10-25-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro to match cell value to worksheet name and copy all rows and row data

    Many thanks, that works great.

    Apologies for the lack of code tags, will make sure they are in my next post.

    thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro copy data range from individual worksheets to new rows in consolidated worksheet
    By bushpotato in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2013, 01:24 AM
  2. [SOLVED] Macro to match cell value on two wooksheet copy data on another column on row back to 1st
    By JDC_8 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-07-2013, 08:39 PM
  3. [SOLVED] Macro to copy data rows to a summary worksheet if the row satisfies a condition
    By cjfreeseman in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-05-2012, 03:33 AM
  4. [SOLVED] Copy rows from my main worksheet to another worksheet based on data in one cell
    By gcol in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 08-08-2012, 05:12 AM
  5. Need macro to find data rows and copy to another worksheet.
    By n2lectual in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2009, 04:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1