+ Reply to Thread
Results 1 to 10 of 10

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

Hybrid View

Elijah Can VBA copy data from an... 03-26-2019, 03:58 PM
mjr veverka Re: Can VBA copy data from an... 03-26-2019, 05:28 PM
Elijah Re: Can VBA copy data from an... 03-27-2019, 05:36 AM
Elijah Re: Can VBA copy data from an... 03-27-2019, 08:46 AM
Elijah Re: Can VBA copy data from an... 03-27-2019, 01:53 PM
Elijah Re: Can VBA copy data from an... 03-27-2019, 02:58 PM
mjr veverka Re: Can VBA copy data from an... 03-27-2019, 03:08 PM
Elijah Re: Can VBA copy data from an... 03-27-2019, 03:16 PM
Elijah Re: Can VBA copy data from an... 03-27-2019, 03:38 PM
Elijah Re: Can VBA copy data from an... 03-27-2019, 04:15 PM
  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

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,314

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

    Quote Originally Posted by Elijah View Post
    ... Thus, it would be advantageous if the copy process could be done to the *.csv file without opening it ...
    It is not possible, every file (disk file/collection/set) to which you want to save something must be opened from the disk in the appropriate mode (for writing), saved, then closed, this cannot be done otherwise.

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

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

    Quote Originally Posted by porucha vevrku View Post
    It is not possible, every file (disk file/collection/set) to which you want to save something must be opened from the disk in the appropriate mode (for writing), saved, then closed, this cannot be done otherwise.
    Here, I assume you're referring to VBA specifically?

    Because I have other software updating another *.csv file while it's closed.

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

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

    If it is indeed not possible, I will try to add Open & Save&Close at beginning/end of my current macro.

    I found some links here which should be helpful, but won't have time until later tonight.

    https://www.excel-easy.com/vba/examples/close-open.html

    https://analysistabs.com/excel-vba/o...ting-workbook/

    Regards.

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

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

    Allright. So, I was able to figure this out step-by-step using multiple macros:

    1. Open CSV

    Sub OpenPartDayCsv()
    
    Set wkb = Workbooks.Open("C:\Users\johan\Desktop\Ninjatrader CSV.csv")
    
    End Sub
    2. Clear CSV

    Already shown above in post 1.

    3. Copy filtered data to CSV

    Already shown above in post 1.

    4. Close and save CSV

    Sub ClosePartDayCsv()
    
    Workbooks("Ninjatrader CSV.csv").Close SaveChanges:=True
    
    End Sub
    Run separately in sequence (1, 2 , 3, 4 and then 1 again), this works fine and stores data in two columns, but if I run them all in sequence like the code shown below, my two columns in the *.csv are merged in column A.

    Merged.PNG

    Any idea why?

    Sub RunAllMacros()
    
    OpenPartDayCsv
    
    ClearCSV
    
    CopyFilteredData
    
    ClosePartDayCsv
    
    End Sub
    Attached Files Attached Files
    Last edited by Elijah; 03-27-2019 at 01:56 PM.

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

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

    Seems like the problem MIGHT be that my regional settings are over-ruled when using VBA to close my *.csv file. It doesn't apply when doing it separately though.

    Trying my best to implement a Save with Local settings = True, but struggling a bit here. Will keep trying, but if anyone know it, I'd be very grateful.
    Last edited by Elijah; 03-27-2019 at 03:02 PM.

  7. #7
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,314

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

    Quote Originally Posted by Elijah View Post
    ... you're referring to VBA specifically?
    ... other software updating ... *.csv file while it's closed
    Not only, it's just your illusion. Every disk file, in order to write something in it, must first be opened with the appropriate mechanism of a given program, that does it. Only the GUI of this mechanism of this program is not always visible.

    Quote Originally Posted by Elijah View Post
    ... I found some links here which should be helpful ...
    Did you look for it on the forum? A lot of this should be here.
    Last edited by mjr veverka; 03-27-2019 at 03:12 PM.

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

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

    Quote Originally Posted by porucha vevrku View Post
    Not only, it's just your illusion. Every disk file, in order to write something in it, must first be opened with the appropriate mechanism of a given program, that does it. Only the GUI of this mechanism of this program is not always visible.
    Understood.

    Quote Originally Posted by porucha vevrku View Post
    Did you look for it on the forum? A lot of this should be here.
    Well, as you can see above, I was able to piece together 4 steps which does exactly what I want.

    The problem is that when executing all 4 together, my *.csv file seems to be saved incorrectly. That is, the local settings on my computer are not used... I end up with all data in one column.

    I've tried correcting the code and implementing it, but can't figure it out on my own...

    But still working on it.

    Any ideas?

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

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

    So, I've tried this in order to force a close & save using my local settings and data split in separate columns. But it does not work...

    Sub ClosePartDayCsv()
    
    With Workbooks("Ninjatrader CSV.csv")
    .SaveAs Filename:="Ninjatrader CSV.csv", FileFormat:=xlCSV, Local:=True
    .Close SaveChanges:=False
    End With
    
    End Sub

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

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

    Allright. It seems like that actually worked.

    BUT, I didn't notice because it's the opening of the *.csv which causes me trouble and I've been using that method.

    Any idea why?

    More precisely:

    1. If I open manually by clicking the *.csv file it opens correctly (data in separate columns) now when using my last sub above in post #9 to save the *.csv.

    2. If I open using the single VBA code shown above, it opens incorrectly.

    For my purposes, I don't need to open it with VBA correctly, so problem solved, I think. I only need to be stored correctly.

+ Reply to Thread

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