Consulting

Results 1 to 4 of 4

Thread: Copy to closed wbk

  1. #1
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location

    Copy to closed wbk

    Hi Guys,

    hope you are doing fine.

    Please help me with the code. I want to copy some range into a closed workbook.
    [vba]Sub PutDataInClosedWorkbook()
    Dim wb As Workbook, CurDatRng As Range, NextDatRng As Range, Slastrow As Long
    Dim sFileName As String
    Application.ScreenUpdating = False ' turn off the screen updating
    Sheets("MainData").Activate
    With CurDatRng
    Set CurDatRng = Range([B3], [B3].End(xlDown))
    Set CurDatRng = CurDatRng.Offset(0, -1).Resize(CurDatRng.Rows.Count, 24)
    End With
    Set wb = Workbooks.Open("F:\MY\CTC\DBase\CDB.xls", True, False)
    ' read data to the target-from the source workbook...wb is the target
    Sheets("WriteTemp").Activate
    With ActiveSheet
    Slastrow = Cells.SpecialCells(xlCellTypeLastCell).row
    End With

    wb.Worksheets("MainData").Range(CurDatRng).Formula = Thisworkbook.Worksheets("WriteTemp").Range([A2], "A" & Slastrow + 1).Formula
    wb.Save
    wb.Close
    Set wb = Nothing
    Application.ScreenUpdating = True
    MsgBox "data transfer complete"
    End Sub[/vba]

    code error in this line [vba]wb.Worksheets("MainData").Range(CurDatRng).Formula = Thisworkbook.Worksheets("WriteTemp").Range([A2], "A" & Slastrow + 1).Formula[/vba]

    Thanks for assistance.
    Rgds, harto

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is always best to open the workbook, copy the data, then close it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor
    Joined
    Nov 2006
    Location
    North East Pennsylvania, USA
    Posts
    203
    Location
    slamet Harto,

    Try this for a start:
     
        wb.Worksheets("MainData").Range(CurDatRng).Copy Thisworkbook.Worksheets("WriteTemp").Range([A2], "A" & Slastrow + 1)

    Have a great day,
    Stan

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Before that line do this:
    [vba]debug.print "Slastrow", Slastrow
    debug.print wb.Worksheets("MainData").Range(CurDatRng).Address
    debug.print Thisworkbook.Worksheets("WriteTemp").Range("A2", "A" & Slastrow + 1).Address[/vba] This will help you debug the problem. Play the macro and check the Immediate Window to see if the results are as you expected.

Posting Permissions

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