+ Reply to Thread
Results 1 to 2 of 2

Excel VBA Automate to open CSV comma delimited file and copy to worksheet

Hybrid View

Jovillanueva Excel VBA Automate to open... 11-06-2014, 03:32 AM
alansidman Re: Excel VBA Automate to... 11-06-2014, 04:43 AM
  1. #1
    Forum Contributor
    Join Date
    07-17-2011
    Location
    PH
    MS-Off Ver
    Excel 2007
    Posts
    183

    Excel VBA Automate to open CSV comma delimited file and copy to worksheet

    Hi,

    I have a CSV file that was exported from an SSRS report.
    I have an excel with Button (form control) to open a CSV file. I'm trying to write a VBA macro to automate to open a CSV file comma delimited and copy the records to a worksheet. Any help is very much appreciated. thanks.

    Below is a sample data.
    The first row is the heading
    Second row is the date and other parameters.
    The Third row is the column heading
    the Fourth row is the data that I would like to copy using vba excel

    textbox1,txtFromDate,textbox3,textbox65,textbox67
    11/5/2014 6:00 AM,11/6/2014 6:00 AM,User,TOP-FJ-PCM,ALL
    
    lineid,textbox100,textbox101,textbox102,txtWrkCtr,textbox54,textbox72,textbox77
    TOP_Line30,2676,2597,79,FJ_BAFF,363,361,2,FAJ1101-5-S,363,361,2
    Last edited by Jovillanueva; 11-06-2014 at 03:34 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,783

    Re: Excel VBA Automate to open CSV comma delimited file and copy to worksheet

    Try this VBA

    Option Explicit
    Sub impCSV()
    
    Dim lr As Long
    Dim lrC As Long
    Dim wbTarget As Workbook 'Comments Worksheet
    Dim wbThis As Workbook  'Current Open Workbook
    Dim strName As String 'Name for source sheet/target workbook
    Dim thePath As String  'Path for Master Spreadsheet
    Dim i As Long 'Name variable to loop
    
        Application.ScreenUpdating = False
    
    'set the current active workbook
        Set wbThis = ActiveWorkbook
    'set the target workbook name; change to your wb name
        strName = "MyNewBook"
    'set the path to the Target Spreadsheet Change this to reflect your path
        thePath = "C:\Users\Alan Sidman\Desktop\"
    'open New Spreadsheet
        Set wbTarget = Workbooks.Open(thePath & strName & ".xlsx")
    'Activate the Target Workbook
        wbTarget.Activate
    
    'activate source workbook
        wbThis.Activate
    'find the last row in column A to determine the range to copy
        lr = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    'clear any thing on the clipboard to mazimize available memory
        Application.CutCopyMode = False
        
        
        For i = 4 To lr
    'Find the last row in the target workbook
        lrC = wbTarget.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    'Copy 4th Row and paste
        wbThis.Sheets("Sheet1").Range("A" & i).EntireRow.Copy wbTarget.Sheets("Sheet1").Range("A" & lrC + 1)
    'Clear the clipboard
        Application.CutCopyMode = False
        wbThis.Activate
    'Skip to next fourth row
    i = i + 3
    Next i
       
        wbTarget.Save
        wbTarget.Close
        wbThis.Activate
        Application.ScreenUpdating = True
             
    'clear memory
        Set wbTarget = Nothing
        Set wbThis = Nothing
        MsgBox "complete"
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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. Macro to convert comma delimited csv file to excel file??
    By sanjeevpandey in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-15-2012, 05:31 AM
  2. Saving multi-tab excel file created from comma delimited text file
    By Marcus Aurelius in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2005, 01:20 PM
  3. [SOLVED] Create a form to open a comma delimited file
    By ~Rick in forum Excel General
    Replies: 1
    Last Post: 09-29-2005, 04:05 PM
  4. Replies: 1
    Last Post: 05-03-2005, 06:06 PM
  5. save an Excel worksheet as a comma delimited file?
    By Trophy Man in forum Excel General
    Replies: 2
    Last Post: 01-08-2005, 12:06 AM

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