Office & Productivity Software forum

General discussion

Excel macro: Make a macro delete itself?

I need help with a macro. The macro runs automatically, imports a .txt file, then saves the worksheet with a new name.

Problem is, when I open the new worksheet the macro tries to run again. Here's what I have:

Sub Auto_Open()
' Auto_Open Macro

Range(Selection, Selection.End(xlDown)).Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;c:\xxxxxxx\xxxxxx\users\xxxxxxx\xxxx.txt", Destination:=Range("A2"))
.Name = "sy060z1_14"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 9, 9, 1, 1, 1, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.SaveAs Filename:="c:\xxxxxxx\xxxxxx\users\xxxxxxx\xxxx.xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.RunAutoMacros Which:=xlAutoClose
End Sub

I need to make the macro delete itself, then resave the file under a new name. I don;t know if this is even possible, since it's still running. Can someone help me out with this?

Discussion is locked
You are posting a reply to: Excel macro: Make a macro delete itself?
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: Excel macro: Make a macro delete itself?
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Collapse -
Use the command line.

In reply to: Excel macro: Make a macro delete itself?

Collapse -
Not sure you need the macro to delete itself...

In reply to: Excel macro: Make a macro delete itself?

The other reply sounds like good info. Here is a macro that runs automatically, imports a .txt file and then saves itself as you requested. It also asks for a users input... but you can hardcode that if you like. You can put this code in a specific excel file or put in in your Personal.xls file. Hope this helps...

Sub Auto_Open()
' Auto_Open Macro


Application.DisplayAlerts = False
input1 = Application.InputBox(prompt:="What is your filename prefix?", Type:=2)

'quick error trap
'If user clicked cancel
If y_n_input = 2 Then Exit Sub

'hardcoded this
ChDir "C:\New_Folder"

Workbooks.OpenText Filename:="" & input1 & ".txt", Origin:= _
437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=False, _
Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _
, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1)), _

'insert statements here if you want to do stuff to file

ActiveWorkbook.SaveAs Filename:="C:\New_Folder\" & input1 & ".xls", _
FileFormat:=xlText, CreateBackup:=False

Application.DisplayAlerts = True
End Sub

Popular Forums

Computer Newbies 10,686 discussions
Computer Help 54,365 discussions
Laptops 21,181 discussions
Networking & Wireless 16,313 discussions
Phones 17,137 discussions
Security 31,287 discussions
TVs & Home Theaters 22,101 discussions
Windows 7 8,164 discussions
Windows 10 2,657 discussions


Enter to win* a free holiday tech gift!

CNET's giving five lucky winners the gift of their choice valued up to $250!