Results 1 to 3 of 3

Open 2nd file(CSV) from cell reference, copy columns to main file & close 2nd file

Threaded View

  1. #1
    Registered User
    Join Date
    01-31-2013
    Location
    Johannesburg
    MS-Off Ver
    Excel 2003/ 2010
    Posts
    4

    Open 2nd file(CSV) from cell reference, copy columns to main file & close 2nd file

    Hi There

    I am hoping someone can help me with this.. I know it should not be too complicated but my VB knowledge is purely from trial and error and not understanding.

    What I want to do is the following (I want to run these steps as separate macros for simplicity and ease of faultfinding, and run them sequentially using 'Application.Run "nextmacroname" ' function):

    Macro 1. In the main workbook (That houses the macros), a filepath and filename for a second workbook is given in cell A1 of sheet "DATA", and this is a variable ("c:\datafolder\130131.csv", which is derived from a formula and set to text format). The macro must open this second workbook (CSV opens as a workbook), and give an error message if the file is not found/ does not exist.

    Macro 2. This macro must ensure that the second workbook (again, the workbook name based on the variable file name in cell A1 of main workbook) is active [being a csv file format, it has only 1 worksheet with same name as the workbook itself ("130131")... not sure if the worksheet would also need to be 'activated' in this case ], select the first 3 columns of data (columns A:C), copy these, and paste these into the first 3 columns of the "DATA" worksheet of the main workbook.

    Macro 3. Close the second workbook (again, as before, workbook name based on the variable file name in cell A1 of main workbook), and ensure that any warnings are ignored (ie. 'do you want to save' , 'format may have changed' etc etc)

    From here I would be able to manipulate the data that has just been 'imported'



    What I have so far is the following:

    Macro 1: Some coding that I found on the forum which seems to work perfectly for this step:

    Sub opencsvfromcell()
    '
    ' opencsvfromcell Macro
    ' open a csv file from name in a given cell
    '
    ' Keyboard Shortcut: Ctrl+l
    '
        Dim strFName As String
        
        strFName = Sheet1.Range("A1").Value
        'this variable contains the workbook name and path
        If FileExists(strFName) Then
        'does it exist?
            If Not BookOpen(Dir(strFName)) Then Workbooks.Open Filename:=strFName
            'if its not already open, open it
        Else
            MsgBox "The file does not exist!"
        End If
        
    End Sub
    
    Function FileExists(strfullname As String) As Boolean
        FileExists = Dir(strfullname) <> ""
    End Function
    
    Function BookOpen(strWBName As String) As Boolean
        Dim wbk As Workbook
        On Error Resume Next
        Set wbk = Workbooks(strWBName)
        If Not wbk Is Nothing Then BookOpen = True
    End Function
    (dont actually know why this works when my sheet is named "DATA" but this just refers to 'Sheet1 when referring to cell A1)

    Macro 2 & 3

    I have no idea how to do these... any help would be greatly appreciated


    Regards
    Lang
    Last edited by Langchop; 01-31-2013 at 05:06 AM.

Thread Information

Users Browsing this Thread

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

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