+ Reply to Thread
Results 1 to 14 of 14

Thread: Open file with macro

  1. #1
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Open file with macro

    Hello all!
    This is the first time I use a macro so bear with me

    Im using Excel 2011 for mac.

    What I'm trying to do is to open a CSV file but I want the macro to use a cell value as filename. That is, I want the macro to open a CSV with the filename stated in a cell value. So, in the code below I would like to change the filename to a variable that is populated from a cell. Also, I would like to create a variable for the path as this may change. See my code below. The first example works and if I change the filename and path to variables it doesnt work, see second example.

    Any suggestions how to change my code are appreciated.

    Br
    cristian

    I have this code:
    Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "CSV"
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\Excel Test\hello.csv", Destination:=Range( _
            "$A$1"))
            .Name = "hello"
            .FieldNames = True
           ...
           ...
           ..
    
        End With
    And I want to change to something like this (this doesn't work):
    
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "CSV"
        Dim FName As String
        FName = Range("R42").Value
        Dim FPath2 As String
        FPath2 = Range("R52").Value
       
        With ActiveSheet.QueryTables.Add(Connection:= _
     TEXT;FPath2 & "\" & FName & ".csv", Destination:=Range( _
            "$A$1"))
            
            .Name = FName
            
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
    ...
    ..
    .

  2. #2
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Open file with macro

    I am not familiar with Mac, but from what I can see, your line in Red should start with "
    Please consider:

    Be polite. Thank those who have helped you. Click the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Re: Open file with macro

    Thank you Winon. It doesn't help. The error message is " Expected separator" now....It seems that it expects some kind of separator and it points out the dot in ".csv"

    "TEXT;FPath2 & "\" & FName & ".csv"

  4. #4
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Open file with macro

    What is the exaxt data you have in R42? It should be without a file extention, i.e. "Filename".csv or "Filename".xlsx etc.
    Please consider:

    Be polite. Thank those who have helped you. Click the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  5. #5
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Re: Open file with macro

    The value of cell R42 is "FileName" without any extention. Hmm...I will test this tomorrow on a Win-computer.

  6. #6
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Re: Open file with macro

    ok,
    This seems to work I think. But, the variables are empty. The variables are in sheet1 and the new sheet created with name CSV is the active one. How do I populate the variable from cell R42 in Sheet1?
     Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "CSV"
        Dim FName As String
        FName = Range("R42").Value
        Dim FPath2 As String
        FPath2 = Range("R52").Value
       
        With ActiveSheet.QueryTables.Add(Connection:= _
     FPath2 & FName & ".csv", Destination:=Range( _
            "$A$1"))

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Open file with macro

    Start simply:

    assuming

    In cel A1: the path eg. E:\OF\
    in cel B1: the filename example.csv

    Sub snb()
      workbooks.open thisworkbook.sheets("sheet1").range("A1").value & thisworkbook.sheets("sheet1").Range("B1").value 
    End Sub



  8. #8
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Open file with macro

    Or you could try,

    Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "CSV"
        Dim FName As String
        FName = Range("R42").Value
        Dim FPath2 As String
        FPath2 = Range("R52").Value
       
        With ActiveSheet.QueryTables.Add(Connection:= _
     FPath2 & FName & ".csv", Destination:=Range( _
            "$A$1"))
    Range("R42")=Sheet1.Range("R42")
    @snb,

    Hi snb, Thanks for helping!
    Please consider:

    Be polite. Thank those who have helped you. Click the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  9. #9
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Re: Open file with macro

    Ok, Thanks. Now I can see that the variables are populated with the right value. But I still get an error message: "Application-defined or object-defined error" and points out the red code below. Im importing a .csv file to the new sheet CSV.

    Update: I can also see that the Destination:=Range(... points to another sheet that is not CSV-sheet.

    This is my code now:
    Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "CSV"
        Dim FName As String
        FName = ThisWorkbook.Sheets("Sheet1").Range("R42").Value
        Dim FPath2 As String
        FPath2 = ThisWorkbook.Sheets("Sheet1").Range("R52").Value
       
        With ActiveSheet.QueryTables.Add(Connection:= _
     FPath2 & "\" & FName & ".csv", Destination:=Range( _
            "$A$1"))
            
            .Name = FName
    Last edited by currambero; 01-20-2012 at 04:41 AM.

  10. #10
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Re: Open file with macro

    Ok. Now I got it. The problem with pointing out the wrong sheet was my bad. But now I have a new problem.
    Error:
    "Excel cannot find the text file to refresh this external data range. Check to make sure the text file has not been moved or renamed, then try the refresh again."

    Code in red is highlighted.

     Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "CSV"
        Dim FName As String
        FName = ThisWorkbook.Sheets("Sheet1").Range("R42").Value
        Dim FPath2 As String
        FPath2 = ThisWorkbook.Sheets("Sheet1").Range("R52").Value
       
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & FPath2 & "\" & FName & ".csv", Destination:=Range("$A$1"))
            
            .Name = FName
           ... 
           ...
            .Refresh BackgroundQuery:=False
        End With

  11. #11
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Open file with macro

    Or you end the pathname with a backslash, or you add a backslash in the connectionstring, but do not both.
    If doing so you will get 2 backslashes.

    Besides how are we supposed to read what is in cells R45 and R52, let alone what's on your system.

    Did you read in the forum rules how to help helpers ? By posting a sample workbook for instance !



  12. #12
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Re: Open file with macro

    TestWorkBook.xlsm


    I have checked the backslashes. Nothing there.

    The value of R42 is just "FileName". That is, I want the filename to be FileName.xsl

    Value of R52 is "C:\Excel Test\FileName.csv"

    Regarding the sample workbook I thought it would be enough with my code. Here's a sample doc. I dont manage to attach the CSV file.

    Br
    cristian

  13. #13
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Open file with macro

    Hello currambero,

    I think you have answered your own problem:

    The value of R42 is just "FileName". That is, I want the filename to be FileName.xsl
    Enter FileName.xsl in R42
    Please consider:

    Be polite. Thank those who have helped you. Click the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  14. #14
    Registered User
    Join Date
    01-19-2012
    Location
    Svweden
    MS-Off Ver
    Excel 2011 Mac
    Posts
    8

    Re: Open file with macro

    Hello!
    Thank you all for you patience and help! It works!!!!

    I did a misstake on my path when working with different computers - Mac/Win. Forward slash/backward slash...

    BR
    cristian

    Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "CSV"
        Dim FName As String
        FName = ThisWorkbook.Sheets("Sheet1").Range("R42").Value
        Dim FPath2 As String
        FPath2 = ThisWorkbook.Sheets("Sheet1").Range("R52").Value
       
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & FPath2 & "\" & FName & ".csv", Destination:=Range("$A$1"))
            
            .Name = FName
           ... 
           ...
            .Refresh BackgroundQuery:=False
        End With

+ Reply to Thread

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.2.0