Consulting

Results 1 to 6 of 6

Thread: Creating a new CSV file using VBA code

  1. #1

    Creating a new CSV file using VBA code

    Dear all, Here I have following VBA code :

    [VBA]Sub calculation()
    Dim test(1 To 5, 1 To 5) As Double
    Dim i, j As Integer
    For i = 1 To 5
    For j = 1 To 5
    test(i, j) = 5
    Next
    Next

    end sub[/VBA]

    Here I want to save the above "test" array into a CSV file, which itself needs to be generated on the fly once the sub "calculation" is being executed. Can anyone please tell me how to do that? Your help will be highly appreciated.

    Thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Simplest to write to a new sheet and save that
    [VBA]
    Sub calculation()
    Dim test(1 To 5, 1 To 5) As Double
    Dim i, j As Integer
    Dim sh As Worksheet
    Dim Pth As String

    Pth = ActiveWorkbook.Path
    Set sh = Sheets.Add
    For i = 1 To 5
    For j = 1 To 5
    sh.Cells(i, j) = 5
    Next
    Next
    sh.Move
    ActiveWorkbook.SaveAs Filename:=Pth & "\Test.csv", FileFormat:=xlCSV
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks for this reply. This is working fine. However here "test" becomes visible and it becomes the active workbook. Is there any trick to save "test" in some invisible way and keep existing workbook as active?

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try savecopyas instead of saveas
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub calculation()
    Dim test(1 To 5, 1 To 5) As Double
    Dim i, j As Integer
    Dim sh As Worksheet
    Dim Pth As String

    Application.ScreenUpdating = False

    Pth = ActiveWorkbook.Path
    Set sh = Sheets.Add
    For i = 1 To 5
    For j = 1 To 5
    sh.Cells(i, j) = 5
    Next
    Next
    sh.Move
    With ActiveWorkbook
    .SaveAs Filename:=Pth & "\Test1.csv", FileFormat:=xlCSV
    .Close False
    End With
    Application.ScreenUpdating = True
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Thanks it worked.

Posting Permissions

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