Results 1 to 10 of 10

Can VBA copy data from an Excel Sheet to a *.csv workbook which is not open?

Threaded View

  1. #1
    Forum Contributor
    Join Date
    03-31-2010
    Location
    Norway
    MS-Off Ver
    Excel 365
    Posts
    632

    Can VBA copy data from an Excel Sheet to a *.csv workbook which is not open?

    Hi,

    I was able to set up a workbook with an associated macro which copies filtered data to a *.csv file. This one works like a charm. However, since I will be using this *.csv file as input on another program I need to close the *.csv file. Thus, it would be advantageous if the copy process could be done to the *.csv file without opening it.

    Is this possible at all without too much trouble?

    An alternative solution could be to add VBA code which 1) Opens *.csv file; 2) Copies data using my existing macro; 3) Close the *.csv file.

    Any thoughts?

    I use one macro for clearing the *.csv before making a copy:

    Sub ClearCSV()
        Windows("NinjaTrader CSV.csv").Activate
        Columns("A:B").Select
        Selection.Delete Shift:=xlToLeft
        Windows("Main Sheet.xlsm").Activate
    End Sub
    And one macro for copying filtered data from the *.csv:

    Sub CopyFilteredData()
    Dim rng As Range
    Dim res, ar
    Dim coll As New Collection
    For Each rng In ActiveWorkbook.ActiveSheet.ListObjects(1).DataBodyRange.Columns("A:B").SpecialCells(xlCellTypeVisible).Rows
        ar = rng.Value
        coll.Add ar
    Next
    ReDim res(1 To coll.Count, 1 To UBound(ar, 2))
    
    For i = 1 To coll.Count
        For j = 1 To UBound(coll(i), 2)
            res(i, j) = coll(i)(1, j)
        Next
    Next
    Workbooks("Ninjatrader CSV.csv").Sheets("NinjaTrader CSV").Range("A1").Resize(UBound(res), UBound(res, 2)) = res
    
    End Sub
    I run both together using this:

    Sub RunAllMacros()
    ClearCSV
    CopyFilteredData
    End Sub
    Best regards,

    Elijah
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Excel VBA copy and paste sheet to open workbook
    By Hoover5896 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-22-2017, 02:25 PM
  2. Open All Excel Files in Folder and Copy and Paste Data in Macro-Running Workbook
    By dhfinancex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2017, 01:48 PM
  3. Simple Open Workbook, Copy Sheet, Paste In New Workbook Question
    By alulla in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2013, 02:10 PM
  4. Replies: 0
    Last Post: 05-08-2013, 02:47 PM
  5. [SOLVED] COPY from an open workbook with particular sheet and columns to active workbook
    By purin0c0 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-12-2012, 03:57 PM
  6. Macro, copy UNLOCKED cell range of sheet in open workbook to workbook template
    By NicksDad in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-31-2012, 11:18 AM
  7. [SOLVED] Macro to copy data from open workbook to next open line of new workbook
    By nhtodd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2012, 09:42 PM

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