Consulting

Results 1 to 2 of 2

Thread: Problem with the VBA code to copy cell and paste in new workbook

  1. #1

    Problem with the VBA code to copy cell and paste in new workbook

    Hello,
    As I have code that create new workbook for every single unique value in the specific area.
    Code looks as below:
    [VBA]Sub Copy_To_WorkbooksB()
    Dim CalcMode As Long
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim WSNew As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim Lrow As Long
    Dim foldername As String
    Dim MyPath As String
    Dim FieldNum As Integer
    Dim FileExtStr As String
    Dim FileFormatNum As Long


    'Name of the sheet with your data
    Set ws1 = Sheets("Arkusz1")

    'Determine the Excel version and file extension/format
    If Val(Application.Version) < 12 Then
    'You use Excel 97-2003
    FileExtStr = ".xls": FileFormatNum = -4143
    Else
    'You use Excel 2007
    If ws1.Parent.FileFormat = 56 Then
    FileExtStr = ".xls": FileFormatNum = 56
    Else
    FileExtStr = ".xlsx": FileFormatNum = 51
    End If
    End If

    'Set filter range : A1 is the top left cell of your filter range and
    'the header of the first column, D is the last column in the filter range
    Set rng = ws1.Range("A2" & Rows.Count)

    'Set Field number of the filter column
    'This example filters on the first field in the range(change the field if needed)
    'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
    FieldNum = 1

    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    End With

    ' Add worksheet to copy/Paste the unique list
    Set ws2 = Worksheets.Add

    'Fill in the path\folder where you want the new folder with the files
    'you can use also this "C:\Users\Ron\test"
    MyPath = "C:\WMSDK"

    'Add a slash at the end if the user forget it
    If Right(MyPath, 1) <> "\" Then
    MyPath = MyPath & "\"
    End If

    'Create folder for the new files
    foldername = MyPath & Format(Now, "yyyy-mm-dd hh-mm-ss") & "\"
    MkDir foldername

    With ws2
    'first we copy the Unique data from the filter field to ws2
    rng.Columns(FieldNum).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=.Range("A3"), Unique:=True

    'loop through the unique list in ws2 and filter/copy to a new workbook
    Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
    For Each cell In .Range("A4:A" & Lrow)

    'Add new workbook with one sheet
    Set WSNew = Workbooks.Add(xlWBATWorksheet).Worksheets(1)

    'Firstly, remove the AutoFilter
    ws1.AutoFilterMode = False

    'Filter the range
    rng.AutoFilter Field:=FieldNum, Criteria1:="=" & cell.Value

    'Copy the visible data and use PasteSpecial to paste to the new worksheet
    ws1.AutoFilter.Range.Copy
    With WSNew.Range("B4")
    ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
    .PasteSpecial Paste:=8
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    .Select
    End With


    'Save the file in the new folder and close it
    WSNew.Parent.SaveAs foldername & " Value = " _
    & cell.Value & FileExtStr, FileFormatNum
    WSNew.Parent.Close False

    'Close AutoFilter
    ws1.AutoFilterMode = False

    Next cell

    'Delete the ws2 sheet
    On Error Resume Next
    Application.DisplayAlerts = False
    .Delete
    Application.DisplayAlerts = True
    On Error GoTo 0

    End With



    MsgBox "Look in " & foldername & " for the files"

    With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
    End With
    End Sub[/VBA]


    However I need to edit this macro a little bit to copy first row to every single new workbook created

    when I add such an code it doesn't work


    [VBA]'copy first row
    ws1.Range("A1:A11").Copy
    With WSNew.Range("A1:A11").PasteSpecial[/VBA]Could you please help me to fix this issue ?

  2. #2
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Remove the [VBA]with[/VBA]

Posting Permissions

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