Consulting

Results 1 to 4 of 4

Thread: Solved: Need to extract to new workbook

  1. #1
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location

    Solved: Need to extract to new workbook

    How can I let this code to save sheets in to new workbook?

    [vba]
    Sub CopyData()
    Dim Arr(), a, tp As Worksheet, sh As Range
    Dim i As Long
    Application.ScreenUpdating = False

    ReDim Arr(0)
    i = -1
    With Sheets("SALARY LIST")
    For Each sh In Range(.Cells(3, 1), .Cells(3, 1).End(xlDown))
    i = i + 1
    ReDim Preserve Arr(i)
    Arr(i) = sh
    Next
    End With

    Set tp = Sheets("TEMP")
    For Each a In Arr
    tp.Range("C12") = a

    tp.Copy After:=Sheets(Sheets.Count)

    Sheets(Sheets.Count).Name = a

    Next

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  2. #2
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    File is attache... I just need employee pay slip to be extracted in a new workbook...
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  3. #3
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location


    [VBA]
    Sub CopyData()
    Dim Arr(), a, tp As Worksheet, sh As Range
    Dim i As Long
    Application.ScreenUpdating = False

    'Put names into an array
    ReDim Arr(0)
    i = -1
    With Sheets("SALARY LIST")
    For Each sh In Range(.Cells(3, 1), .Cells(3, 1).End(xlDown))
    i = i + 1
    ReDim Preserve Arr(i)
    Arr(i) = sh
    Next
    End With

    'Add name to template
    Set tp = Sheets("TEMP")
    For Each a In Arr
    tp.Range("C12") = a
    'Copy to a new sheet
    tp.Copy After:=Sheets(Sheets.Count)
    'Rename
    Sheets(Sheets.Count).Name = a
    'Fix values
    Sheets(a).Range("A1:A43").Value = Sheets(a).Range("A1:A43").Value 'Having Error here
    Next
    'Move name sheets to new workbook
    Sheets(Arr).Move
    Application.ScreenUpdating = True

    End Sub

    [/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  4. #4
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    I FOUND SOLUTION...

    [VBA]
    Sub CopyData()
    Dim w As Workbook, ws As Worksheet, ss As Worksheet
    Dim Arr(), a, tp As Worksheet, sh As Range
    Dim i As Long
    Application.ScreenUpdating = False

    ReDim Arr(0)
    i = -1
    With Sheets("SALARY LIST")
    For Each sh In Range(.Cells(3, 1), .Cells(3, 1).End(xlDown))
    i = i + 1
    ReDim Preserve Arr(i)
    Arr(i) = sh
    Next
    End With

    Set tp = Sheets("TEMP")
    For Each a In Arr
    tp.Range("C12") = a

    tp.Copy after:=Sheets(Sheets.Count)

    Sheets(Sheets.Count).Name = a

    Next

    Application.ScreenUpdating = True

    For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "SALARY LIST" And ws.Name <> "TEMP" Then
    If w Is Nothing Then
    ws.Move
    Set w = ActiveWorkbook
    Else
    ws.Move after:=ss
    End If
    Set ss = ActiveSheet
    End If
    Next ws
    ThisWorkbook.Activate

    End Sub
    [/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

Posting Permissions

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