Consulting

Results 1 to 6 of 6

Thread: Solved: It copy one row not all needed rows

  1. #1
    VBAX Regular
    Joined
    May 2011
    Posts
    28
    Location

    Solved: It copy one row not all needed rows

    I want my code to copy all similar data (based on value of E) to new sheet which must be the same name of this value as example I have data in column E for classes so I want my code to check this column and all "Math" instances be copied to new sheet with name "Math" and paste data on it
    here is my code it works well but it just copy 2 rows not all instances rows , also I think there may be other way simpler than this

    Attached is sample file
    Please need help
    Attached Files Attached Files

  2. #2
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    you want it to make a new sheet for each unique value in column E?
    ------------------------------------------------
    Happy Coding my friends

  3. #3
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    [VBA]Sub test()
    Dim cell As Range
    Dim lr As Long, r As Long
    ActiveWorkbook.Sheets(1).Activate
    lr = Range("E" & Rows.Count).End(xlUp).Row
    For Each cell In Range("E2:E" & lr)
    If cell.Value <> cell.Offset(-1, 0).Value Then
    ActiveWorkbook.Sheets.Add.Name = cell.Value
    r = 1
    cell.EntireRow.Copy Destination:=Sheets(cell.Value).Range("A" & r)
    r = r + 1
    ElseIf cell.Value = cell.Offset(-1, 0).Value Then
    cell.EntireRow.Copy Destination:=Sheets(cell.Value).Range("A" & r)
    r = r + 1
    End If
    Next cell

    End Sub[/VBA]
    ------------------------------------------------
    Happy Coding my friends

  4. #4
    VBAX Regular
    Joined
    May 2011
    Posts
    28
    Location
    Quote Originally Posted by CatDaddy
    you want it to make a new sheet for each unique value in column E?
    yes that's exactly what i want and this sheet name is = to this unique value

  5. #5
    VBAX Regular
    Joined
    May 2011
    Posts
    28
    Location
    Quote Originally Posted by CatDaddy
    [VBA]Sub test()
    Dim cell As Range
    Dim lr As Long, r As Long
    ActiveWorkbook.Sheets(1).Activate
    lr = Range("E" & Rows.Count).End(xlUp).Row
    For Each cell In Range("E2:E" & lr)
    If cell.Value <> cell.Offset(-1, 0).Value Then
    ActiveWorkbook.Sheets.Add.Name = cell.Value
    r = 1
    cell.EntireRow.Copy Destination:=Sheets(cell.Value).Range("A" & r)
    r = r + 1
    ElseIf cell.Value = cell.Offset(-1, 0).Value Then
    cell.EntireRow.Copy Destination:=Sheets(cell.Value).Range("A" & r)
    r = r + 1
    End If
    Next cell

    End Sub[/VBA]


    Really many many thanks it works well as i need exactly
    and thanks for your fast reply

  6. #6
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    no problem
    ------------------------------------------------
    Happy Coding my friends

Posting Permissions

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