Consulting

Results 1 to 8 of 8

Thread: How to create a file?

  1. #1

    How to create a file?

    I want to copy an xls file and rename it? I can use the filecopy statement but It needs the destination file to be created first. But How can I create a file through vba and rename. Kindly help. I want the easiest possible way. I think it will be a one liner. Thank you.

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    I am not sure I understand what you are asking. Does this do the trick?

    [vba]Option Explicit

    Sub exa()
    Dim FilePath As String
    '// Change path or paths to suit //
    FilePath = ThisWorkbook.Path & "\"
    FileCopy FilePath & "Test.xls", FilePath & "TestCopy.xls"
    End Sub[/vba]
    Last edited by Aussiebear; 10-16-2010 at 02:41 AM. Reason: Adjusted correct code tags for User

  3. #3
    GTO, How to create that TestCopy.xls through vba?

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I must be missing something. Did you try the code?

  5. #5
    FileCopy Statement won't create the file but replace the bytes of the Source with the Destination. It wont create the "Destination File"

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    You said you want to "...copy an xls file and rename it..."

    Are you actually wanting to create a new/blank file?

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    FileCopy was not designed to copy the current file for some odd reason.

    Before doing something like that, you should probably save your file.

    Your logic is reversed. A copy of the source is put into the destination.

    When you copy a file, you also need to consider what to do if the file exists already.

    With no error checking:
    [VBA]Sub t()
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile ThisWorkbook.FullName, ThisWorkbook.Path & "\" & "TestCopy.xls", True 'overwrite
    Set fso = Nothing
    End Sub[/VBA]

  8. #8
    Thank you kenneth

Posting Permissions

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