Home > OS >  How do you make VBA document open a copy of itself in a folder every time you open it?
How do you make VBA document open a copy of itself in a folder every time you open it?

Time:11-03

When you run this program it opens a csv file in the allocated folder. What should i write in this code for when i open the file it makes a copy?

Open "C:\raspbbbb\users.csv" For Output As #1
i = 2
Do While Cells(i, 1) <> ""
myStr = ""
For j = 9 To 10
myStr = myStr & Cells(i, j) & "; "
Next
Print #1, myStr
i = i   1
Loop
Close #1
End Sub

I tried changing the output and close types but that just makes it crash

CodePudding user response:

In the code module for ThisWorkbook, create an event script for Workbook_Open:

Private Sub Workbook_Open()
    Dim CopyName As String
    CopyName = Split(ThisWorkbook.Name, ".")(0) & " Copy." & Split(ThisWorkbook.Name, ".")(1)
    ThisWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & CopyName
End Sub

Save the file as a Macro Enabled Workbook, xlsm. Now when you open the workbook, it will create a copy of itself in the same folder.

CodePudding user response:

I wonder if you could possibly rephrase the question as it is not too clear what you are trying to achieve, and what is then going wrong? Many thanks.

  • Related