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?
 
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?

Use variables instead of ANYTHING absolute. Then you shouldn't have any problems.

For a dynamic range:

Code:
Sub dynamicRange()

    Dim startRow As Integer
    Dim endRow As Long
    
    Dim startCol As Integer
    Dim endCol As Long
    
    Dim rng As Range
    
    startRow = 1
    startCol = 1
    endRow = Cells(Rows.Count, "A").End(xlUp)
    endCol = Cells(1, Columns.Count).End(xlToLeft).column
    
    
    Set rng = Range(Cells(startRow, startCol), Cells(endRow, endCol))
    
    For x = startRow To endRow
        'CODE
    Next x


End Sub

Dynamic sheets:

Code:
Sub dynamicSheets()

    For x = 1 To Sheets.Count
        'code
    Next x


End Sub
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,215,506
Messages
6,125,197
Members
449,214
Latest member
mr_ordinaryboy

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