Consulting

Results 1 to 7 of 7

Thread: Solved: Changing a filename

  1. #1
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location

    Solved: Changing a filename

    hi,

    I have a code that opens a .csv file from the company's intranet.
    Data is then copied & pasted from the opened file to the workbook that generated the macro. And then the file is closed.
    The file that is opened is referenced by the EndUser that generates the report. e.g. USERNAME.REPORTNAME.csv.
    I can run this macro without a problem because I am the one generating the request. However,if somebody else ran this report, they cannot open their own report because the link to the file is hardcoded with my UserName in the coding.

    Is there any way to have the EndUser input their USERNAME and then somehow link this USERNAME to the file that gets opened?

    [vba]

    Workbooks.Open Filename:= _
    "http://xxxxxxxxxx.xxxxxx.com/xxxxxx/...REPORTNAME.CSV"
    ActiveWindow.Visible = True
    Application.DisplayAlerts = False
    '=============
    Windows("USERNAME.REPORTNAME.CSV").Activate
    Cells.Select
    Selection.Copy
    Windows("ORIGINALREPORTNAME.xls").Activate
    Sheets("Sheet1").Select
    Range("A1").Select
    ActiveSheet.Paste
    Windows("USERNAME.REPORTNAME.CSV").Close
    [/vba]


    thanks
    zach

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim sUser As String

    sUser = Environ("UserName")
    Workbooks.Open Filename:= _
    "http://xxxxxxxxxx.xxxxxx.com/xxxxxx/xxxxxx/xxxxxx/" & sUser & ".REPORTNAME.CSV"
    ActiveWindow.Visible = True
    Application.DisplayAlerts = False
    '=============
    Windows(sUser & ".REPORTNAME.CSV").Activate
    Cells.Select
    Selection.Copy
    Windows("ORIGINALREPORTNAME.xls").Activate
    Sheets("Sheet1").Select
    Range("A1").Select
    ActiveSheet.Paste
    Windows(sUser & ".REPORTNAME.CSV").Close
    [/vba]

  3. #3
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi EL Xld,

    thanks for your speedy reply.
    I modified my code but i don't understand how and where the EndUser can enter their "username" when running the macro


    zach

  4. #4
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    zach,
    sUser = Environ("UserName")
    retrieves the user name from the system, so the user doesn't need to enter thier user name, just need to be signed in as themselves.

    HTH
    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  5. #5
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi CBrine,

    thanks for the explanation. i see how that works now.
    the problem i have now is that the username from the system is in lower case while the report is in upper case. is there a trick around this?

    thanks again
    zach

  6. #6
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    "http://xxxxxxxxxx.xxxxxx.com/xxxxxx/xxxxxx/xxxxxx/" & Ucase(sUser) & ".REPORTNAME.CSV"

    This should fix it up.

    HTH
    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  7. #7
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    Cbrine,
    That is amazing and so simple!

    Thanks again
    zach

Posting Permissions

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