Can VBA code be programmed to change itself?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
Given that VBA code can be programmed to make virtually any changes in Excel, Word, etc., I was wondering if VBA code can be written to change itself, for example, by deleting itself if certain conditions are met, for security purposes?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
One way:

Code:
Sub KillMe()</SPAN></SPAN></SPAN>
    With ThisWorkbook</SPAN></SPAN></SPAN></SPAN>
        If Len(Dir(.FullName)) Then</SPAN></SPAN></SPAN></SPAN>
            .Saved = True</SPAN></SPAN></SPAN></SPAN>
            On Error Resume Next</SPAN></SPAN></SPAN></SPAN>
            .ChangeFileAccess Mode:=xlReadOnly</SPAN></SPAN></SPAN></SPAN>
            On Error GoTo 0</SPAN></SPAN></SPAN></SPAN>
            SetAttr .FullName, vbNormal</SPAN></SPAN></SPAN></SPAN>
            Kill .FullName</SPAN></SPAN></SPAN></SPAN>
        End If</SPAN></SPAN></SPAN></SPAN>
        .Close SaveChanges:=False</SPAN></SPAN></SPAN></SPAN>
    End With</SPAN></SPAN></SPAN></SPAN>
End Sub</SPAN></SPAN></SPAN></SPAN>
 
Upvote 0
Deletes the file containing the code.
 
Upvote 0
The Excel spreadsheet (including any data), or just the VBA code?

If it's the entire Excel file, then I guess it would have to be used as an Addin, to avoid losing any data, right?
 
Upvote 0
Code:
Sub KillMyCode()
    Dim sFile       As String

    With ThisWorkbook
        sFile = .FullName

        If Len(Dir(sFile)) > 0 And Right(sFile, 4) = "xlsm" Then
            Application.DisplayAlerts = False
            .SaveAs Filename:=Left(sFile, InStrRev(sFile, ".")) & "xlsx", _
                    FileFormat:=xlOpenXMLWorkbook
            SetAttr sFile, vbNormal
            Kill sFile
        End If

        .Close SaveChanges:=False
    End With
End Sub
 
Upvote 0
Sorry if this is an old thread, but I'm actually ruminating on that same question myself:

Would it be possible for the VBA code, instead of changing itself in a destructive manner, to change itself in a constructive manner? Such as expanding its own ranges or including new sheets in a coded query?

I'm going to start an experiment where a spreadsheet queries, returns results, and develops new queries. And i'm curious how the VBA might develop new sheets and include those sheets in the next query.

Any suggestions?
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top